diff --git a/core/core.services.yml b/core/core.services.yml index e5fae05..5376fd4 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -604,14 +604,13 @@ services: tags: - { name: access_check, applies_to: _csrf_token } arguments: ['@csrf_token'] - site_status_negotiator: - class: Drupal\Core\Site\StatusNegotiator - arguments: ['@state', '@current_user'] + maintenance_mode_exemption: + class: Drupal\Core\Site\ChainMaintenanceExemption tags: - { name: service_collector, tag: maintenance_mode_exemption, call: addExemption } maintenance_mode_subscriber: class: Drupal\Core\EventSubscriber\MaintenanceModeSubscriber - arguments: ['@site_status_negotiator', '@string_translation', '@url_generator', '@current_user'] + arguments: ['@state', '@maintenance_mode_exemption', '@string_translation', '@url_generator', '@current_user'] tags: - { name: event_subscriber } path_subscriber: diff --git a/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php index fe595b1..c5cd0d0 100644 --- a/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php @@ -10,7 +10,8 @@ use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Site\StatusNegotiatorInterface; +use Drupal\Core\Site\MaintenanceExemptionInterface; +use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -28,11 +29,18 @@ class MaintenanceModeSubscriber implements EventSubscriberInterface { use StringTranslationTrait; /** - * The site status negotiator. + * The state. * - * @var \Drupal\Core\Site\StatusNegotiatorInterface + * @var \Drupal\Core\State\StateInterface */ - protected $statusNegotiator; + protected $state; + + /** + * The maintenance mode exemption. + * + * @var \Drupal\Core\Site\MaintenanceExemptionInterface + */ + protected $maintenanceExemption; /** * The current account. @@ -51,8 +59,10 @@ class MaintenanceModeSubscriber implements EventSubscriberInterface { /** * Constructs a new MaintenanceModeSubscriber. * - * @param \Drupal\Core\Site\StatusNegotiatorInterface $status_negotiator - * The site status negotiator. + * @param \Drupal\Core\State\StateInterface $state + * The state. + * @param \Drupal\Core\Site\MaintenanceExemptionInterface $maintenance_exemption + * The maintenance mode exemption. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation * The string translation. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator @@ -60,8 +70,9 @@ class MaintenanceModeSubscriber implements EventSubscriberInterface { * @param \Drupal\Core\Session\AccountInterface $account * The current user. */ - public function __construct(StatusNegotiatorInterface $status_negotiator, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account) { - $this->statusNegotiator = $status_negotiator; + public function __construct(StateInterface $state, MaintenanceExemptionInterface $maintenance_exemption, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account) { + $this->state = $state; + $this->maintenanceExemption = $maintenance_exemption; $this->stringTranslation = $translation; $this->urlGenerator = $url_generator; $this->account = $account; @@ -74,9 +85,9 @@ public function __construct(StatusNegotiatorInterface $status_negotiator, Transl * The event to process. */ public function onKernelRequestMaintenance(GetResponseEvent $event) { - if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST && $this->statusNegotiator->getStatus() != StatusNegotiatorInterface::SITE_ONLINE) { + if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST && $this->state->get('system.maintenance_mode')) { $route_match = RouteMatch::createFromRequest($event->getRequest()); - if (!$this->statusNegotiator->exempt($route_match, $this->account)) { + if (!$this->maintenanceExemption->exempt($route_match, $this->account)) { // Deliver the 503 page if the site is in maintenance mode and the // logged in user is not allowed to bypass it. throw new ServiceUnavailableHttpException(); diff --git a/core/lib/Drupal/Core/Site/ChainMaintenanceExemption.php b/core/lib/Drupal/Core/Site/ChainMaintenanceExemption.php new file mode 100644 index 0000000..8b526d9 --- /dev/null +++ b/core/lib/Drupal/Core/Site/ChainMaintenanceExemption.php @@ -0,0 +1,46 @@ +exemptionHandlers as $handler) { + if ($handler->exempt($route_match, $account)) { + return TRUE; + } + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function addExemption(MaintenanceExemptionInterface $handler) { + $this->exemptionHandlers[] = $handler; + return $this; + } + +} diff --git a/core/lib/Drupal/Core/Site/ChainMaintenanceExemptionInterface.php b/core/lib/Drupal/Core/Site/ChainMaintenanceExemptionInterface.php new file mode 100644 index 0000000..e3c508e --- /dev/null +++ b/core/lib/Drupal/Core/Site/ChainMaintenanceExemptionInterface.php @@ -0,0 +1,25 @@ +state = $state; - $this->exemptionHandlers = array(); - } - - /** - * {@inheritdoc} - */ - public function getStatus() { - return $this->state->get('system.maintenance_mode') ? StatusNegotiatorInterface::SITE_OFFLINE : StatusNegotiatorInterface::SITE_ONLINE; - } - - /** - * {@inheritdoc} - */ - public function exempt(RouteMatchInterface $route_match, AccountInterface $account) { - foreach ($this->exemptionHandlers as $handler) { - if ($handler->exempt($route_match, $account)) { - return TRUE; - } - } - - return FALSE; - } - - /** - * Add an exemption handler. - * - * @param \Drupal\Core\Site\MaintenanceExemptionInterface $handler - * A maintenance exemption handler. - */ - public function addExemption(MaintenanceExemptionInterface $handler) { - $this->exemptionHandlers[] = $handler; - } - -} diff --git a/core/lib/Drupal/Core/Site/StatusNegotiatorInterface.php b/core/lib/Drupal/Core/Site/StatusNegotiatorInterface.php deleted file mode 100644 index 0eabb49..0000000 --- a/core/lib/Drupal/Core/Site/StatusNegotiatorInterface.php +++ /dev/null @@ -1,45 +0,0 @@ -statusNegotiator = $status_negotiator; + public function __construct(StateInterface $state, MaintenanceExemptionInterface $maintenance_exemption, AccountInterface $account) { + $this->state = $state; + $this->maintenanceExemption = $maintenance_exemption; $this->account = $account; } @@ -55,12 +66,11 @@ public function __construct(StatusNegotiatorInterface $status_negotiator, Accoun */ public function onKernelRequestMaintenance(GetResponseEvent $event) { $request = $event->getRequest(); - $site_status = $this->statusNegotiator->getStatus(); $path = $request->attributes->get('_system_path'); - if ($site_status == StatusNegotiatorInterface::SITE_OFFLINE) { + if ($this->state->get('system.maintenance_mode')) { $route_match = RouteMatch::createFromRequest($request); // If the site is offline, log out unprivileged users. - if ($this->account->isAuthenticated() && !$this->statusNegotiator->exempt($route_match, $this->account)) { + if ($this->account->isAuthenticated() && !$this->maintenanceExemption->exempt($route_match, $this->account)) { user_logout(); // Redirect to homepage. $event->setResponse(new RedirectResponse(url('', array('absolute' => TRUE)))); diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 74b68df..97339e9 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -38,7 +38,7 @@ services: arguments: ['@database', '@config.factory', '@entity.manager', '@entity.query'] user_maintenance_mode_subscriber: class: Drupal\user\EventSubscriber\MaintenanceModeSubscriber - arguments: ['@site_status_negotiator', '@current_user'] + arguments: ['@state', '@maintenance_mode_exemption', '@current_user'] tags: - { name: event_subscriber } theme.negotiator.admin_theme: