diff --git a/core/core.services.yml b/core/core.services.yml index a7addbd..5663b5e 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -829,10 +829,6 @@ services: tags: - { name: access_check, applies_to: _csrf_token, needs_incoming_request: TRUE } arguments: ['@csrf_token'] - access_check.auth_method: - class: Drupal\Core\Access\AuthenticationMethodCheck - tags: - - { name: access_check, needs_incoming_request: TRUE } maintenance_mode: class: Drupal\Core\Site\MaintenanceMode arguments: ['@state', '@current_user'] @@ -1094,6 +1090,11 @@ services: arguments: ['@authentication', '@current_user'] tags: - { name: event_subscriber } + authentication_method_check_subscriber: + class: Drupal\Core\EventSubscriber\AuthenticationMethodCheckSubscriber + arguments: ['@current_user'] + tags: + - { name: event_subscriber } account_switcher: class: Drupal\Core\Session\AccountSwitcher arguments: ['@current_user', '@session_handler.write_safe'] diff --git a/core/lib/Drupal/Core/Access/AuthenticationMethodCheck.php b/core/lib/Drupal/Core/Access/AuthenticationMethodCheck.php deleted file mode 100644 index c27aa3e..0000000 --- a/core/lib/Drupal/Core/Access/AuthenticationMethodCheck.php +++ /dev/null @@ -1,71 +0,0 @@ -defaultProviders = array('cookie' => TRUE); - } - - /** - * {@inheritdoc} - */ - public function applies(Route $route) { - return TRUE; - } - - /** - * Checks whether authentication was performed with an allowed method. - * - * @param Symfony\Component\Routing\Route $route - * The route to check. - * @param Symfony\Component\HttpFoundation\Request $request - * The incoming request. This parameter is not used by the method. Its - * purpose is to indicate that this access check only makes sense when - * processing the incoming request. - * @param Drupal\Core\Session\AccountProxyInterface $account - * The account proxy for the authenticated user. - */ - public function access(Route $route, Request $request, AccountProxyInterface $account) { - $provider_id = $account->getAuthenticationMethod(); - if (!isset($provider_id)) { - return AccessResult::allowed()->setCacheable(FALSE); - } - - $has_auth_option = $route->hasOption('_auth'); - if (!$has_auth_option && !empty($this->defaultProviders[$provider_id])) { - return AccessResult::allowed()->setCacheable(FALSE); - } - - if ($has_auth_option && in_array($provider_id, $route->getOption('_auth'))) { - return AccessResult::allowed()->setCacheable(FALSE); - } - - return AccessResult::forbidden()->setCacheable(FALSE); - } - -} diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationMethodCheckSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationMethodCheckSubscriber.php new file mode 100644 index 0000000..c71190a --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationMethodCheckSubscriber.php @@ -0,0 +1,76 @@ +currentUser = $current_user; + + // @todo: Collect list of providers in a compiler pass and inject them into + // the constructor. + $this->defaultProviders = array('cookie' => TRUE); + } + + /** + * Checks whether authentication was performed with a method allowed on the route. + * + * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * The request event. + */ + public function onKernelRequestCheckAuthenticationMethod(GetResponseEvent $event) { + $provider_id = $this->currentUser->getAuthenticationMethod(); + + if (isset($provider_id)) { + $route_match = RouteMatch::createFromRequest($event->getRequest()); + $route = $route_match->getRouteObject(); + $has_auth_option = isset($route) && $route->hasOption('_auth'); + + $allowed_by_default = !$has_auth_option && !empty($this->defaultProviders[$provider_id]); + $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); + if (!$allowed_by_default && !$allowed_by_option) { + throw new AccessDeniedHttpException(); + } + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + // Runs after routing. + $events[KernelEvents::REQUEST][] = array('onKernelRequestCheckAuthenticationMethod', 31); + return $events; + } + +}