MaintenanceModeSubscriber.php 2.85 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\user\EventSubscriber\MaintenanceModeSubscriber.
 */

namespace Drupal\user\EventSubscriber;

10
use Drupal\Core\Routing\RouteMatch;
11
use Drupal\Core\Routing\UrlGeneratorTrait;
12 13
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\MaintenanceModeInterface;
14 15 16 17 18 19 20 21 22 23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Maintenance mode subscriber to logout users.
 */
class MaintenanceModeSubscriber implements EventSubscriberInterface {

24 25
  use UrlGeneratorTrait;

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  /**
   * 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;
  }

53 54 55 56 57 58 59 60
  /**
   * Determine whether the page is configured to be offline.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event to process.
   */
  public function onKernelRequestMaintenance(GetResponseEvent $event) {
    $request = $event->getRequest();
61
    $route_match = RouteMatch::createFromRequest($request);
62
    $path = $request->attributes->get('_system_path');
63
    if ($this->maintenanceMode->applies($route_match)) {
64
      // If the site is offline, log out unprivileged users.
65
      if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
66 67
        user_logout();
        // Redirect to homepage.
68
        $event->setResponse(new RedirectResponse($this->url('<front>', [], ['absolute' => TRUE])));
69 70 71
        return;
      }
    }
72
    if ($this->account->isAuthenticated()) {
73
      if ($path == 'user/login') {
74 75
        // If the user is already logged in, redirect to their profile page.
        $event->setResponse($this->redirect('entity.user.canonical', ['user' => $this->account->id()]));
76 77 78
        return;
      }
      if ($path == 'user/register') {
79
        // If the user is already registered, redirect to their edit page.
80
        $event->setResponse(new RedirectResponse($this->url('entity.user.edit_form', ['user' => $this->account->id()], ['absolute' => TRUE])));
81 82 83 84 85 86 87 88 89 90 91 92 93 94
        return;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('onKernelRequestMaintenance', 35);
    return $events;
  }

}