Пример #1
1
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         $url_is_valid = TRUE;
         /** @var $link_item \Drupal\link\LinkItemInterface */
         $link_item = $value;
         $link_type = $link_item->getFieldDefinition()->getSetting('link_type');
         $url_string = $link_item->url;
         // Validate the url property.
         if ($url_string !== '') {
             try {
                 // @todo This shouldn't be needed, but massageFormValues() may not
                 //   run.
                 $parsed_url = UrlHelper::parse($url_string);
                 $url = Url::createFromPath($parsed_url['path']);
                 if ($url->isExternal() && !UrlHelper::isValid($url_string, TRUE)) {
                     $url_is_valid = FALSE;
                 } elseif ($url->isExternal() && !($link_type & LinkItemInterface::LINK_EXTERNAL)) {
                     $url_is_valid = FALSE;
                 }
             } catch (NotFoundHttpException $e) {
                 $url_is_valid = FALSE;
             } catch (MatchingRouteNotFoundException $e) {
                 $url_is_valid = FALSE;
             } catch (ParamNotConvertedException $e) {
                 $url_is_valid = FALSE;
             }
         }
         if (!$url_is_valid) {
             $this->context->addViolation($this->message, array('%url' => $url_string));
         }
     }
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         $uri_is_valid = TRUE;
         /** @var $link_item \Drupal\link\LinkItemInterface */
         $link_item = $value;
         $link_type = $link_item->getFieldDefinition()->getSetting('link_type');
         // Try to resolve the given URI to a URL. It may fail if it's schemeless.
         try {
             $url = $link_item->getUrl();
         } catch (\InvalidArgumentException $e) {
             $uri_is_valid = FALSE;
         }
         // If the link field doesn't support both internal and external links,
         // check whether the URL (a resolved URI) is in fact violating either
         // restriction.
         if ($uri_is_valid && $link_type !== LinkItemInterface::LINK_GENERIC) {
             if (!($link_type & LinkItemInterface::LINK_EXTERNAL) && $url->isExternal()) {
                 $uri_is_valid = FALSE;
             }
             if (!($link_type & LinkItemInterface::LINK_INTERNAL) && !$url->isExternal()) {
                 $uri_is_valid = FALSE;
             }
         }
         if (!$uri_is_valid) {
             $this->context->addViolation($this->message, array('@uri' => $link_item->uri));
         }
     }
 }
Пример #3
0
 public function verifyCountryList($value, ExecutionContextInterface $context)
 {
     $jsonType = new JsonType();
     if (!$jsonType->isValid($value)) {
         $context->addViolation(Translator::getInstance()->trans("Country list is not valid JSON"));
     }
     $countryList = json_decode($value, true);
     foreach ($countryList as $countryItem) {
         if (is_array($countryItem)) {
             $country = CountryQuery::create()->findPk($countryItem[0]);
             if (null === $country) {
                 $context->addViolation(Translator::getInstance()->trans("Country ID %id not found", ['%id' => $countryItem[0]]));
             }
             if ($countryItem[1] == "0") {
                 continue;
             }
             $state = StateQuery::create()->findPk($countryItem[1]);
             if (null === $state) {
                 $context->addViolation(Translator::getInstance()->trans("State ID %id not found", ['%id' => $countryItem[1]]));
             }
         } else {
             $context->addViolation(Translator::getInstance()->trans("Wrong country definition"));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         try {
             /** @var \Drupal\Core\Url $url */
             $url = $value->getUrl();
         } catch (\InvalidArgumentException $e) {
             return;
         }
         if ($url->isRouted()) {
             $allowed = TRUE;
             try {
                 $url->toString();
             } catch (RouteNotFoundException $e) {
                 $allowed = FALSE;
             } catch (InvalidParameterException $e) {
                 $allowed = FALSE;
             } catch (MissingMandatoryParametersException $e) {
                 $allowed = FALSE;
             }
             if (!$allowed) {
                 $this->context->addViolation($constraint->message, array('@uri' => $value->uri));
             }
         }
     }
 }
Пример #5
0
 public function verifyTaxList($value, ExecutionContextInterface $context)
 {
     $jsonType = new JsonType();
     if (!$jsonType->isValid($value)) {
         $context->addViolation(Translator::getInstance()->trans("Tax list is not valid JSON"));
     }
     $taxList = json_decode($value, true);
     /* check we have 2 level max */
     foreach ($taxList as $taxLevel1) {
         if (is_array($taxLevel1)) {
             foreach ($taxLevel1 as $taxLevel2) {
                 if (is_array($taxLevel2)) {
                     $context->addViolation(Translator::getInstance()->trans("Bad tax list JSON"));
                 } else {
                     $taxModel = TaxQuery::create()->findPk($taxLevel2);
                     if (null === $taxModel) {
                         $context->addViolation(Translator::getInstance()->trans("Tax ID not found in tax list JSON"));
                     }
                 }
             }
         } else {
             $taxModel = TaxQuery::create()->findPk($taxLevel1);
             if (null === $taxModel) {
                 $context->addViolation(Translator::getInstance()->trans("Tax ID not found in tax list JSON"));
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($items, Constraint $constraint)
 {
     if (isset($items)) {
         $date_values = $items->getValue();
         if ($date_values[0]['value'] >= $date_values[1]['value']) {
             $this->context->addViolation($constraint->message);
         }
     }
 }
Пример #7
0
 /**
  * Wrapper for $this->context->addViolation()
  *
  * @deprecated Deprecated since version 2.1, to be removed in 2.3.
  */
 protected function setMessage($template, array $parameters = array())
 {
     $this->messageTemplate = $template;
     $this->messageParameters = $parameters;
     if (!$this->context instanceof ExecutionContext) {
         throw new ValidatorException('ConstraintValidator::initialize() must be called before setting violation messages');
     }
     $this->context->addViolation($template, $parameters);
 }
Пример #8
0
 public function verifyDeliveryModule($value, ExecutionContextInterface $context)
 {
     $module = ModuleQuery::create()->filterActivatedByTypeAndId(BaseModule::DELIVERY_MODULE_TYPE, $value)->findOne();
     if (null === $module) {
         $context->addViolation(Translator::getInstance()->trans("Delivery module ID not found"));
     } elseif (!$module->isDeliveryModule()) {
         $context->addViolation(sprintf(Translator::getInstance()->trans("delivery module %s is not a Thelia\\Module\\DeliveryModuleInterface"), $module->getCode()));
     }
 }
 public function verifyPasswordField($value, ExecutionContextInterface $context)
 {
     $data = $context->getRoot()->getData();
     if ($data["password"] != $data["password_confirm"]) {
         $context->addViolation(Translator::getInstance()->trans("password confirmation is not the same as password field"));
     }
     if ($data["password"] !== '' && strlen($data["password"]) < 4) {
         $context->addViolation(Translator::getInstance()->trans("password must be composed of at least 4 characters"));
     }
 }
Пример #10
0
 /**
  * Wrapper for $this->context->addViolation()
  *
  * @deprecated Deprecated since version 2.1, to be removed in 2.3.
  */
 protected function setMessage($template, array $parameters = array())
 {
     trigger_error('setMessage() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
     $this->messageTemplate = $template;
     $this->messageParameters = $parameters;
     if (!$this->context instanceof ExecutionContext) {
         throw new ValidatorException('ConstraintValidator::initialize() must be called before setting violation messages');
     }
     $this->context->addViolation($template, $parameters);
 }
 /**
  * @param string $url referrer url to check against base href of this application
  * @param \Symfony\Component\Validator\ExecutionContextInterface $context
  */
 public function hasCorrectBaseHref($url, ExecutionContextInterface $context)
 {
     if (!is_string($url)) {
         $context->addViolation('URL is not a string.');
     } else {
         $base_href = $this->getContext()->getRouting()->getBaseHref();
         if (strpos($url, "{$base_href}", 0) !== 0) {
             $context->addViolation('URL does not start with base href of this application. Same origin violation.');
         }
     }
 }
Пример #12
0
 public function checkStateId($value, ExecutionContextInterface $context)
 {
     if ($value['migrate']) {
         if (null !== ($state = StateQuery::create()->findPk($value['new_state']))) {
             if ($state->getCountryId() !== $value['new_country']) {
                 $context->addViolation(Translator::getInstance()->trans("The state id '%id' does not belong to country id '%id_country'", ['%id' => $value['new_state'], '%id_country' => $value['new_country']]));
             }
         } else {
             $context->addViolation(Translator::getInstance()->trans("The state id '%id' doesn't exist", ['%id' => $value['new_state']]));
         }
     }
 }
Пример #13
0
 public function checkEmails($value, ExecutionContextInterface $context)
 {
     $data = $context->getRoot()->getData();
     $value = trim($value);
     if ("" === trim($value) && !empty($data["enabled"])) {
         $context->addViolation($this->trans("The Emails can not be empty", ["%id" => $value]));
     }
     $emails = explode(',', $value);
     foreach ($emails as $email) {
         if (false === filter_var($email, FILTER_VALIDATE_EMAIL)) {
             $context->addViolation($this->trans("'%email' is not a valid email address", ["%email" => $email]));
         }
     }
 }
Пример #14
0
 public function verifyPasswordField($value, ExecutionContextInterface $context)
 {
     $data = $context->getRoot()->getData();
     if ($data["password"] === '' && $data["password_confirm"] === '') {
         $context->addViolation("password can't be empty");
     }
     if ($data["password"] != $data["password_confirm"]) {
         $context->addViolation("password confirmation is not the same as password field");
     }
     $minLength = ConfigQuery::getMinimuAdminPasswordLength();
     if (strlen($data["password"]) < $minLength) {
         $context->addViolation("password must be composed of at least {$minLength} characters");
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         try {
             /** @var \Drupal\Core\Url $url */
             $url = $value->getUrl();
         } catch (\InvalidArgumentException $e) {
             return;
         }
         // Disallow external URLs using untrusted protocols.
         if ($url->isExternal() && !in_array(parse_url($url->getUri(), PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) {
             $this->context->addViolation($constraint->message, array('@uri' => $value->uri));
         }
     }
 }
 public function verifyState($value, ExecutionContextInterface $context)
 {
     $data = $context->getRoot()->getData();
     if (null !== ($country = CountryQuery::create()->findPk($data['country']))) {
         if ($country->getHasStates()) {
             if (null !== ($state = StateQuery::create()->findPk($data['state']))) {
                 if ($state->getCountryId() !== $country->getId()) {
                     $context->addViolation(Translator::getInstance()->trans("This state doesn't belong to this country."));
                 }
             } else {
                 $context->addViolation(Translator::getInstance()->trans("You should select a state for this country."));
             }
         }
     }
 }
Пример #17
0
 /**
  * {@inheritdoc}
  */
 public function validate($items, Constraint $constraint)
 {
     /** @var \Drupal\Core\Field\FieldItemListInterface $items */
     /** @var \Drupal\user\UserInterface $account */
     $account = $items->getEntity();
     $existing_value = NULL;
     if ($account->id()) {
         $account_unchanged = \Drupal::entityManager()->getStorage('user')->loadUnchanged($account->id());
         $existing_value = $account_unchanged->getEmail();
     }
     $required = !(!$existing_value && \Drupal::currentUser()->hasPermission('administer users'));
     if ($required && (!isset($items) || $items->isEmpty())) {
         $this->context->addViolation($this->message, ['@name' => $account->getFieldDefinition('mail')->getLabel()]);
     }
 }
Пример #18
0
 public function verifyProfileId($value, ExecutionContextInterface $context)
 {
     $profile = ProfileQuery::create()->findPk($value);
     if (null === $profile) {
         $context->addViolation(Translator::getInstance()->trans("Profile ID not found"));
     }
 }
Пример #19
0
 public function verifyExistingEmail($value, ExecutionContextInterface $context)
 {
     $customer = CustomerQuery::getCustomerByEmail($value);
     if ($customer) {
         $context->addViolation(Translator::getInstance()->trans("This email already exists."));
     }
 }
Пример #20
0
 public function verifyTaxId($value, ExecutionContextInterface $context)
 {
     $tax = TaxQuery::create()->findPk($value);
     if (null === $tax) {
         $context->addViolation("Tax ID not found");
     }
 }
Пример #21
0
 public function checkRefDifferent($value, ExecutionContextInterface $context)
 {
     $originalRef = ProductQuery::create()->filterByRef($value, Criteria::EQUAL)->count();
     if ($originalRef !== 0) {
         $context->addViolation($this->translator->trans('This product reference is already assigned to another product.'));
     }
 }
Пример #22
0
 public function verifyExistingEmail($value, ExecutionContextInterface $context)
 {
     $customer = NewsletterQuery::create()->filterByUnsubscribed(false)->findOneByEmail($value);
     if ($customer) {
         $context->addViolation(Translator::getInstance()->trans("You are already registered!"));
     }
 }
Пример #23
0
 /**
  * Validate a date entered with the default Language date format.
  *
  * @param string                    $value
  * @param ExecutionContextInterface $context
  */
 public function checkLocalizedDate($value, ExecutionContextInterface $context)
 {
     $format = LangQuery::create()->findOneByByDefault(true)->getDateFormat();
     if (false === \DateTime::createFromFormat($format, $value)) {
         $context->addViolation(Translator::getInstance()->trans("Date '%date' is invalid, please enter a valid date using %fmt format", ['%fmt' => $format, '%date' => $value]));
     }
 }
Пример #24
0
 public function verifyCountry($value, ExecutionContextInterface $context)
 {
     $address = CountryQuery::create()->findPk($value);
     if (null === $address) {
         $context->addViolation(Translator::getInstance()->trans("Country ID not found"));
     }
 }
Пример #25
0
 public function checkDuplicateCode($value, ExecutionContextInterface $context)
 {
     $currency = CurrencyQuery::create()->findOneByCode($value);
     if ($currency) {
         $context->addViolation(Translator::getInstance()->trans('A currency with code "%name" already exists.', ['%name' => $value]));
     }
 }
Пример #26
0
 public function checkCityName($value, ExecutionContextInterface $context)
 {
     $isValid = InseeGeoMunicipalityQuery::create()->findOneById($value);
     if (!isset($isValid)) {
         $context->addViolation(Translator::getInstance()->trans('city.error', [], INSEEGeo::DOMAIN_NAME));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         $valid_geometry = TRUE;
         try {
             if (!geoPHP::load($value, 'wkt')) {
                 $valid_geometry = FALSE;
             }
         } catch (\Exception $e) {
             $valid_geometry = FALSE;
         }
         if (!$valid_geometry) {
             $this->context->addViolation($this->message, array('@value' => $value));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($value, Constraint $constraint)
 {
     if (isset($value)) {
         try {
             $url = $value->getUrl();
         } catch (\InvalidArgumentException $e) {
             return;
         }
         // Disallow URLs if the current user doesn't have the 'link to any page'
         // permission nor can access this URI.
         $allowed = $this->current_user->hasPermission('link to any page') || $url->access();
         if (!$allowed) {
             $this->context->addViolation($constraint->message, array('@uri' => $value->uri));
         }
     }
 }
Пример #29
0
 public function checkDuplicateName($value, ExecutionContextInterface $context)
 {
     $config = ConfigQuery::create()->findOneByName($value);
     if ($config) {
         $context->addViolation(Translator::getInstance()->trans('A variable with name "%name" already exists.', array('%name' => $value)));
     }
 }
Пример #30
0
 public function verifyExistingEmail($value, ExecutionContextInterface $context)
 {
     $customer = CustomerQuery::create()->findOneByEmail($value);
     if (null === $customer) {
         $context->addViolation(Translator::getInstance()->trans("This email does not exists"));
     }
 }