1.20.x user.pages.inc user_pass_reset($uid, $timestamp, $hashed_pass, $immediate_login = FALSE)

Menu callback; process one time login link and redirects to the user page on success.

Parameters

int $uid: User ID for the user who would like their password reset.

int $timestamp: Timestamp when the one-time password reset link was generated.

string $hashed_pass: Hashed version of the user's password using user_pass_rehash().

bool $immediate_login: Skip the change password form and just immediately log in.

File

modules/user/user.pages.inc, line 117
User page callback file for the user module.

Code

function user_pass_reset($uid, $timestamp, $hashed_pass, $immediate_login = FALSE) {
  global $user;

  // When processing the one-time login link, we have to make sure that a user
  // isn't already logged in.
  if ($user->uid) {
    // The existing user is already logged in.
    if ($user->uid == $uid) {
      backdrop_set_message(t('You are logged in as %user. <a href="!user_edit">Change your password.</a>', array('%user' => $user->name, '!user_edit' => url("user/$user->uid/edit"))));
    }
    // A different user is already logged in on the computer.
    else {
      $reset_link_account = user_load($uid);
      if (!empty($reset_link_account)) {
        backdrop_set_message(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a reset password link for user %resetting_user. Please <a href="!logout">logout</a> and try using the link again.', 
        array('%other_user' => $user->name, '%resetting_user' => $reset_link_account->name, '!logout' => url('user/logout'))), 'warning');
      }
      else {
        // Invalid one-time link specifies an unknown user.
        backdrop_set_message(t('The one-time login link you clicked is invalid.'), 'error');
      }
    }
    backdrop_goto();
  }
  else {
    // Time out, in seconds, until login URL expires.
    $timeout = config_get('system.core', 'user_password_reset_timeout');
    $current = REQUEST_TIME;
    // Some redundant checks for extra security ?
    $users = user_load_multiple(array($uid), array('status' => '1'));
    if ($timestamp <= $current && $account = reset($users)) {
      // No time out for first time login.
      if ($account->login && $current - $timestamp > $timeout) {
        backdrop_set_message(t('You have tried to use a reset password link that has expired. Please request a new one using the form below.'), 'error');
        backdrop_goto('user/password');
      }
      elseif ($account->uid && $timestamp >= $account->login && $timestamp <= $current && $hashed_pass == user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid)) {
        if ($immediate_login) {
          $user = $account;
          user_login_finalize();
          backdrop_set_message(t('You have used your one-time log-in link and are now logged-in.'));
          watchdog('user', 'User %name used one-time password reset link at time %timestamp.', array('%name' => $account->name, '%timestamp' => format_date(REQUEST_TIME, 'long')));
          backdrop_goto();
        }
        else {
          return backdrop_get_form('user_pass_reset_form', $account);
        }
      }
      else {
        backdrop_set_message(t('You have tried to use a reset password link that has either been used or is no longer valid. Please request a new one using the form below.'), 'error');
        backdrop_goto('user/password');
      }
    }
    else {
      // Deny access, no more clues.
      // Everything will be in the watchdog's URL for the administrator to check.
      backdrop_access_denied();
      backdrop_exit();
    }
  }
}