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

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

namespace Drupal\user\EventSubscriber;

10 11 12
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\MaintenanceModeInterface;
13 14 15 16 17 18 19 20 21 22
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 {

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

50 51 52 53 54 55 56 57
  /**
   * 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();
58
    $route_match = RouteMatch::createFromRequest($request);
59
    $path = $request->attributes->get('_system_path');
60
    if ($this->maintenanceMode->applies($route_match)) {
61
      // If the site is offline, log out unprivileged users.
62
      if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
63 64 65 66 67 68
        user_logout();
        // Redirect to homepage.
        $event->setResponse(new RedirectResponse(url('<front>', array('absolute' => TRUE))));
        return;
      }

69 70 71 72
      if ($this->account->isAnonymous() && $path == 'user') {
        // Forward anonymous user to login page.
        $event->setResponse(new RedirectResponse(url('user/login', array('absolute' => TRUE))));
        return;
73 74
      }
    }
75
    if ($this->account->isAuthenticated()) {
76 77 78 79 80 81 82
      if ($path == 'user/login') {
        // If user is logged in, redirect to 'user' instead of giving 403.
        $event->setResponse(new RedirectResponse(url('user', array('absolute' => TRUE))));
        return;
      }
      if ($path == 'user/register') {
        // Authenticated user should be redirected to user edit page.
83
        $event->setResponse(new RedirectResponse(url('user/' . $this->account->id() . '/edit', array('absolute' => TRUE))));
84 85 86 87 88 89 90 91 92 93 94 95 96 97
        return;
      }
    }
  }

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

}