MaintenanceModeSubscriber.php

Same filename in this branch
  1. 11.x core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
Same filename in other branches
  1. 9 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php
  2. 9 core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
  3. 8.9.x core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php
  4. 8.9.x core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php
  5. 10 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php
  6. 10 core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php

Namespace

Drupal\user\EventSubscriber

File

core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php

View source
<?php

namespace Drupal\user\EventSubscriber;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\MaintenanceModeEvents;
use Drupal\Core\Site\MaintenanceModeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
 * Maintenance mode subscriber to log out users.
 */
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 and user is not exempt.
     *
     * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
     *   The event to process.
     */
    public function onMaintenanceModeRequest(RequestEvent $event) {
        // If the site is offline, log out unprivileged users.
        if ($this->account
            ->isAuthenticated()) {
            user_logout();
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[MaintenanceModeEvents::MAINTENANCE_MODE_REQUEST][] = [
            'onMaintenanceModeRequest',
            -900,
        ];
        return $events;
    }

}

Classes

Title Deprecated Summary
MaintenanceModeSubscriber Maintenance mode subscriber to log out users.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.