Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / EventSubscriber / MaintenanceModeSubscriber.php
1 <?php
2
3 namespace Drupal\user\EventSubscriber;
4
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;
12
13 /**
14  * Maintenance mode subscriber to log out users.
15  */
16 class MaintenanceModeSubscriber implements EventSubscriberInterface {
17
18   use UrlGeneratorTrait;
19
20   /**
21    * The maintenance mode.
22    *
23    * @var \Drupal\Core\Site\MaintenanceMode
24    */
25   protected $maintenanceMode;
26
27   /**
28    * The current account.
29    *
30    * @var \Drupal\Core\Session\AccountInterface
31    */
32   protected $account;
33
34   /**
35    * Constructs a new MaintenanceModeSubscriber.
36    *
37    * @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode
38    *   The maintenance mode.
39    * @param \Drupal\Core\Session\AccountInterface $account
40    *   The current user.
41    */
42   public function __construct(MaintenanceModeInterface $maintenance_mode, AccountInterface $account) {
43     $this->maintenanceMode = $maintenance_mode;
44     $this->account = $account;
45   }
46
47   /**
48    * Logout users if site is in maintenance mode.
49    *
50    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
51    *   The event to process.
52    */
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)) {
59         user_logout();
60         // Redirect to homepage.
61         $event->setResponse($this->redirect($this->url('<front>')));
62       }
63     }
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public static function getSubscribedEvents() {
70     $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 31];
71     return $events;
72   }
73
74 }