diff --git a/core/modules/basic_auth/basic_auth.services.yml b/core/modules/basic_auth/basic_auth.services.yml index a0d4368..05d1252 100644 --- a/core/modules/basic_auth/basic_auth.services.yml +++ b/core/modules/basic_auth/basic_auth.services.yml @@ -1,6 +1,6 @@ services: authentication.basic_auth: class: Drupal\basic_auth\Authentication\Provider\BasicAuth - arguments: ['@config.factory', '@user.auth', '@flood', '@entity.manager'] + arguments: ['@config.factory', '@user.auth', '@flood', '@entity.manager', '@current_user'] tags: - { name: authentication_provider, priority: 100 } diff --git a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php index eb8920c..47d2a35 100644 --- a/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php +++ b/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php @@ -7,12 +7,13 @@ namespace Drupal\basic_auth\Authentication\Provider; -use \Drupal\Component\Utility\String; +use Drupal\Component\Utility\String; use Drupal\Core\Authentication\AuthenticationProviderInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Routing\RouteMatch; +use Drupal\Core\Session\AccountInterface; use Drupal\user\UserAuthInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; @@ -54,6 +55,13 @@ class BasicAuth implements AuthenticationProviderInterface { protected $entityManager; /** + * The current user. + * + * @var Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** * Constructs a HTTP basic authentication provider object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory @@ -65,11 +73,12 @@ class BasicAuth implements AuthenticationProviderInterface { * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. */ - public function __construct(ConfigFactoryInterface $config_factory, UserAuthInterface $user_auth, FloodInterface $flood, EntityManagerInterface $entity_manager) { + public function __construct(ConfigFactoryInterface $config_factory, UserAuthInterface $user_auth, FloodInterface $flood, EntityManagerInterface $entity_manager, AccountInterface $current_user) { $this->configFactory = $config_factory; $this->userAuth = $user_auth; $this->flood = $flood; $this->entityManager = $entity_manager; + $this->currentUser = $current_user; } /** @@ -139,7 +148,7 @@ public function cleanup(Request $request) {} public function handleException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); - if ($GLOBALS['user']->isAnonymous() && $exception instanceof AccessDeniedHttpException && !$this->hasCredentials($request)) { + if ($this->currentUser->isAnonymous() && $exception instanceof AccessDeniedHttpException && !$this->hasCredentials($request)) { $route = RouteMatch::createFromRequest($request)->getRouteObject(); if (isset($route) && $this->appliesToRoute($route)) { $site_name = $this->configFactory->get('system.site')->get('name');