public function ResetPassAccessCheck::access in Simple Password Reset 8
File
- src/AccessChecks/ResetPassAccessCheck.php, line 18
Class
- ResetPassAccessCheck
- A custom access check.
Namespace
Drupal\simple_pass_reset\AccessChecks
Code
public function access($uid, $timestamp, $hash, AccountInterface $account) {
$user = \Drupal::entityTypeManager()
->getStorage('user')
->load($uid);
$timeout = \Drupal::config('user.settings')
->get('password_reset_timeout');
if ($user === NULL || !$user
->isActive()) {
return AccessResult::forbidden();
}
if ($account
->isAuthenticated()) {
if ($account
->id() != $uid) {
\Drupal::messenger()
->addWarning(t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href=":logout">log out</a> and try using the link again.', [
'%other_user' => $account
->getAccountName(),
'%resetting_user' => $user
->getAccountName(),
':logout' => Url::fromRoute('user.logout')
->toString(),
]));
return AccessResult::forbidden();
}
}
if ($timestamp <= \Drupal::time()
->getRequestTime() && $user) {
if ($user
->getLastLoginTime() && \Drupal::time()
->getRequestTime() - $timestamp > $timeout) {
\Drupal::messenger()
->addError(t('You have tried to use a one-time login link that has expired. Please request a new one using the <a href=":link">link</a>.', [
':link' => Url::fromRoute('user.pass')
->toString(),
]));
return AccessResult::forbidden();
}
elseif ($timestamp >= $user
->getLastLoginTime() && $timestamp <= \Drupal::time()
->getRequestTime() && hash_equals($hash, user_pass_rehash($user, $timestamp))) {
return AccessResult::Allowed();
}
}
return AccessResult::forbidden();
}