diff --git a/core/core.services.yml b/core/core.services.yml index fa5c33e..e5fae05 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -611,7 +611,7 @@ services: - { name: service_collector, tag: maintenance_mode_exemption, call: addExemption } maintenance_mode_subscriber: class: Drupal\Core\EventSubscriber\MaintenanceModeSubscriber - arguments: ['@site_status_negotiator', '@config.factory', '@string_translation', '@url_generator', '@current_user'] + arguments: ['@site_status_negotiator', '@string_translation', '@url_generator', '@current_user'] tags: - { name: event_subscriber } path_subscriber: diff --git a/core/lib/Drupal/Core/Controller/ExceptionController.php b/core/lib/Drupal/Core/Controller/ExceptionController.php index 320cb06..7183019 100644 --- a/core/lib/Drupal/Core/Controller/ExceptionController.php +++ b/core/lib/Drupal/Core/Controller/ExceptionController.php @@ -18,6 +18,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\HttpKernelInterface; use Drupal\Component\Utility\String; +use Drupal\Component\Utility\Xss; use Symfony\Component\Debug\Exception\FlattenException; use Drupal\Core\ContentNegotiation; use Drupal\Core\Utility\Error; @@ -324,6 +325,29 @@ public function on500Html(FlattenException $exception, Request $request) { } /** + * Deliver the 503 page if the site is in maintenance mode. + * + * @param \Symfony\Component\Debug\Exception\FlattenException $exception + * The flattened exception. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request object that triggered this exception. + * + * @return \Symfony\Component\HttpFoundation\Response + * A response object. + */ + public function on503Html(FlattenException $exception, Request $request) { + $maintenance_message = $this->container->get('config.factory')->get('system.maintenance')->get('message'); + $site_name = $this->container->get('config.factory')->get('system.site')->get('name'); + + $content = Xss::filterAdmin(String::format($maintenance_message, array('@site' => $site_name))); + $output = DefaultHtmlPageRenderer::renderPage($content, $this->t('Site under maintenance')); + $response = new Response($output); + $response->setStatusCode(503, 'Service Unavailable'); + + return $response; + } + + /** * Processes an AccessDenied exception that occurred on a JSON request. * * @param \Symfony\Component\Debug\Exception\FlattenException $exception diff --git a/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php index b36ec07..fe595b1 100644 --- a/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php @@ -7,21 +7,17 @@ namespace Drupal\Core\EventSubscriber; -use Drupal\Component\Utility\String; -use Drupal\Component\Utility\Xss; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Page\DefaultHtmlPageRenderer; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Site\StatusNegotiatorInterface; -use Drupal\Core\State\StateInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; +use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; /** @@ -46,13 +42,6 @@ class MaintenanceModeSubscriber implements EventSubscriberInterface { protected $account; /** - * The config factory. - * - * @var \Drupal\Core\Config\ConfigFactoryInterface - */ - protected $config; - - /** * The url generator. * * @var \Drupal\Core\Routing\UrlGeneratorInterface @@ -64,8 +53,6 @@ class MaintenanceModeSubscriber implements EventSubscriberInterface { * * @param \Drupal\Core\Site\StatusNegotiatorInterface $status_negotiator * The site status negotiator. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The config factory. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation * The string translation. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator @@ -73,9 +60,8 @@ class MaintenanceModeSubscriber implements EventSubscriberInterface { * @param \Drupal\Core\Session\AccountInterface $account * The current user. */ - public function __construct(StatusNegotiatorInterface $status_negotiator, ConfigFactoryInterface $config_factory, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account) { + public function __construct(StatusNegotiatorInterface $status_negotiator, TranslationInterface $translation, UrlGeneratorInterface $url_generator, AccountInterface $account) { $this->statusNegotiator = $status_negotiator; - $this->config = $config_factory; $this->stringTranslation = $translation; $this->urlGenerator = $url_generator; $this->account = $account; @@ -88,19 +74,12 @@ public function __construct(StatusNegotiatorInterface $status_negotiator, Config * The event to process. */ public function onKernelRequestMaintenance(GetResponseEvent $event) { - if ($this->statusNegotiator->getStatus() != StatusNegotiatorInterface::SITE_ONLINE) { + if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST && $this->statusNegotiator->getStatus() != StatusNegotiatorInterface::SITE_ONLINE) { $route_match = RouteMatch::createFromRequest($event->getRequest()); if (!$this->statusNegotiator->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. - drupal_maintenance_theme(); - $content = Xss::filterAdmin(String::format($this->config->get('system.maintenance')->get('message'), array( - '@site' => $this->config->get('system.site')->get('name'), - ))); - $content = DefaultHtmlPageRenderer::renderPage($content, $this->t('Site under maintenance')); - $response = new Response('Service unavailable', 503); - $response->setContent($content); - $event->setResponse($response); + throw new ServiceUnavailableHttpException(); } else { // Display a message if the logged in user has access to the site in diff --git a/core/modules/user/src/Site/MaintenanceExemption.php b/core/modules/user/src/Site/MaintenanceExemption.php index c914c4d..6e3ab4b 100644 --- a/core/modules/user/src/Site/MaintenanceExemption.php +++ b/core/modules/user/src/Site/MaintenanceExemption.php @@ -21,9 +21,7 @@ class MaintenanceExemption implements MaintenanceExemptionInterface { */ public function exempt(RouteMatchInterface $route_match, AccountInterface $account) { $route_name = $route_match->getRouteName(); - if ($route_name == 'user.login' || $route_name == 'user.pass' || $route_name == 'user.reset') { - return TRUE; - } + return in_array($route_name, ['user.login', 'user.pass', 'user.reset']); } }