diff --git a/core/modules/user/lib/Drupal/user/Controller/UserController.php b/core/modules/user/lib/Drupal/user/Controller/UserController.php index abd57e9..e782019 100644 --- a/core/modules/user/lib/Drupal/user/Controller/UserController.php +++ b/core/modules/user/lib/Drupal/user/Controller/UserController.php @@ -7,12 +7,10 @@ namespace Drupal\user\Controller; -use Drupal\Core\Controller\ControllerInterface; -use Drupal\user\Form\UserLoginForm; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\HttpKernelInterface; +use Drupal\Core\Controller\ControllerInterface; /** * Controller routines for user routes. @@ -20,51 +18,16 @@ class UserController implements ControllerInterface { /** - * The HTTP Kernel. - * - * @var \Symfony\Component\HttpKernel\HttpKernelInterface + * Constructs an UserController object. */ - protected $httpKernel; - - /** - * Constructs a new UserController. - * - * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel - * The HTTP Kernel. - */ - public function __construct(HttpKernelInterface $http_kernel) { - $this->httpKernel = $http_kernel; + public function __construct() { } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static( - $container->get('http_kernel') - ); - } - - /** - * Returns the user page. - * - * Displays user profile if user is logged in, or login form for anonymous - * users. - * - * @param \Symfony\Component\HttpFoundation\Request $request - * The request object. - * - * @return \Symfony\Component\HttpFoundation\Response - * Returns a subrequest for either the user page or the login form. - */ - public function userPage(Request $request) { - global $user; - $parameters = array(); - if ($request->query->has('destination')) { - $parameters['destination'] = $request->query->get('destination'); - } - $url = ($user->uid) ? '/user/' . $user->uid : '/user/login'; - return $this->httpKernel->handle(Request::create($url, 'GET', $parameters), HttpKernelInterface::MASTER_REQUEST); + return new static(); } /** diff --git a/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php b/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php index 9927b53..4984bff 100644 --- a/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php +++ b/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php @@ -9,10 +9,12 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Controller\ControllerInterface; +use Drupal\Core\Database\Connection; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Form\FormInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\HttpKernelInterface; /** * Provides a user login form. @@ -41,16 +43,36 @@ class UserLoginForm implements FormInterface, ControllerInterface { protected $flood; /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * The HTTP Kernel. + * + * @var \Symfony\Component\HttpKernel\HttpKernelInterface + */ + protected $httpKernel; + + /** * Constructs a new UserLoginForm. * * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory. * @param \Drupal\Core\Flood\FloodInterface $flood * The flood service. + * @param \Drupal\Core\Database\Connection $connection + * The database connection. + * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel + * The HTTP Kernel. */ - public function __construct(ConfigFactory $config_factory, FloodInterface $flood) { + public function __construct(ConfigFactory $config_factory, FloodInterface $flood, Connection $connection, HttpKernelInterface $http_kernel) { $this->configFactory = $config_factory; $this->flood = $flood; + $this->connection = $connection; + $this->httpKernel = $http_kernel; } /** @@ -59,7 +81,9 @@ public function __construct(ConfigFactory $config_factory, FloodInterface $flood public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), - $container->get('flood') + $container->get('flood'), + $container->get('connection'), + $container->get('http_kernel') ); } @@ -74,6 +98,16 @@ public function getFormID() { * {@inheritdoc} */ public function buildForm(array $form, array &$form_state, Request $request = NULL) { + global $user; + // If the user is already logged in, load the user page. + if ($user->uid) { + $parameters = array(); + if ($request->query->has('destination')) { + $parameters['destination'] = $request->query->get('destination'); + } + return $this->httpKernel->handle(Request::create('/user/' . $user->uid, 'GET', $parameters), HttpKernelInterface::MASTER_REQUEST); + } + $this->request = $request; // Display login form: $form['name'] = array( @@ -126,7 +160,7 @@ public function submitForm(array &$form, array &$form_state) { } /** - * A FAPI validate handler. Sets an error if supplied username has been blocked. + * Sets an error if supplied username has been blocked. */ public function validateName(array &$form, array &$form_state) { if (!empty($form_state['values']['name']) && user_is_blocked($form_state['values']['name'])) { @@ -136,9 +170,9 @@ public function validateName(array &$form, array &$form_state) { } /** - * A validate handler on the login form. Check supplied username/password - * against local users table. If successful, $form_state['uid'] - * is set to the matching user ID. + * Checks supplied username/password against local users table. + * + * If successful, $form_state['uid'] is set to the matching user ID. */ public function validateAuthentication(array &$form, array &$form_state) { $password = trim($form_state['values']['pass']); @@ -153,7 +187,7 @@ public function validateAuthentication(array &$form, array &$form_state) { $form_state['flood_control_triggered'] = 'ip'; return; } - $account = db_query("SELECT * FROM {users} WHERE name = :name AND status = 1", array(':name' => $form_state['values']['name']))->fetchObject(); + $account = $this->connection->query("SELECT * FROM {users} WHERE name = :name AND status = 1", array(':name' => $form_state['values']['name']))->fetchObject(); if ($account) { if ($flood_config->get('uid_only')) { // Register flood events based on the uid only, so they apply for any @@ -182,11 +216,9 @@ public function validateAuthentication(array &$form, array &$form_state) { } /** - * The final validation handler on the login form. + * Checks if user was not authenticated, or if too many logins were attempted. * - * Sets a form error if user has not been authenticated, or if too many - * logins have been attempted. This validation function should always - * be the last one. + * This validation function should always be the last one. */ public function validateFinal(array &$form, array &$form_state) { $flood_config = $this->configFactory->get('user.flood'); diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index 7f8e98b..125c7d3 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -78,10 +78,11 @@ user_pass: user_page: pattern: '/user' defaults: - _controller: '\Drupal\user\Controller\UserController::userPage' + _form: '\Drupal\user\Form\UserLoginForm' requirements: _access: 'TRUE' +# Register a duplicate route for /user/login to preserve user expectations. user_login: pattern: '/user/login' defaults: