ChangePwdPageController.php in Password Separate Form 8
File
src/Controller/ChangePwdPageController.php
View source
<?php
namespace Drupal\change_pwd_page\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\user\UserDataInterface;
use Drupal\user\UserStorageInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\Component\Datetime\TimeInterface;
class ChangePwdPageController extends ControllerBase {
protected $dateFormatter;
protected $userStorage;
protected $userData;
protected $time;
public function __construct(DateFormatterInterface $date_formatter, UserStorageInterface $user_storage, UserDataInterface $user_data, TimeInterface $time) {
$this->dateFormatter = $date_formatter;
$this->userStorage = $user_storage;
$this->userData = $user_data;
$this->time = $time;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('entity_type.manager')
->getStorage('user'), $container
->get('user.data'), $container
->get('datetime.time'));
}
public function changePass() {
return $this
->redirect('change_pwd_page.change_password_form', [
'user' => $this
->currentUser()
->id(),
]);
}
public function resetPass($uid, $timestamp, $hash) {
$account = $this
->currentUser();
$config = $this
->config('user.settings');
if ($account
->isAuthenticated()) {
if ($account
->id() == $uid) {
user_logout();
}
else {
if ($reset_link_user = $this->userStorage
->load($uid)) {
$this
->messenger()
->addMessage($this
->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
->getDisplayName(),
'%resetting_user' => $reset_link_user
->getDisplayName(),
':logout' => Url::fromRoute('user.logout')
->toString(),
]), 'warning');
}
else {
$this
->messenger()
->addError($this
->t('The one-time login link you clicked is invalid.'));
}
return $this
->redirect('<front>');
}
}
$timeout = $config
->get('password_reset_timeout');
$current = $this->time
->getRequestTime();
$user = $this->userStorage
->load($uid);
if ($user && $user
->isActive()) {
if ($user
->getLastLoginTime() && $current - $timestamp > $timeout) {
$this
->messenger()
->addError($this
->t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'));
return $this
->redirect('user.pass');
}
elseif ($user
->isAuthenticated() && $timestamp >= $user
->getLastLoginTime() && $timestamp <= $current && hash_equals($hash, user_pass_rehash($user, $timestamp))) {
$expiration_date = $user
->getLastLoginTime() ? $this->dateFormatter
->format($timestamp + $timeout) : NULL;
return $this
->formBuilder()
->getForm('Drupal\\change_pwd_page\\Form\\ChangePasswordResetForm', $user, $expiration_date, $timestamp, $hash);
}
else {
$this
->messenger()
->addError($this
->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'));
return $this
->redirect('user.pass');
}
}
throw new AccessDeniedHttpException();
}
}