You are here

class MaintenanceModeSubscriber in Zircon Profile 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\Core\EventSubscriber\MaintenanceModeSubscriber
  2. 8 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber
Same name and namespace in other branches
  1. 8.0 core/modules/user/src/EventSubscriber/MaintenanceModeSubscriber.php \Drupal\user\EventSubscriber\MaintenanceModeSubscriber

Maintenance mode subscriber to logout users.

Hierarchy

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 21
Contains \Drupal\user\EventSubscriber\MaintenanceModeSubscriber.

Namespace

Drupal\user\EventSubscriber
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MaintenanceModeSubscriber::$account protected property The current account.
MaintenanceModeSubscriber::$maintenanceMode protected property The maintenance mode.
MaintenanceModeSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
MaintenanceModeSubscriber::onKernelRequestMaintenance public function Logout users if site is in maintenance mode.
MaintenanceModeSubscriber::__construct public function Constructs a new MaintenanceModeSubscriber.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator protected function Returns the URL generator service.
UrlGeneratorTrait::redirect protected function Returns a redirect response object for the specified route.
UrlGeneratorTrait::setUrlGenerator public function Sets the URL generator service.
UrlGeneratorTrait::url protected function Generates a URL or path for a specific route based on the given parameters.