Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 15 |
MaintenanceModeSubscriber | |
0.00% |
0 / 1 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 15 |
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
onKernelRequestMaintenance | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 9 |
|||
getSubscribedEvents | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
<?php | |
/** | |
* @file | |
* Contains \Drupal\user\EventSubscriber\MaintenanceModeSubscriber. | |
*/ | |
namespace Drupal\user\EventSubscriber; | |
use Drupal\Core\Routing\RouteMatch; | |
use Drupal\Core\Routing\UrlGeneratorTrait; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\Core\Site\MaintenanceModeInterface; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\HttpKernel\KernelEvents; | |
/** | |
* Maintenance mode subscriber to logout users. | |
*/ | |
class MaintenanceModeSubscriber implements EventSubscriberInterface { | |
use UrlGeneratorTrait; | |
/** | |
* The maintenance mode. | |
* | |
* @var \Drupal\Core\Site\MaintenanceMode | |
*/ | |
protected $maintenanceMode; | |
/** | |
* The current account. | |
* | |
* @var \Drupal\Core\Session\AccountInterface | |
*/ | |
protected $account; | |
/** | |
* Constructs a new MaintenanceModeSubscriber. | |
* | |
* @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode | |
* The maintenance mode. | |
* @param \Drupal\Core\Session\AccountInterface $account | |
* The current user. | |
*/ | |
public function __construct(MaintenanceModeInterface $maintenance_mode, AccountInterface $account) { | |
$this->maintenanceMode = $maintenance_mode; | |
$this->account = $account; | |
} | |
/** | |
* Logout users if site is in maintenance mode. | |
* | |
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event | |
* The event to process. | |
*/ | |
public function onKernelRequestMaintenance(GetResponseEvent $event) { | |
$request = $event->getRequest(); | |
$route_match = RouteMatch::createFromRequest($request); | |
if ($this->maintenanceMode->applies($route_match)) { | |
// If the site is offline, log out unprivileged users. | |
if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) { | |
user_logout(); | |
// Redirect to homepage. | |
$event->setResponse($this->redirect($this->url('<front>'))); | |
} | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() { | |
$events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 31]; | |
return $events; | |
} | |
} |