You are here

class MaintenanceModeExempt in Maintenance Exempt 8

Provides the default implementation of the maintenance mode service.

Hierarchy

Expanded class hierarchy of MaintenanceModeExempt

1 string reference to 'MaintenanceModeExempt'
maintenance_exempt.services.yml in ./maintenance_exempt.services.yml
maintenance_exempt.services.yml
1 service uses MaintenanceModeExempt
maintenance_mode in ./maintenance_exempt.services.yml
Drupal\maintenance_exempt\MaintenanceModeExempt

File

src/MaintenanceModeExempt.php, line 15

Namespace

Drupal\maintenance_exempt
View source
class MaintenanceModeExempt extends MaintenanceMode implements MaintenanceModeInterface {

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  private $request;

  /**
   * The path matcher service.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  private $pathMatcher;

  /**
   * Constructs a new maintenance mode service.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
   *   The path matcher service.
   */
  public function __construct(StateInterface $state, RequestStack $request_stack, PathMatcherInterface $path_matcher) {
    $this->state = $state;
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->pathMatcher = $path_matcher;
  }

  /**
   * {@inheritdoc}
   */
  public function exempt(AccountInterface $account) {

    // Standard core behaviour - check user's permission.
    if ($account
      ->hasPermission('access site in maintenance mode')) {
      return TRUE;
    }

    // Check if the IP address should be exempted.
    $client_ip = $this->request
      ->getClientIp();
    if (in_array($client_ip, maintenance_exempt_get_ips())) {
      return TRUE;
    }
    if (maintenance_exempt_by_cidr_notation($client_ip)) {
      return TRUE;
    }

    // Check if the URL should be exempted.
    $exempt_urls = \Drupal::config('maintenance_exempt.settings')
      ->get('exempt_urls');

    // Check the actual URL.
    $current_url = $this->request
      ->getPathInfo();
    if ($this->pathMatcher
      ->matchPath($current_url, $exempt_urls)) {
      return TRUE;
    }

    // Check the system path of aliased paths.
    $current_path = \Drupal::service('path.current')
      ->getPath();
    if ($this->pathMatcher
      ->matchPath($current_path, $exempt_urls)) {
      return TRUE;
    }

    // Fetch the query string exemption key if there is one.
    $config = \Drupal::config('maintenance_exempt.settings');
    $key = $config
      ->get('query_key');

    // Exemption status may be stored in the session.
    if (isset($_SESSION['maintenance_exempt']) && $_SESSION['maintenance_exempt'] == $key) {
      return TRUE;
    }
    if ($key && isset($_GET[$key])) {
      $_SESSION['maintenance_exempt'] = $key;
      return TRUE;
    }

    // No valid exemption, so user remains blocked.
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MaintenanceMode::$state protected property The state.
MaintenanceMode::applies public function Returns whether the site is in maintenance mode. Overrides MaintenanceModeInterface::applies
MaintenanceModeExempt::$pathMatcher private property The path matcher service.
MaintenanceModeExempt::$request private property The current request.
MaintenanceModeExempt::exempt public function Determines whether a user has access to the site in maintenance mode. Overrides MaintenanceMode::exempt
MaintenanceModeExempt::__construct public function Constructs a new maintenance mode service. Overrides MaintenanceMode::__construct