Verified Commit 1f780ee5 authored by godotislate's avatar godotislate
Browse files

task: #3593667 Convert user.module routes to attributes

By: longwave
By: smustgrave
By: godotislate
(cherry picked from commit 29066acd)
parent ef59f268
Loading
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;

@@ -165,6 +166,15 @@ public static function create(ContainerInterface $container) {
   * @return \Symfony\Component\HttpFoundation\Response
   *   A response which contains the ID and CSRF token.
   */
  #[Route(
    path: '/user/login',
    name: 'user.login.http',
    methods: ['POST'],
    requirements: [
      '_user_is_logged_in' => 'FALSE',
      '_format' => 'json',
    ],
  )]
  public function login(Request $request) {
    $format = $this->getRequestFormat($request);

@@ -250,6 +260,15 @@ public function login(Request $request) {
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  #[Route(
    path: '/user/password',
    name: 'user.pass.http',
    methods: ['POST'],
    requirements: [
      '_access' => 'TRUE',
      '_format' => 'json',
    ],
  )]
  public function resetPassword(Request $request) {
    $format = $this->getRequestFormat($request);

@@ -337,6 +356,16 @@ protected function userLoginFinalize(UserInterface $user) {
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  #[Route(
    path: '/user/logout',
    name: 'user.logout.http',
    methods: ['POST'],
    requirements: [
      '_user_is_logged_in' => 'TRUE',
      '_format' => 'json',
      '_csrf_token' => 'TRUE',
    ],
  )]
  public function logout() {
    $this->userLogout();
    return new Response(NULL, 204);
@@ -355,6 +384,15 @@ protected function userLogout() {
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response.
   */
  #[Route(
    path: '/user/login_status',
    name: 'user.login_status.http',
    methods: ['GET'],
    requirements: [
      '_access' => 'TRUE',
      '_format' => 'json',
    ],
  )]
  public function loginStatus() {
    if ($this->currentUser()->isAuthenticated()) {
      $response = new Response(self::LOGGED_IN);
+71 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\user\Form\UserPasswordResetForm;
use Drupal\user\OneTimeAuthentication;
@@ -19,6 +20,7 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Attribute\Route;

/**
 * Controller routines for user routes.
@@ -118,6 +120,16 @@ public static function create(ContainerInterface $container) {
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   The redirect response.
   */
  #[Route(
    path: '/user/reset/{uid}/{timestamp}/{hash}',
    name: 'user.reset',
    requirements: ['_access' => 'TRUE'],
    options: [
      '_maintenance_access' => TRUE,
      'no_cache' => TRUE,
    ],
    defaults: ['_title' => new TranslatableMarkup('Reset password')],
  )]
  public function resetPass(Request $request, $uid, $timestamp, $hash) {
    $account = $this->currentUser();
    // When processing the one-time login link, we have to make sure that a user
@@ -188,6 +200,16 @@ public function resetPass(Request $request, $uid, $timestamp, $hash) {
   *   If the pass_reset_timeout or pass_reset_hash are not available in the
   *   session. Or if $uid is for a blocked user or invalid user ID.
   */
  #[Route(
    path: '/user/reset/{uid}',
    name: 'user.reset.form',
    requirements: ['_user_is_logged_in' => 'FALSE'],
    options: [
      '_maintenance_access' => TRUE,
      'no_cache' => TRUE,
    ],
    defaults: ['_title' => new TranslatableMarkup('Reset password')],
  )]
  public function getResetPassForm(Request $request, $uid) {
    $session = $request->getSession();
    $timestamp = $session->get('pass_reset_timeout');
@@ -236,6 +258,16 @@ public function getResetPassForm(Request $request, $uid) {
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   If $uid is for a blocked user or invalid user ID.
   */
  #[Route(
    path: '/user/reset/{uid}/{timestamp}/{hash}/login',
    name: 'user.reset.login',
    requirements: ['_user_is_logged_in' => 'FALSE'],
    options: [
      '_maintenance_access' => TRUE,
      'no_cache' => TRUE,
    ],
    defaults: ['_title' => new TranslatableMarkup('Reset password')],
  )]
  public function resetPassLogin($uid, $timestamp, $hash, Request $request) {
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->userStorage->load($uid);
@@ -357,6 +389,12 @@ protected function validatePathParameters(UserInterface $user, int $timestamp, s
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Returns a redirect to the profile of the currently logged in user.
   */
  #[Route(
    path: '/user',
    name: 'user.page',
    requirements: ['_user_is_logged_in' => 'TRUE'],
    defaults: ['_title' => new TranslatableMarkup('My account')],
  )]
  public function userPage() {
    return $this->redirect('entity.user.canonical', ['user' => $this->currentUser()->id()]);
  }
@@ -371,6 +409,17 @@ public function userPage() {
   *   Returns a redirect to the profile edit form of the currently logged in
   *   user.
   */
  #[Route(
    path: '/user/edit',
    name: 'user.edit',
    requirements: ['_user_is_logged_in' => 'TRUE'],
    defaults: ['_title' => new TranslatableMarkup('Edit account')],
  )]
  #[Route(
    path: '/.well-known/change-password',
    name: 'user.well-known.change_password',
    requirements: ['_user_is_logged_in' => 'TRUE'],
  )]
  public function userEditPage() {
    return $this->redirect('entity.user.edit_form', ['user' => $this->currentUser()->id()], [], 302);
  }
@@ -395,6 +444,15 @@ public function userTitle(?UserInterface $user = NULL) {
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirection to home page.
   */
  #[Route(
    path: '/user/logout',
    name: 'user.logout',
    requirements: [
      '_user_is_logged_in' => 'TRUE',
      '_csrf_token' => 'TRUE',
    ],
    options: ['_csrf_confirm_form_route' => 'user.logout.confirm'],
  )]
  public function logout() {
    if ($this->currentUser()->isAuthenticated()) {
      user_logout();
@@ -415,6 +473,19 @@ public function logout() {
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response.
   */
  #[Route(
    path: '/user/{user}/cancel/confirm/{timestamp}/{hashed_pass}',
    name: 'user.cancel_confirm',
    requirements: [
      '_entity_access' => 'user.delete',
      'user' => '\d+',
    ],
    defaults: [
      '_title' => new TranslatableMarkup('Confirm account cancellation'),
      'timestamp' => 0,
      'hashed_pass' => '',
    ],
  )]
  public function confirmCancel(UserInterface $user, $timestamp = 0, $hashed_pass = '') {
    // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
    $timeout = 86400;
+12 −0
Original line number Diff line number Diff line
@@ -4,13 +4,25 @@

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\RoleInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Attribute\Route;

/**
 * Configure administrator role settings for this site.
 */
#[Route(
  path: '/admin/people/role-settings',
  name: 'user.role.settings',
  requirements: [
    '_permission' => 'administer permissions',
  ],
  defaults: [
    '_title' => new TranslatableMarkup('Role settings'),
  ],
)]
class RoleSettingsForm extends FormBase {

  /**
+15 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Core\Form\WorkspaceSafeFormInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Render\BareHtmlPageRendererInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\user\UserAuthenticationInterface;
use Drupal\user\UserAuthInterface;
@@ -14,12 +15,26 @@
use Drupal\user\UserStorageInterface;
use Drupal\user\UserFloodControlInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Attribute\Route;

/**
 * Provides a user login form.
 *
 * @internal
 */
#[Route(
  path: '/user/login',
  name: 'user.login',
  requirements: [
    '_user_is_logged_in' => 'FALSE',
  ],
  options: [
    '_maintenance_access' => TRUE,
  ],
  defaults: [
    '_title' => new TranslatableMarkup('Log in'),
  ],
)]
class UserLoginForm extends FormBase implements WorkspaceSafeFormInterface {

  /**
+8 −0
Original line number Diff line number Diff line
@@ -9,10 +9,18 @@
use Drupal\Core\Form\WorkspaceSafeFormInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Symfony\Component\Routing\Attribute\Route;

/**
 * Provides a confirmation form for user logout.
 */
#[Route(
  path: '/user/logout/confirm',
  name: 'user.logout.confirm',
  requirements: [
    '_user_is_logged_in' => 'TRUE',
  ],
)]
class UserLogoutConfirm extends ConfirmFormBase implements WorkspaceSafeFormInterface {

  /**
Loading