3 namespace Drupal\user\EventSubscriber;
5 use Drupal\Core\Routing\RouteMatch;
6 use Drupal\Core\Routing\UrlGeneratorTrait;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Site\MaintenanceModeInterface;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
11 use Symfony\Component\HttpKernel\KernelEvents;
14 * Maintenance mode subscriber to log out users.
16 class MaintenanceModeSubscriber implements EventSubscriberInterface {
18 use UrlGeneratorTrait;
21 * The maintenance mode.
23 * @var \Drupal\Core\Site\MaintenanceMode
25 protected $maintenanceMode;
28 * The current account.
30 * @var \Drupal\Core\Session\AccountInterface
35 * Constructs a new MaintenanceModeSubscriber.
37 * @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode
38 * The maintenance mode.
39 * @param \Drupal\Core\Session\AccountInterface $account
42 public function __construct(MaintenanceModeInterface $maintenance_mode, AccountInterface $account) {
43 $this->maintenanceMode = $maintenance_mode;
44 $this->account = $account;
48 * Logout users if site is in maintenance mode.
50 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
51 * The event to process.
53 public function onKernelRequestMaintenance(GetResponseEvent $event) {
54 $request = $event->getRequest();
55 $route_match = RouteMatch::createFromRequest($request);
56 if ($this->maintenanceMode->applies($route_match)) {
57 // If the site is offline, log out unprivileged users.
58 if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
60 // Redirect to homepage.
61 $event->setResponse($this->redirect($this->url('<front>')));
69 public static function getSubscribedEvents() {
70 $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 31];