diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index cbb8295..d4a4eed 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -93,10 +93,9 @@ public function authenticate(Request $request) { // If authentication is triggered after routing, restrict the allowed // providers to the ones specified in the routes _auth option. // - // @todo: Currently it is necessary to replicate the behavior of - // AuthenticationEnhancer here, and restrict allowed providers to the - // default one, if none was specified on the _auth option. This restriction - // will be removed by https://www.drupal.org/node/2286971 + // @todo Duplicates AuthenticationEnhancer by reducing providers to the + // default one if no _auth was specified. This restriction will be removed + // by https://www.drupal.org/node/2286971 if ($request->attributes->has(RouteObjectInterface::ROUTE_OBJECT)) { $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT); $allowed_providers = $route->getOption('_auth') ? $route->getOption('_auth') : array($this->defaultProviderId()); diff --git a/core/modules/user/src/EventSubscriber/ExceptionRedirectSubscriber.php b/core/modules/user/src/EventSubscriber/ExceptionRedirectSubscriber.php index 11a46f3..d1c7154 100644 --- a/core/modules/user/src/EventSubscriber/ExceptionRedirectSubscriber.php +++ b/core/modules/user/src/EventSubscriber/ExceptionRedirectSubscriber.php @@ -8,8 +8,8 @@ namespace Drupal\user\EventSubscriber; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\UrlGeneratorInterface; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; @@ -55,22 +55,37 @@ public function __construct(AccountInterface $account, URLGeneratorInterface $ur public function onException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if ($exception instanceof AccessDeniedHttpException && $this->account->isAuthenticated()) { - $request = $event->getRequest(); - $route_name = $request->attributes->get(RouteObjectInterface::ROUTE_NAME); - if ($route_name == 'user.login') { - // If user is logged in, redirect to 'user' instead of giving 403. - $event->setResponse(new RedirectResponse($this->urlGenerator->generateFromPath('user', array('absolute' => TRUE)))); - return; - } - if ($route_name == 'user.register') { - // Authenticated user should be redirected to user edit page. - $event->setResponse(new RedirectResponse($this->urlGenerator->generateFromPath('user/' . $this->account->id() . '/edit', array('absolute' => TRUE)))); - return; + $route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName(); + switch ($route_name) { + case 'user.login'; + // If user is logged in, redirect to 'user' instead of giving 403. + $event->setResponse(new RedirectResponse($this->url('user.view', array('user' => $this->account->id()), array('absolute' => TRUE)))); + break; + + case 'user.register'; + // Authenticated user should be redirected to user edit page. + $event->setResponse(new RedirectResponse($this->url('user.edit', array('user' => $this->account->id()), array('absolute' => TRUE)))); + break; } } } /** + * Generates a URL or path for a specific route based on the given parameters. + * + * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for + * details on the arguments, usage, and possible exceptions. + * + * @return string + * The generated URL for the given route. + * + * @todo: Use UrlGeneratorTrait (see https://www.drupal.org/node/2282161) + */ + protected function url($route_name, $route_parameters = array(), $options = array()) { + return $this->urlGenerator->generateFromRoute($route_name, $route_parameters, $options); + } + + /** * {@inheritdoc} */ public static function getSubscribedEvents() {