class MaintenanceModeSubscriber

Same name in this branch
  1. 9 core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber
Same name in other branches
  1. 8.9.x core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber
  3. 10 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber
  4. 10 core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber
  5. 11.x core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber
  6. 11.x core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\Core\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
user_maintenance_mode_subscriber in core/modules/user/user.services.yml
Drupal\user\EventSubscriber\MaintenanceModeSubscriber

File

core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php, line 17

Namespace

Drupal\user\EventSubscriber
View 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.
     *
     * @deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. Use
     *   \Drupal\user\EventSubscriber::onMaintenanceModeRequest() instead.
     *
     * @see https://www.drupal.org/node/3255799
     */
    public function onKernelRequestMaintenance(RequestEvent $event) {
        @trigger_error('\\Drupal\\user\\EventSubscriber::onKernelRequestMaintenance() is deprecated in drupal:9.4.0 and is removed from drupal:10.0.0. Use \\Drupal\\user\\EventSubscriber::onMaintenanceModeRequest() instead. See https://www.drupal.org/node/3255799', E_USER_DEPRECATED);
        $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()));
            }
        }
    }
    
    /**
     * 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();
            // Redirect to homepage.
            $event->setResponse(new RedirectResponse(Url::fromRoute('<front>')->toString()));
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        $events[MaintenanceModeEvents::MAINTENANCE_MODE_REQUEST][] = [
            'onMaintenanceModeRequest',
            -900,
        ];
        return $events;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
MaintenanceModeSubscriber::$account protected property The current account.
MaintenanceModeSubscriber::$maintenanceMode protected property The maintenance mode.
MaintenanceModeSubscriber::getSubscribedEvents public static function
MaintenanceModeSubscriber::onKernelRequestMaintenance Deprecated public function Logout users if site is in maintenance mode.
MaintenanceModeSubscriber::onMaintenanceModeRequest public function Logout users if site is in maintenance mode and user is not exempt.
MaintenanceModeSubscriber::__construct public function Constructs a new MaintenanceModeSubscriber.

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