MaintenanceModeSubscriber.php 2.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

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

namespace Drupal\user\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
14
use Drupal\Core\EventSubscriber\MaintenanceModeSubscriber as CoreMaintenanceModeSubscriber;
15 16 17 18 19 20 21 22 23 24 25 26 27

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

  /**
   * 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) {
28
    $user = \Drupal::currentUser();
29 30
    $request = $event->getRequest();
    $site_status = $request->attributes->get('_maintenance');
31 32
    // @todo Remove dependency on the internal _system_path attribute:
    //   https://www.drupal.org/node/2288911.
33
    $path = $request->attributes->get('_system_path');
34
    if ($site_status == CoreMaintenanceModeSubscriber::SITE_OFFLINE) {
35
      // If the site is offline, log out unprivileged users.
36
      if ($user->isAuthenticated() && !$user->hasPermission('access site in maintenance mode')) {
37 38 39 40 41 42
        user_logout();
        // Redirect to homepage.
        $event->setResponse(new RedirectResponse(url('<front>', array('absolute' => TRUE))));
        return;
      }

43
      if ($user->isAnonymous()) {
44 45 46 47 48 49 50 51
        switch ($path) {
          case 'user':
            // Forward anonymous user to login page.
            $event->setResponse(new RedirectResponse(url('user/login', array('absolute' => TRUE))));
            return;
          case 'user/login':
          case 'user/password':
            // Disable offline mode.
52
            $request->attributes->set('_maintenance', CoreMaintenanceModeSubscriber::SITE_ONLINE);
53 54 55 56
            break;
          default:
            if (strpos($path, 'user/reset/') === 0) {
              // Disable offline mode.
57
              $request->attributes->set('_maintenance', CoreMaintenanceModeSubscriber::SITE_ONLINE);
58 59 60 61 62
            }
            break;
        }
      }
    }
63
    if ($user->isAuthenticated()) {
64 65 66 67 68 69 70
      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.
71
        $event->setResponse(new RedirectResponse(url('user/' . $user->id() . '/edit', array('absolute' => TRUE))));
72 73 74 75 76 77 78 79 80 81 82 83 84 85
        return;
      }
    }
  }

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

}