diff --git a/apigee_edge.module b/apigee_edge.module index f0a0a18c..6a542c39 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -326,6 +326,7 @@ function apigee_edge_entity_base_field_info_alter(&$fields, EntityTypeInterface $mail = $fields['mail']; $mail->setRequired(TRUE); $mail->addConstraint('DeveloperMailUnique'); + $mail->addConstraint('DeveloperLowercaseEmail'); // Add a bundle to these fields to allow other modules to display them // as configurable (fields added through the UI or configuration do have a @@ -336,6 +337,28 @@ function apigee_edge_entity_base_field_info_alter(&$fields, EntityTypeInterface } } +/** + * Implements hook_form_FORM_ID_alter() for install_configure_form(). + * + * Allows the profile to alter the site configuration form. + */ +function apigee_edge_form_install_configure_form_alter(&$form, FormStateInterface $form_state) { + $form['#validate'][] = '_apigee_edge_site_install_form_check_email'; +} + +/** +* @internal +*/ +function _apigee_edge_site_install_form_check_email(&$form, FormStateInterface $form_state) { + $org_controller = \Drupal::service('apigee_edge.controller.organization'); + // Check if org is ApigeeX. + if ($org_controller->isOrganizationApigeeX()) { + if (preg_match('/[A-Z]/', $form_state->getValue(['account', 'mail']))) { + $form_state->setErrorByName('account][mail', 'This email address accepts only lowercase characters.'); + } + } +} + /** * Implements hook_entity_extra_field_info(). */ diff --git a/src/Plugin/Validation/Constraint/LowercaseEmailConstraint.php b/src/Plugin/Validation/Constraint/LowercaseEmailConstraint.php new file mode 100644 index 00000000..aef5c3c8 --- /dev/null +++ b/src/Plugin/Validation/Constraint/LowercaseEmailConstraint.php @@ -0,0 +1,43 @@ +sdkConnector = $sdk_connector; + $this->orgController = $org_controller; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('apigee_edge.sdk_connector'), + $container->get('apigee_edge.controller.organization') + ); + } + + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) { + try { + if (!isset($this->cache[$this->sdkConnector->getOrganization()])) { + $this->cache[$this->sdkConnector->getOrganization()] = $this->orgController; + } + // Check if organization is ApigeeX. + if ($this->cache[$this->sdkConnector->getOrganization()]->isOrganizationApigeeX()) { + foreach ($value as $item) { + if (preg_match('/[A-Z]/', $item->value)) { + // The value contains uppercase character, the error, is applied. + $this->context->addViolation($constraint->notLowercase, ['%value' => $item->value]); + } + } + } + } + catch (\Exception $e) { + // If not able to connect to Apigee Edge. + \Drupal::logger('apigee_edge')->error($e->getMessage()); + } + } + +}