Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding validation for email-id field #1050

Merged
merged 16 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions apigee_edge.module
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -336,6 +337,31 @@ 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_checkemail';
}

function apigee_edge_checkemail(&$form, FormStateInterface $form_state) {
kedarkhaire marked this conversation as resolved.
Show resolved Hide resolved
try {
$org_controller = \Drupal::service('apigee_edge.controller.organization');
// Check if org is ApigeeX.
if ($org_controller->isOrganizationApigeeX()) {
kedarkhaire marked this conversation as resolved.
Show resolved Hide resolved
if (preg_match('/[A-Z]/', $form_state->getValue(['account', 'mail']))) {
$form_state->setErrorByName('account][mail', 'This email address accepts only lowercase characters.');
}
}
}
catch (\Exception $exception) {
// If not able to connect to Apigee Edge.
Drupal::logger('apigee_edge')->error($exception->getMessage());
}
}

/**
* Implements hook_entity_extra_field_info().
*/
Expand Down
2 changes: 1 addition & 1 deletion src/CliService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function createEdgeRoleForDrupal(
string $password,
?string $base_url,
?string $role_name,
?bool $force
?bool $force,
kedarkhaire marked this conversation as resolved.
Show resolved Hide resolved
) {
$this->apigeeEdgeManagementCliService->createEdgeRoleForDrupal(
$io,
Expand Down
2 changes: 1 addition & 1 deletion src/CliServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function createEdgeRoleForDrupal(
string $password,
?string $base_url,
?string $role_name,
?bool $force
?bool $force,
);

}
18 changes: 10 additions & 8 deletions src/Command/Util/ApigeeEdgeManagementCliService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ public function __construct(ClientInterface $http_client) {
/**
* {@inheritdoc}
*/
public function createEdgeRoleForDrupal(StyleInterface $io,
callable $t,
string $org,
string $email,
string $password,
?string $base_url,
?string $role_name,
bool $force) {
public function createEdgeRoleForDrupal(
StyleInterface $io,
callable $t,
string $org,
string $email,
string $password,
?string $base_url,
?string $role_name,
bool $force,
) {

// Set default base URL if var is null or empty string.
if (empty($base_url)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function createEdgeRoleForDrupal(
string $password,
?string $base_url,
?string $role_name,
bool $force
bool $force,
);

}
3 changes: 2 additions & 1 deletion src/Commands/ApigeeEdgeCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public function createEdgeRole(
'base-url' => NULL,
'role-name' => NULL,
'force' => FALSE,
]) {
],
) {

// Call the CLI Service.
$this->cliService->createEdgeRoleForDrupal(
Expand Down
43 changes: 43 additions & 0 deletions src/Plugin/Validation/Constraint/LowercaseEmailConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright 2024 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/

namespace Drupal\apigee_edge\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;

/**
* Checks that the submitted value is a unique integer.
*
* @Constraint(
* id = "DeveloperLowercaseEmail",
* label = @Translation("Developer Lowercase Email", context = "Validation"),
* type = "email"
* )
*/
class LowercaseEmailConstraint extends Constraint {

/**
* The message that will be shown if the value contains any uppercase characters.
*
* @var string
*/
public $notLowercase = 'This email address accepts only lowercase characters.';

}
103 changes: 103 additions & 0 deletions src/Plugin/Validation/Constraint/LowercaseEmailConstraintValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* Copyright 2024 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/

namespace Drupal\apigee_edge\Plugin\Validation\Constraint;

use Drupal\apigee_edge\Entity\Controller\OrganizationController;
use Drupal\apigee_edge\SDKConnectorInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
* Validates the LowercaseEmail constraint.
*/
class LowercaseEmailConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

/**
* The internal entity cache.
*
* @var \Apigee\Edge\Api\Management\Entity\OrganizationInterface[]
*/
private $cache = [];

/**
* The organization controller service.
*
* @var \Drupal\apigee_edge\Entity\Controller\OrganizationController
*/
private $orgController;

/**
* The sdk comnector service.
*
* @var \Drupal\apigee_edge\SDKConnectorInterface
*/
private $sdkConnector;

/**
* Constructs a ValidReferenceConstraintValidator object.
*
* @param \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector
* The SDK connector service.
* @param \Drupal\apigee_edge\Entity\Controller\OrganizationController $org_controller
* The organization controller service.
*/
public function __construct(SDKConnectorInterface $sdk_connector, OrganizationController $org_controller) {
$this->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());
}
}

}
Loading