You are here

function force_password_change_validate_user in Force Password Change 2.0.x

Same name and namespace in other branches
  1. 8 force_password_change.module \force_password_change_validate_user()
  2. 7.2 force_password_change.module \force_password_change_validate_user()
  3. 7 force_password_change.module \force_password_change_validate_user()

This function is called after a user's account page is updated.

1 string reference to 'force_password_change_validate_user'
force_password_change_form_alter in ./force_password_change.module
Implements hook_form_alter().

File

./force_password_change.module, line 238

Code

function force_password_change_validate_user(array &$form, FormStateInterface $form_state) {
  $account = \Drupal::routeMatch()
    ->getParameter('user');
  $current_user = \Drupal::currentUser();

  // Check to see if the user's account has been flagged to change
  // their password, and if so, have they changed it?
  if ($account
    ->id() == $current_user
    ->id() && \Drupal::service('user.data')
    ->get('force_password_change', $account
    ->id(), 'force_password_change')) {
    if (!strlen($form_state
      ->getValue('pass'))) {
      $form_state
        ->setErrorByName('pass', t('You must choose a new password'));
    }
  }

  // Check to see if the new password is different from the old password.
  if (\Drupal::service('user.auth')
    ->authenticate($account
    ->getAccountName(), $form_state
    ->getValue('pass'))) {
    $form_state
      ->setErrorByName('pass', t('You cannot use your current password. Please choose a different password.'));
  }
}