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