Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\user\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Routing\RouteMatch;
|
Chris@0
|
6 use Drupal\Core\Routing\UrlGeneratorTrait;
|
Chris@0
|
7 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
8 use Drupal\Core\Site\MaintenanceModeInterface;
|
Chris@0
|
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
10 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
Chris@0
|
11 use Symfony\Component\HttpKernel\KernelEvents;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Maintenance mode subscriber to log out users.
|
Chris@0
|
15 */
|
Chris@0
|
16 class MaintenanceModeSubscriber implements EventSubscriberInterface {
|
Chris@0
|
17
|
Chris@0
|
18 use UrlGeneratorTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The maintenance mode.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Site\MaintenanceMode
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $maintenanceMode;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The current account.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $account;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Constructs a new MaintenanceModeSubscriber.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode
|
Chris@0
|
38 * The maintenance mode.
|
Chris@0
|
39 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
40 * The current user.
|
Chris@0
|
41 */
|
Chris@0
|
42 public function __construct(MaintenanceModeInterface $maintenance_mode, AccountInterface $account) {
|
Chris@0
|
43 $this->maintenanceMode = $maintenance_mode;
|
Chris@0
|
44 $this->account = $account;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Logout users if site is in maintenance mode.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
|
Chris@0
|
51 * The event to process.
|
Chris@0
|
52 */
|
Chris@0
|
53 public function onKernelRequestMaintenance(GetResponseEvent $event) {
|
Chris@0
|
54 $request = $event->getRequest();
|
Chris@0
|
55 $route_match = RouteMatch::createFromRequest($request);
|
Chris@0
|
56 if ($this->maintenanceMode->applies($route_match)) {
|
Chris@0
|
57 // If the site is offline, log out unprivileged users.
|
Chris@0
|
58 if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
|
Chris@0
|
59 user_logout();
|
Chris@0
|
60 // Redirect to homepage.
|
Chris@0
|
61 $event->setResponse($this->redirect($this->url('<front>')));
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public static function getSubscribedEvents() {
|
Chris@0
|
70 $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 31];
|
Chris@0
|
71 return $events;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 }
|