diff --git a/core/core.services.yml b/core/core.services.yml index 6655946..7cf4917 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -693,7 +693,7 @@ services: route_enhancer.lazy_collector: class: Drupal\Core\Routing\LazyRouteEnhancer tags: - - { name: non_lazy_route_enhancer, priority: 5000 } + - { name: non_lazy_route_enhancer } parent: container.trait route_enhancer_subscriber: class: Drupal\Core\EventSubscriber\RouteEnhancerSubscriber @@ -706,6 +706,11 @@ services: tags: - { name: route_enhancer } - { name: event_subscriber } + route_enhancer.authentication: + class: Drupal\Core\Routing\Enhancer\AuthenticationEnhancer + tags: + - { name: non_lazy_route_enhancer, priority: 1000 } + arguments: ['@authentication', '@current_user', '@request_stack'] route_enhancer.entity: class: Drupal\Core\Entity\Enhancer\EntityRouteEnhancer tags: diff --git a/core/lib/Drupal/Core/Routing/AccessAwareRouter.php b/core/lib/Drupal/Core/Routing/AccessAwareRouter.php index 247ef90..33feaaa 100644 --- a/core/lib/Drupal/Core/Routing/AccessAwareRouter.php +++ b/core/lib/Drupal/Core/Routing/AccessAwareRouter.php @@ -102,14 +102,15 @@ public function matchRequest(Request $request) { $request->attributes->add($parameters); } catch (\Exception $e) { - $routing_exception = $e; - } - - // Authenticate the user. - $this->accountProxy->setAccount($this->authenticationProvider->authenticate($request)); - - if (isset($routing_exception)) { - throw $routing_exception; + try { + $this->accountProxy->setAccount($this->authenticationProvider->authenticate($request)); + } + catch (\Exception $e) { + // Ignore exception thrown while trying to authenticate inside the + // enclosing catch clause. + } + + throw $e; } $this->checkAccess($request); diff --git a/core/lib/Drupal/Core/Routing/Enhancer/AuthenticationEnhancer.php b/core/lib/Drupal/Core/Routing/Enhancer/AuthenticationEnhancer.php new file mode 100644 index 0000000..79202c6 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/Enhancer/AuthenticationEnhancer.php @@ -0,0 +1,85 @@ +authenticationProvider = $authentication_provider; + $this->accountProxy = $account_proxy; + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public function enhance(array $defaults, Request $request) { + // Authenticate the user if the request currently being routed is the + // incomming master request. + if ($request === $this->requestStack->getMasterRequest()) { + $this->accountProxy->setAccount($this->authenticationProvider->authenticate($request)); + } + + return $defaults; + } + + /** + * {@inheritdoc} + */ + public function applies(Route $route) { + return TRUE; + } + +}