diff --git a/core/core.services.yml b/core/core.services.yml index 5663b5e..c5c3c43 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1090,9 +1090,9 @@ services: arguments: ['@authentication', '@current_user'] tags: - { name: event_subscriber } - authentication_method_check_subscriber: - class: Drupal\Core\EventSubscriber\AuthenticationMethodCheckSubscriber - arguments: ['@current_user'] + authentication_provider_check_subscriber: + class: Drupal\Core\EventSubscriber\AuthenticationProviderCheckSubscriber + arguments: ['@authentication'] tags: - { name: event_subscriber } account_switcher: diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php index 53af56a..925f334 100644 --- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php +++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php @@ -22,7 +22,7 @@ * * If no provider set an active user then the user is set to anonymous. */ -class AuthenticationManager implements AuthenticationProviderInterface { +class AuthenticationManager implements AuthenticationProviderInterface, AuthenticationProviderAccessInterface { /** * Array of all registered authentication providers, keyed by ID. @@ -46,6 +46,14 @@ class AuthenticationManager implements AuthenticationProviderInterface { protected $sortedProviders; /** + * Associative array of providers available on every request unless + * explicitely excluded. + * + * @var array + */ + protected $defaultProviders = ['cookie' => TRUE]; + + /** * Adds a provider to the array of registered providers. * * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $provider @@ -75,17 +83,12 @@ public function applies(Request $request) { /** * {@inheritdoc} */ - public function authenticate(Request $request, &$triggered_provider = NULL) { + public function authenticate(Request $request) { $account = NULL; - // Iterate the allowed providers. - foreach ($this->getSortedProviders() as $provider_id => $provider) { - if ($provider->applies($request)) { - // Try to authenticate with this provider, skipping all others. - $account = $provider->authenticate($request); - $triggered_provider = $provider_id; - break; - } + $provider_id = $this->getProvider($request); + if (isset($provider_id)) { + $account = $this->providers[$provider_id]->authenticate($request); } // No provider returned a valid account, so set the user to anonymous. @@ -97,6 +100,36 @@ public function authenticate(Request $request, &$triggered_provider = NULL) { } /** + * {@inheritdoc} + */ + public function access(Request $request) { + $result = TRUE; + + $route = RouteMatch::createFromRequest($request)->getRouteObject(); + $provider_id = $this->getProvider($request); + if (isset($provider_id)) { + $has_auth_option = isset($route) && $route->hasOption('_auth'); + + $allowed_by_default = !$has_auth_option && isset($this->defaultProviders[$provider_id]); + $allowed_by_option = $has_auth_option && in_array($provider_id, $route->getOption('_auth')); + $result = $allowed_by_default || $allowed_by_option; + } + + return $result; + } + + /** + * Returns id of the authentication provider for a request. + */ + protected function getProvider(Request $request) { + foreach ($this->getSortedProviders() as $provider_id => $provider) { + if ($provider->applies($request)) { + return $provider_id; + } + } + } + + /** * Returns the sorted array of authentication providers. * * @return array diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php new file mode 100644 index 0000000..d62dd1c --- /dev/null +++ b/core/lib/Drupal/Core/Authentication/AuthenticationProviderAccessInterface.php @@ -0,0 +1,37 @@ +container->initialized('current_user')) { $current_user_id = $this->container->get('current_user')->id(); - $current_user_auth = $this->container->get('current_user')->getAuthenticationMethod(); } // If there is a session manager, close and save the session. @@ -740,7 +739,6 @@ protected function initializeContainer($rebuild = FALSE) { if (!empty($current_user_id)) { $this->container->get('current_user')->setInitialAccountId($current_user_id); - $this->container->get('current_user')->setAuthenticationMethod($current_user_auth); } \Drupal::setContainer($this->container); diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationMethodCheckSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationMethodCheckSubscriber.php deleted file mode 100644 index c71190a..0000000 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationMethodCheckSubscriber.php +++ /dev/null @@ -1,76 +0,0 @@ -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; - } - -} diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationProviderCheckSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationProviderCheckSubscriber.php new file mode 100644 index 0000000..1ea4987 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationProviderCheckSubscriber.php @@ -0,0 +1,53 @@ +check = $check; + } + + /** + * Checks whether authentication was performed with a method allowed on the route. + * + * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event + * The request event. + */ + public function onKernelRequestCheckAuthenticationProvider(GetResponseEvent $event) { + if (!$this->check->access($event->getRequest())) { + throw new AccessDeniedHttpException(); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + // Runs after routing. + $events[KernelEvents::REQUEST][] = array('onKernelRequestCheckAuthenticationProvider', 31); + return $events; + } + +} diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php index 1146f30..4d622a7 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @@ -58,10 +58,8 @@ public function __construct(AuthenticationProviderInterface $authentication_prov public function onKernelRequestAuthenticate(GetResponseEvent $event) { if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { $request = $event->getRequest(); - $triggered_provider = NULL; - $account = $this->authenticationProvider->authenticate($request, $triggered_provider); + $account = $this->authenticationProvider->authenticate($request); $this->accountProxy->setAccount($account); - $this->accountProxy->setAuthenticationMethod($triggered_provider); } } diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index 7c56b1b..b675b02 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -28,11 +28,6 @@ class AccountProxy implements AccountProxyInterface { protected $account; /** - * The authentication method used. - */ - protected $authenticationMethod; - - /** * Initial account id. * * @var int @@ -187,18 +182,4 @@ public function setInitialAccountId($account_id) { $this->initialAccountId = $account_id; } - /** - * {@inheritdoc} - */ - public function setAuthenticationMethod($provider_id) { - $this->authenticationMethod = $provider_id; - } - - /** - * {@inheritdoc} - */ - public function getAuthenticationMethod() { - return $this->authenticationMethod; - } - } diff --git a/core/lib/Drupal/Core/Session/AccountProxyInterface.php b/core/lib/Drupal/Core/Session/AccountProxyInterface.php index ee0f16a..7ac9758 100644 --- a/core/lib/Drupal/Core/Session/AccountProxyInterface.php +++ b/core/lib/Drupal/Core/Session/AccountProxyInterface.php @@ -48,20 +48,4 @@ public function getAccount(); */ public function setInitialAccountId($account_id); - /** - * Sets the used authentication method. - * - * @param string $provider_id - * The id of the used authentication method. - */ - public function setAuthenticationMethod($provider_id); - - /** - * Gets the used authentication method. - * - * @return string|NULL - * The id of the used authentication method or NULL if not authenticated. - */ - public function getAuthenticationMethod(); - }