Chris@0: dateFormatter = $date_formatter;
Chris@0: $this->userStorage = $user_storage;
Chris@0: $this->userData = $user_data;
Chris@0: $this->logger = $logger;
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * {@inheritdoc}
Chris@0: */
Chris@0: public static function create(ContainerInterface $container) {
Chris@0: return new static(
Chris@0: $container->get('date.formatter'),
Chris@0: $container->get('entity.manager')->getStorage('user'),
Chris@0: $container->get('user.data'),
Chris@0: $container->get('logger.factory')->get('user')
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Redirects to the user password reset form.
Chris@0: *
Chris@0: * In order to never disclose a reset link via a referrer header this
Chris@0: * controller must always return a redirect response.
Chris@0: *
Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0: * The request.
Chris@0: * @param int $uid
Chris@0: * User ID of the user requesting reset.
Chris@0: * @param int $timestamp
Chris@0: * The current timestamp.
Chris@0: * @param string $hash
Chris@0: * Login link hash.
Chris@0: *
Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse
Chris@0: * The redirect response.
Chris@0: */
Chris@0: public function resetPass(Request $request, $uid, $timestamp, $hash) {
Chris@0: $account = $this->currentUser();
Chris@0: // When processing the one-time login link, we have to make sure that a user
Chris@0: // isn't already logged in.
Chris@0: if ($account->isAuthenticated()) {
Chris@0: // The current user is already logged in.
Chris@0: if ($account->id() == $uid) {
Chris@0: user_logout();
Chris@0: // We need to begin the redirect process again because logging out will
Chris@0: // destroy the session.
Chris@0: return $this->redirect(
Chris@0: 'user.reset',
Chris@0: [
Chris@0: 'uid' => $uid,
Chris@0: 'timestamp' => $timestamp,
Chris@0: 'hash' => $hash,
Chris@0: ]
Chris@0: );
Chris@0: }
Chris@0: // A different user is already logged in on the computer.
Chris@0: else {
Chris@0: /** @var \Drupal\user\UserInterface $reset_link_user */
Chris@0: if ($reset_link_user = $this->userStorage->load($uid)) {
Chris@17: $this->messenger()
Chris@17: ->addWarning($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 log out and try using the link again.',
Chris@17: [
Chris@18: '%other_user' => $account->getAccountName(),
Chris@18: '%resetting_user' => $reset_link_user->getAccountName(),
Chris@17: ':logout' => $this->url('user.logout'),
Chris@17: ]));
Chris@0: }
Chris@0: else {
Chris@0: // Invalid one-time link specifies an unknown user.
Chris@17: $this->messenger()->addError($this->t('The one-time login link you clicked is invalid.'));
Chris@0: }
Chris@0: return $this->redirect('');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: $session = $request->getSession();
Chris@0: $session->set('pass_reset_hash', $hash);
Chris@0: $session->set('pass_reset_timeout', $timestamp);
Chris@0: return $this->redirect(
Chris@0: 'user.reset.form',
Chris@0: ['uid' => $uid]
Chris@0: );
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Returns the user password reset form.
Chris@0: *
Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0: * The request.
Chris@0: * @param int $uid
Chris@0: * User ID of the user requesting reset.
Chris@0: *
Chris@0: * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
Chris@0: * The form structure or a redirect response.
Chris@0: *
Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
Chris@0: * If the pass_reset_timeout or pass_reset_hash are not available in the
Chris@0: * session. Or if $uid is for a blocked user or invalid user ID.
Chris@0: */
Chris@0: public function getResetPassForm(Request $request, $uid) {
Chris@0: $session = $request->getSession();
Chris@0: $timestamp = $session->get('pass_reset_timeout');
Chris@0: $hash = $session->get('pass_reset_hash');
Chris@0: // As soon as the session variables are used they are removed to prevent the
Chris@0: // hash and timestamp from being leaked unexpectedly. This could occur if
Chris@0: // the user does not click on the log in button on the form.
Chris@0: $session->remove('pass_reset_timeout');
Chris@0: $session->remove('pass_reset_hash');
Chris@0: if (!$hash || !$timestamp) {
Chris@0: throw new AccessDeniedHttpException();
Chris@0: }
Chris@0:
Chris@0: /** @var \Drupal\user\UserInterface $user */
Chris@0: $user = $this->userStorage->load($uid);
Chris@0: if ($user === NULL || !$user->isActive()) {
Chris@0: // Blocked or invalid user ID, so deny access. The parameters will be in
Chris@0: // the watchdog's URL for the administrator to check.
Chris@0: throw new AccessDeniedHttpException();
Chris@0: }
Chris@0:
Chris@0: // Time out, in seconds, until login URL expires.
Chris@0: $timeout = $this->config('user.settings')->get('password_reset_timeout');
Chris@0:
Chris@0: $expiration_date = $user->getLastLoginTime() ? $this->dateFormatter->format($timestamp + $timeout) : NULL;
Chris@0: return $this->formBuilder()->getForm(UserPasswordResetForm::class, $user, $expiration_date, $timestamp, $hash);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Validates user, hash, and timestamp; logs the user in if correct.
Chris@0: *
Chris@0: * @param int $uid
Chris@0: * User ID of the user requesting reset.
Chris@0: * @param int $timestamp
Chris@0: * The current timestamp.
Chris@0: * @param string $hash
Chris@0: * Login link hash.
Chris@0: *
Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse
Chris@0: * Returns a redirect to the user edit form if the information is correct.
Chris@0: * If the information is incorrect redirects to 'user.pass' route with a
Chris@0: * message for the user.
Chris@0: *
Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
Chris@0: * If $uid is for a blocked user or invalid user ID.
Chris@0: */
Chris@0: public function resetPassLogin($uid, $timestamp, $hash) {
Chris@0: // The current user is not logged in, so check the parameters.
Chris@0: $current = REQUEST_TIME;
Chris@0: /** @var \Drupal\user\UserInterface $user */
Chris@0: $user = $this->userStorage->load($uid);
Chris@0:
Chris@0: // Verify that the user exists and is active.
Chris@0: if ($user === NULL || !$user->isActive()) {
Chris@0: // Blocked or invalid user ID, so deny access. The parameters will be in
Chris@0: // the watchdog's URL for the administrator to check.
Chris@0: throw new AccessDeniedHttpException();
Chris@0: }
Chris@0:
Chris@0: // Time out, in seconds, until login URL expires.
Chris@0: $timeout = $this->config('user.settings')->get('password_reset_timeout');
Chris@0: // No time out for first time login.
Chris@0: if ($user->getLastLoginTime() && $current - $timestamp > $timeout) {
Chris@17: $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.'));
Chris@0: return $this->redirect('user.pass');
Chris@0: }
Chris@0: elseif ($user->isAuthenticated() && ($timestamp >= $user->getLastLoginTime()) && ($timestamp <= $current) && Crypt::hashEquals($hash, user_pass_rehash($user, $timestamp))) {
Chris@0: user_login_finalize($user);
Chris@0: $this->logger->notice('User %name used one-time login link at time %timestamp.', ['%name' => $user->getDisplayName(), '%timestamp' => $timestamp]);
Chris@17: $this->messenger()->addStatus($this->t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
Chris@0: // Let the user's password be changed without the current password
Chris@0: // check.
Chris@0: $token = Crypt::randomBytesBase64(55);
Chris@0: $_SESSION['pass_reset_' . $user->id()] = $token;
Chris@0: return $this->redirect(
Chris@0: 'entity.user.edit_form',
Chris@0: ['user' => $user->id()],
Chris@0: [
Chris@0: 'query' => ['pass-reset-token' => $token],
Chris@0: 'absolute' => TRUE,
Chris@0: ]
Chris@0: );
Chris@0: }
Chris@0:
Chris@17: $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.'));
Chris@0: return $this->redirect('user.pass');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Redirects users to their profile page.
Chris@0: *
Chris@0: * This controller assumes that it is only invoked for authenticated users.
Chris@0: * This is enforced for the 'user.page' route with the '_user_is_logged_in'
Chris@0: * requirement.
Chris@0: *
Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse
Chris@0: * Returns a redirect to the profile of the currently logged in user.
Chris@0: */
Chris@0: public function userPage() {
Chris@0: return $this->redirect('entity.user.canonical', ['user' => $this->currentUser()->id()]);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Route title callback.
Chris@0: *
Chris@0: * @param \Drupal\user\UserInterface $user
Chris@0: * The user account.
Chris@0: *
Chris@0: * @return string|array
Chris@0: * The user account name as a render array or an empty string if $user is
Chris@0: * NULL.
Chris@0: */
Chris@0: public function userTitle(UserInterface $user = NULL) {
Chris@17: return $user ? ['#markup' => $user->getDisplayName(), '#allowed_tags' => Xss::getHtmlTagList()] : '';
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Logs the current user out.
Chris@0: *
Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse
Chris@0: * A redirection to home page.
Chris@0: */
Chris@0: public function logout() {
Chris@0: user_logout();
Chris@0: return $this->redirect('');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Confirms cancelling a user account via an email link.
Chris@0: *
Chris@0: * @param \Drupal\user\UserInterface $user
Chris@0: * The user account.
Chris@0: * @param int $timestamp
Chris@0: * The timestamp.
Chris@0: * @param string $hashed_pass
Chris@0: * The hashed password.
Chris@0: *
Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse
Chris@0: * A redirect response.
Chris@0: */
Chris@0: public function confirmCancel(UserInterface $user, $timestamp = 0, $hashed_pass = '') {
Chris@0: // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
Chris@0: $timeout = 86400;
Chris@0: $current = REQUEST_TIME;
Chris@0:
Chris@0: // Basic validation of arguments.
Chris@0: $account_data = $this->userData->get('user', $user->id());
Chris@0: if (isset($account_data['cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
Chris@0: // Validate expiration and hashed password/login.
Chris@0: if ($timestamp <= $current && $current - $timestamp < $timeout && $user->id() && $timestamp >= $user->getLastLoginTime() && Crypt::hashEquals($hashed_pass, user_pass_rehash($user, $timestamp))) {
Chris@0: $edit = [
Chris@0: 'user_cancel_notify' => isset($account_data['cancel_notify']) ? $account_data['cancel_notify'] : $this->config('user.settings')->get('notify.status_canceled'),
Chris@0: ];
Chris@0: user_cancel($edit, $user->id(), $account_data['cancel_method']);
Chris@0: // Since user_cancel() is not invoked via Form API, batch processing
Chris@0: // needs to be invoked manually and should redirect to the front page
Chris@0: // after completion.
Chris@16: return batch_process('');
Chris@0: }
Chris@0: else {
Chris@17: $this->messenger()->addError($this->t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'));
Chris@0: return $this->redirect('entity.user.cancel_form', ['user' => $user->id()], ['absolute' => TRUE]);
Chris@0: }
Chris@0: }
Chris@0: throw new AccessDeniedHttpException();
Chris@0: }
Chris@0:
Chris@0: }