You are here

user_email_verification.module in User email verification 8

Same filename and directory in other branches
  1. 7 user_email_verification.module

User email verification hooks and alters.

File

user_email_verification.module
View source
<?php

/**
 * @file
 * User email verification hooks and alters.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\UserInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see \Drupal\user\AccountSettingsForm
 */
function user_email_verification_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) {
  $config = \Drupal::configFactory()
    ->getEditable('user_email_verification.settings');
  $form['user_email_verification'] = [
    '#type' => 'details',
    '#title' => t('User Email verification'),
    '#open' => TRUE,
    '#weight' => 0,
  ];
  $roles = user_role_names(TRUE);
  unset($roles[UserInterface::AUTHENTICATED_ROLE]);
  $form['user_email_verification']['user_email_verification_skip_roles'] = [
    '#type' => 'checkboxes',
    '#title' => t('Skip roles'),
    '#default_value' => $config
      ->get('skip_roles'),
    '#options' => $roles,
    '#description' => t('Select the roles for which we should not verify the Email address.'),
  ];
  $form['user_email_verification']['user_email_verification_validate_interval'] = [
    '#type' => 'number',
    '#min' => 1,
    '#step' => 1,
    '#title' => t('Verification time interval'),
    '#default_value' => $config
      ->get('validate_interval'),
    '#field_suffix' => t('seconds'),
    '#description' => t('Enter the time interval in seconds in which the user must validate Email.'),
    '#required' => TRUE,
  ];
  $form['user_email_verification']['user_email_verification_num_reminders'] = [
    '#type' => 'select',
    '#title' => t('Send reminder'),
    '#options' => [
      0 => '- never -',
      1 => 'Once',
      2 => 'Twice',
      3 => 'Three times',
    ],
    '#default_value' => $config
      ->get('num_reminders'),
    '#description' => t('Select the number of reminders to be sent spread equally through the time interval in which the user must validate Email.'),
  ];
  $form['user_email_verification']['user_email_verification_mail_subject'] = [
    '#type' => 'textfield',
    '#title' => t('Verification mail subject'),
    '#default_value' => $config
      ->get('mail_subject'),
    '#maxlength' => 180,
    '#description' => t('Subject for Email when user is requesting a new verification link or Verify your Email reminder mail.'),
    '#states' => [
      'visible' => [
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '1',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '2',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '3',
          ],
        ],
      ],
      'required' => [
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '1',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '2',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '3',
          ],
        ],
      ],
    ],
  ];
  $form['user_email_verification']['user_email_verification_mail_body'] = [
    '#type' => 'textarea',
    '#title' => t('Verification mail body'),
    '#default_value' => $config
      ->get('mail_body'),
    '#description' => t('Use [user:verify-email] to display the link to Email verification.'),
    '#rows' => 5,
    '#states' => [
      'visible' => [
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '1',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '2',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '3',
          ],
        ],
      ],
      'required' => [
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '1',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '2',
          ],
        ],
        'or',
        [
          'select[name="user_email_verification_num_reminders"]' => [
            'value' => '3',
          ],
        ],
      ],
    ],
  ];
  $form['user_email_verification']['user_email_verification_extended_enable'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable extended verification period'),
    '#default_value' => $config
      ->get('extended_enable'),
    '#description' => t('Extended verification period allows you to define another time period when the account can be still verified even after being blocked.'),
  ];
  $form['user_email_verification']['extended'] = [
    '#type' => 'details',
    '#title' => t('Extended verification period'),
    '#open' => TRUE,
    '#states' => [
      'visible' => [
        'input[name="user_email_verification_extended_enable"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['user_email_verification']['extended']['user_email_verification_extended_validate_interval'] = [
    '#type' => 'number',
    '#min' => 1,
    '#step' => 1,
    '#title' => t('Extended verification time interval'),
    '#default_value' => $config
      ->get('extended_validate_interval'),
    '#field_suffix' => t('seconds'),
    '#description' => t('Enter the extended time interval in seconds (the time after "Verification time interval") in which the user must validate Email before the account gets deleted completely.'),
    '#states' => [
      'required' => [
        'input[name="user_email_verification_extended_enable"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['user_email_verification']['extended']['user_email_verification_extended_mail_subject'] = [
    '#type' => 'textfield',
    '#title' => t('Mail subject'),
    '#default_value' => $config
      ->get('extended_mail_subject'),
    '#maxlength' => 180,
    '#description' => t('Subject for Email when an account is blocked after not being verified.'),
    '#states' => [
      'required' => [
        'input[name="user_email_verification_extended_enable"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['user_email_verification']['extended']['user_email_verification_extended_mail_body'] = [
    '#type' => 'textarea',
    '#title' => t('Mail body'),
    '#default_value' => $config
      ->get('extended_mail_body'),
    '#rows' => 5,
    '#description' => t('Use [user:verify-email-extended] to display the link to Email verification.'),
    '#states' => [
      'required' => [
        'input[name="user_email_verification_extended_enable"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Add submit handler to save user email verification configuration.
  $form['#submit'][] = 'user_email_verification_form_user_admin_settings_submit';
}

/**
 * Form submission handler for user account settings form.
 *
 * @see user_email_verification_form_user_admin_settings_alter()
 */
function user_email_verification_form_user_admin_settings_submit($form, FormStateInterface $form_state) {
  \Drupal::configFactory()
    ->getEditable('user_email_verification.settings')
    ->set('skip_roles', array_filter($form_state
    ->getValue('user_email_verification_skip_roles')))
    ->set('validate_interval', (int) $form_state
    ->getValue('user_email_verification_validate_interval'))
    ->set('num_reminders', (int) $form_state
    ->getValue('user_email_verification_num_reminders'))
    ->set('mail_subject', $form_state
    ->getValue('user_email_verification_mail_subject'))
    ->set('mail_body', $form_state
    ->getValue('user_email_verification_mail_body'))
    ->set('extended_enable', (bool) $form_state
    ->getValue('user_email_verification_extended_enable'))
    ->set('extended_validate_interval', (int) $form_state
    ->getValue('user_email_verification_extended_validate_interval'))
    ->set('extended_mail_subject', $form_state
    ->getValue('user_email_verification_extended_mail_subject'))
    ->set('extended_mail_body', $form_state
    ->getValue('user_email_verification_extended_mail_body'))
    ->save();
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function user_email_verification_user_insert(UserInterface $user) {
  \Drupal::service('user_email_verification.service')
    ->createVerification($user, \Drupal::currentUser()
    ->hasPermission('administer users'));
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function user_email_verification_user_delete(UserInterface $user) {
  \Drupal::service('user_email_verification.service')
    ->deleteVerification($user);
}

/**
 * Implements hook_cron().
 */
function user_email_verification_cron() {
  \Drupal::service('user_email_verification.service')
    ->cronHandler();
}

/**
 * Implements hook_mail().
 */
function user_email_verification_mail($key, &$message, $params) {
  \Drupal::service('user_email_verification.service')
    ->initEmailMessage($key, $message, $params);
}

/**
 * Implements hook_entity_extra_field_info().
 */
function user_email_verification_entity_extra_field_info() {
  $extra = [];
  $extra['user']['user']['display']['user_email_verification_verified'] = [
    'label' => t('Email verified'),
    'description' => t('Indicator is user email verified or no.'),
    'weight' => 100,
    'visible' => TRUE,
  ];
  $extra['user']['user']['display']['user_email_verification_verified_date'] = [
    'label' => t('Email verification date'),
    'description' => t('Date when user verified his Email.'),
    'weight' => 100,
    'visible' => TRUE,
  ];
  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function user_email_verification_user_view(array &$build, EntityInterface $user, EntityViewDisplayInterface $display, $view_mode) {
  if ($user instanceof UserInterface) {
    $verification = \Drupal::service('user_email_verification.service')
      ->loadVerificationByUserId($user
      ->id());
    $verification = $verification && isset($verification['verified']) ? $verification['verified'] : 0;
    if ($display
      ->getComponent('user_email_verification_verified')) {
      $build['user_email_verification_verified'] = [
        '#type' => 'item',
        '#title' => t('Email verified'),
        '#markup' => '<span class="value">' . ($verification ? t('Yes') : t('No')) . '</span>',
        '#wrapper_attributes' => [
          'class' => [
            'user-email-verification-is-verified',
          ],
        ],
      ];
    }
    if ($display
      ->getComponent('user_email_verification_verified_date') && $verification) {
      $build['user_email_verification_verified_date'] = [
        '#type' => 'item',
        '#title' => t('Email verification date'),
        '#markup' => '<span class="value">' . \Drupal::service('date.formatter')
          ->format($verification, 'long') . '</span>',
        '#wrapper_attributes' => [
          'class' => [
            'user-email-verification-verified-date',
          ],
        ],
      ];
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function user_email_verification_user_presave(UserInterface $user) {

  // Mark user Email verified when admin user activates blocked user account.
  if (\Drupal::currentUser()
    ->hasPermission('administer users') && isset($user->original) && $user->original
    ->isBlocked() && $user
    ->isActive()) {
    \Drupal::service('user_email_verification.service')
      ->setEmailVerifiedByUserId($user
      ->id());
  }
}

/**
 * Implements hook_help().
 */
function user_email_verification_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the user_email_verification module.
    case 'help.page.user_email_verification':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<strong>' . t('Module allows:') . '</strong>';
      $output .= '<ul>';
      $output .= '<li>' . t('to have Email verification') . '</li>';
      $output .= '<li>' . t('to type password on registration') . '</li>';
      $output .= '<li>' . t('to be logged in right after registration') . '</li>';
      $output .= '</ul>';
      $output .= '<p>' . t('If user do not verify the Email in a certain time interval account will be blocked.');
      $output .= '<h3>' . t('Configuration:') . '</h3>';
      $output .= '<ul>';
      $output .= '<li>' . t('Go to: Manage -> Configuration -> Account settings') . '</li>';
      $output .= '<li>' . t('In <strong>Registration and cancellation</strong> section:');
      $output .= '<ul>';
      $output .= '<li>' . t('Set "Visitors" option for "Who can register accounts?"') . '</li>';
      $output .= '<li>' . t('Uncheck "Require email verification when a visitor creates an account"') . '</li>';
      $output .= '</ul></li>';
      $output .= '<li>' . t('In <strong>Emails</strong> section:');
      $output .= '<ul>';
      $output .= '<li>' . t('Add [user:verify-email] to the "Welcome (no approval required)" mail to send to the user the Email verification link') . '</li>';
      $output .= '</ul></li>';
      $output .= '<li>' . t('In User Email verification section:');
      $output .= '<ul>';
      $output .= '<li>' . t('Set "Skip roles" - the roles which shouldn\'t verify the Email') . '</li>';
      $output .= '<li>' . t('Set "Verification time interval" (in seconds) - the time for user to verify the Email, <strong>when this time is over - user account will be blocked</strong>') . '</li>';
      $output .= '<li>' . t('Set "Send reminder" - how many times user will be notified (with Verification mail) during "Verification time interval"');
      $output .= '<ul>';
      $output .= '<li>' . t('Customize "Verification mail subject" and "Verification mail body" if "Send reminder" was set') . '</li>';
      $output .= '</ul></li>';
      $output .= '<li>' . t('Check "Enable extended verification period" if you\'d like to provide an extra time to the user to verify the Email and activate blocked account');
      $output .= '<ul>';
      $output .= '<li>' . t('Set "Extended verification time interval" (in seconds) - the time for user to verify Email and unblock account, <strong>when this time is over - user account will be removed or blocked, depends on "When cancelling a user account" setting</strong>') . '</li>';
      $output .= '<li>' . t('Customize "Mail subject" and "Mail body"') . '</li>';
      $output .= '</ul></li>';
      $output .= '</ul></li>';
      $output .= '<li>' . t('Click "Save configuration" button') . '</li>';
      $output .= '</ul>';
      return $output;
    default:
  }
}