class MaintenanceModeSubscriber in Drupal 9
Same name in this branch
- 9 core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber
- 9 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber
Same name and namespace in other branches
- 8 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber
Maintenance mode subscriber to log out users.
Hierarchy
- class \Drupal\user\EventSubscriber\MaintenanceModeSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of MaintenanceModeSubscriber
1 string reference to 'MaintenanceModeSubscriber'
- user.services.yml in core/
modules/ user/ user.services.yml - core/modules/user/user.services.yml
1 service uses MaintenanceModeSubscriber
File
- core/
modules/ user/ src/ EventSubscriber/ MaintenanceModeSubscriber.php, line 17
Namespace
Drupal\user\EventSubscriberView source
class MaintenanceModeSubscriber implements EventSubscriberInterface {
/**
* 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\RequestEvent $event
* The event to process.
*/
public function onKernelRequestMaintenance(RequestEvent $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(new RedirectResponse(Url::fromRoute('<front>')
->toString()));
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'onKernelRequestMaintenance',
31,
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MaintenanceModeSubscriber:: |
protected | property | The current account. | |
MaintenanceModeSubscriber:: |
protected | property | The maintenance mode. | |
MaintenanceModeSubscriber:: |
public static | function | ||
MaintenanceModeSubscriber:: |
public | function | Logout users if site is in maintenance mode. | |
MaintenanceModeSubscriber:: |
public | function | Constructs a new MaintenanceModeSubscriber. |