You are here

function email_confirmer_user_user_presave in Email confirmer 8

Implements hook_ENTITY_TYPE_presave().

File

email_confirmer_user/email_confirmer_user.module, line 17
Users related email confirmation module.

Code

function email_confirmer_user_user_presave(EntityInterface $entity) {

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

  // Do nothing if no user email update, currently in a confirmer operation or
  // email change confirmation is disabled.
  if (!isset($entity->original) || drupal_static('email_confirmer_user_' . $entity
    ->id()) || ($new_email = $entity
    ->getEmail()) == ($old_email = $entity->original
    ->getEmail()) || !($config = \Drupal::config('email_confirmer_user.settings')
    ->get('user_email_change')) || !$config['enabled']) {
    return;
  }

  // Accept email change when updated by an admin user.
  if (\Drupal::currentUser()
    ->hasPermission('email confirmer user bypass email change')) {

    // Remove any pending email change confirmation on this user.
    \Drupal::service('user.data')
      ->delete('email_confirmer_user', $entity
      ->id(), 'email_change_new_address');
    return;
  }

  // Check for previous confirmations for the new email address.
  if ($config['consider_existent'] && !empty($confirmations = \Drupal::service('email_confirmer')
    ->getConfirmations($new_email, 'confirmed', 0, $config['limit_user_realm'] ? 'email_confirmer_user' : ''))) {

    // Limit to user's own confirmations.

    /** @var \Drupal\email_confirmer\EmailConfirmationInterface $confirmation */
    foreach ($confirmations as $confirmation) {
      if ($confirmation->uid->target_id == $entity
        ->id()) {
        return;
      }
    }
  }

  // Save the new email on user data space.
  \Drupal::service('user.data')
    ->set('email_confirmer_user', $entity
    ->id(), 'email_change_new_address', $new_email);

  // Restore original email address.
  $entity
    ->setEmail($old_email);

  // Launch the confirmation for the new email address.
  $confirmation = \Drupal::service('email_confirmer')
    ->createConfirmation($new_email);
  $confirmation
    ->setRealm('email_confirmer_user')
    ->setProperty('user', $entity
    ->id())
    ->setPrivate()
    ->setResponseUrl($entity
    ->toUrl('edit-form'), 'confirm')
    ->sendRequest();
  $confirmation
    ->save();
  \Drupal::messenger()
    ->addStatus(t('A message has been sent to the new email address %email to confirm the change. Until confirmed, your current address will be used.', [
    '%email' => $new_email,
  ]));

  // Send notification to the current email address.
  if ($config['notify_current']) {
    \Drupal::service('plugin.manager.mail')
      ->mail('email_confirmer_user', 'notify_current', mb_substr(PHP_OS, 0, 3) == 'WIN' ? $old_email : '"' . addslashes(Unicode::mimeHeaderEncode(\Drupal::config('system.site')
      ->get('name'))) . '" <' . $old_email . '>', \Drupal::languageManager()
      ->getDefaultLanguage()
      ->getId(), [
      'context' => [
        'email_confirmer_confirmation' => $confirmation,
        'user' => $entity,
      ],
    ]);
  }
}