You are here

function force_password_change_user_login in Force Password Change 2.0.x

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

Implements hook_user_login().

File

./force_password_change.module, line 194

Code

function force_password_change_user_login($account) {

  // Only test if the user's password should be checked when
  // the site's settings require it on login and not on every page load.
  if (\Drupal::config('force_password_change.settings')
    ->get('check_login_only')) {

    // Check to see if the user has a pending password change.
    if (\Drupal::service('force_password_change.service')
      ->checkForForce()) {
      $middleware = \Drupal::service('force_password_change.on_only_login');
      \Drupal::messenger()
        ->addStatus(t('An administrator has required that you change your password. Please change your password to proceed.'));
      $response = new RedirectResponse(Url::fromRoute('entity.user.edit_form', [
        'user' => $account
          ->id(),
      ])
        ->toString());
      $middleware
        ->setRedirectResponse($response);
    }
  }
}