You are here

function apigee_edge_form_user_register_form_developer_email_validate in Apigee Edge 8

Validates whether the provided email address is already taken on Apigee Edge.

Parameters

array $form: Form array.

Drupal\Core\Form\FormStateInterface $form_state: Form state object.

1 string reference to 'apigee_edge_form_user_register_form_developer_email_validate'
apigee_edge_form_user_register_form_alter in ./apigee_edge.module
Implements hook_form_FORM_ID_alter().

File

./apigee_edge.module, line 885
Copyright 2018 Google Inc.

Code

function apigee_edge_form_user_register_form_developer_email_validate(array $form, FormStateInterface $form_state) {

  // Do nothing if the developer does not exists.
  if (empty($form_state
    ->getValue('developer_exists'))) {
    return;
  }

  /** @var \Drupal\user\RegisterForm $registerForm */
  $registerForm = $form_state
    ->getFormObject();

  // Pass this information to hook_user_presave() in case if we would get
  // there.
  $registerForm
    ->getEntity()->{APIGEE_EDGE_USER_REGISTRATION_SOURCE} = 'user_register_form';

  // Do nothing if user has administer users permission.
  // (Form is probably displayed on admin/people/create.)
  if ($form_state
    ->getValue('administer_users')) {

    // Add email address to the whitelist because we do not want to
    // display the same error message for an admin user as a regular user.
    DeveloperEmailUniqueValidator::whitelist($form_state
      ->getValue('mail'));

    // If administrator has not confirmed that they would like to create a user
    // in Drupal with an existing developer id on Apigee Edge then add a custom
    // error to the field.
    if (empty($form_state
      ->getValue('apigee_edge_developer_exists'))) {
      $form_state
        ->setErrorByName('mail', t('This email address already exists on Apigee Edge.'));
    }
    return;
  }
  $config = \Drupal::config('apigee_edge.developer_settings');
  $request = \Drupal::request();
  $token = $request->query
    ->get($config
    ->get('verification_token'));
  $timestamp = $request->query
    ->get('timestamp');

  /** @var \Drupal\user\UserInterface $account */

  // Build user object from the submitted form values.
  $account = $registerForm
    ->buildEntity($form, $form_state);

  // If required parameters are available in the url.
  if ($token && $timestamp) {

    // If token is (still) valid then account's email to the whitelist of the
    // validator. This way it is not going to throw a validation error for this
    // email this time.
    if (apigee_edge_existing_developer_registration_hash_validate($account, $token, $timestamp)) {
      DeveloperEmailUniqueValidator::whitelist($account
        ->getEmail());
      return;
    }
    else {

      // Let user known that the token in the url has expired.
      // Drupal sends a new verification email.
      $form_state
        ->setErrorByName('mail', t('Registration token expired or invalid. We have sent you a new link.'));
    }
  }

  // Use shared storage to keep track of sent verification emails.
  // Form state's storage can not be used for this purpose because its values
  // are being cleared for every new requests. Private storage is too private
  // in case of anonymous user because every page request creates a new, empty
  // private temp storage.
  $storage = \Drupal::service('tempstore.shared');

  /** @var \Drupal\Core\TempStore\PrivateTempStore $sendNotifications */
  $sendNotifications = $storage
    ->get('apigee_edge_developer_email_verification_sent');

  // Do not send multiple email verifications to the same email address
  // every time when form validation fails with an error.
  if (!$sendNotifications
    ->get($account
    ->getEmail()) || $form_state
    ->getValue('apigee_edge_developer_unreceived_mail')) {

    // Send verification email to the user.
    $result = _apigee_edge_send_developer_email_verification_email($account, $account
      ->getPreferredLangcode());
    try {
      $sendNotifications
        ->set($account
        ->getEmail(), $result);
    } catch (TempStoreException $e) {
      watchdog_exception(__FUNCTION__, $e);
    }
  }
}