diff --git a/core/core.services.yml b/core/core.services.yml index f23086e..03bca22 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -495,7 +495,7 @@ services: arguments: ['@url_generator', '@module_handler'] router: class: Drupal\Core\Routing\AccessAwareRouter - arguments: ['@router.no_access_checks', '@access_manager', '@current_user'] + arguments: ['@router.no_access_checks', '@access_manager', '@authentication', '@current_user'] router.dynamic: class: Symfony\Cmf\Component\Routing\DynamicRouter arguments: ['@router.request_context', '@router.matcher', '@url_generator'] diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php index 3a6a705..ec4ca05 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @@ -51,18 +51,6 @@ public function __construct(AuthenticationProviderInterface $authentication_prov } /** - * Authenticates user on request. - * - * @see \Drupal\Core\Authentication\AuthenticationProviderInterface::authenticate() - */ - public function onKernelRequestAuthenticate(GetResponseEvent $event) { - if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { - $request = $event->getRequest(); - $this->currentUser->setAccount($this->authenticationProvider->authenticate($request)); - } - } - - /** * Triggers authentication clean up on response. * * @see \Drupal\Core\Authentication\AuthenticationProviderInterface::cleanup() @@ -89,8 +77,6 @@ public function onException(GetResponseForExceptionEvent $event) { * {@inheritdoc} */ public static function getSubscribedEvents() { - // Authentication needs to be performed before the access check. - $events[KernelEvents::REQUEST][] = ['onKernelRequestAuthenticate', 31]; $events[KernelEvents::RESPONSE][] = ['onRespond', 0]; $events[KernelEvents::EXCEPTION][] = ['onException', 75]; return $events; diff --git a/core/lib/Drupal/Core/Routing/AccessAwareRouter.php b/core/lib/Drupal/Core/Routing/AccessAwareRouter.php index 1cf937c..1091736 100644 --- a/core/lib/Drupal/Core/Routing/AccessAwareRouter.php +++ b/core/lib/Drupal/Core/Routing/AccessAwareRouter.php @@ -8,7 +8,8 @@ namespace Drupal\Core\Routing; use Drupal\Core\Access\AccessManagerInterface; -use Drupal\Core\Session\AccountInterface; +use Drupal\Core\Authentication\AuthenticationProviderInterface; +use Drupal\Core\Session\AccountProxyInterface; use Symfony\Cmf\Component\Routing\ChainRouter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; @@ -34,11 +35,18 @@ class AccessAwareRouter implements AccessAwareRouterInterface { protected $accessManager; /** - * The account to use in access checks. + * Authentication provider. * - * @var \Drupal\Core\Session\AccountInterface; + * @var AuthenticationProviderInterface */ - protected $account; + protected $authenticationProvider; + + /** + * The account proxy to authenticate and to use in access checks. + * + * @var \Drupal\Core\Session\AccountProxyInterface; + */ + protected $accountProxy; /** * Constructs a router for Drupal with access check and upcasting. @@ -47,13 +55,16 @@ class AccessAwareRouter implements AccessAwareRouterInterface { * The chain router doing the actual routing. * @param \Drupal\Core\Access\AccessManagerInterface $access_manager * The access manager. - * @param \Drupal\Core\Session\AccountInterface $account - * The account to use in access checks. + * @param AuthenticationProviderInterface $authentication_provider + * The authentication provider. + * @param \Drupal\Core\Session\AccountProxyInterface $account_proxy + * The account proxy to authenticate and to use in access checks. */ - public function __construct(ChainRouter $chain_router, AccessManagerInterface $access_manager, AccountInterface $account) { + public function __construct(ChainRouter $chain_router, AccessManagerInterface $access_manager, AuthenticationProviderInterface $authentication_provider, AccountProxyInterface $account_proxy) { $this->chainRouter = $chain_router; $this->accessManager = $access_manager; - $this->account = $account; + $this->authenticationProvider = $authentication_provider; + $this->accountProxy = $account_proxy; } /** @@ -88,6 +99,10 @@ public function getContext() { public function matchRequest(Request $request) { $parameters = $this->chainRouter->matchRequest($request); $request->attributes->add($parameters); + + // Authenticate the user. + $this->accountProxy->setAccount($this->authenticationProvider->authenticate($request)); + $this->checkAccess($request); // We can not return $parameters because the access check can change the // request attributes. @@ -101,7 +116,7 @@ public function matchRequest(Request $request) { * The request to access check. */ protected function checkAccess(Request $request) { - if (!$this->accessManager->checkRequest($request, $this->account)) { + if (!$this->accessManager->checkRequest($request, $this->accountProxy)) { throw new AccessDeniedHttpException(); } }