You are here

class DomainAccessCheck in Domain Access 8

Provides a global access check to ensure inactive domains are restricted.

Hierarchy

Expanded class hierarchy of DomainAccessCheck

1 file declares its use of DomainAccessCheck
DomainSubscriber.php in domain/src/EventSubscriber/DomainSubscriber.php
1 string reference to 'DomainAccessCheck'
domain.services.yml in domain/domain.services.yml
domain/domain.services.yml
1 service uses DomainAccessCheck
access_check.domain in domain/domain.services.yml
Drupal\domain\Access\DomainAccessCheck

File

domain/src/Access/DomainAccessCheck.php, line 16

Namespace

Drupal\domain\Access
View source
class DomainAccessCheck implements AccessCheckInterface {

  /**
   * The Domain negotiator.
   *
   * @var \Drupal\domain\DomainNegotiatorInterface
   */
  protected $domainNegotiator;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

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

  /**
   * Constructs the object.
   *
   * @param \Drupal\domain\DomainNegotiatorInterface $negotiator
   *   The domain negotiation service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
   *   The path matcher service.
   */
  public function __construct(DomainNegotiatorInterface $negotiator, ConfigFactoryInterface $config_factory, PathMatcherInterface $path_matcher) {
    $this->domainNegotiator = $negotiator;
    $this->configFactory = $config_factory;
    $this->pathMatcher = $path_matcher;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Route $route) {
    return $this
      ->checkPath($route
      ->getPath());
  }

  /**
   * {@inheritdoc}
   */
  public function checkPath($path) {
    $allowed_paths = $this->configFactory
      ->get('domain.settings')
      ->get('login_paths');
    return !$this->pathMatcher
      ->matchPath($path, $allowed_paths);
  }

  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {
    $domain = $this->domainNegotiator
      ->getActiveDomain();

    // Is the domain allowed?
    // No domain, let it pass.
    if (empty($domain)) {
      return AccessResult::allowed()
        ->addCacheTags([
        'url.site',
      ]);
    }

    // Active domain, let it pass.
    if ($domain
      ->status()) {
      return AccessResult::allowed()
        ->addCacheTags([
        'url.site',
      ]);
    }
    else {
      $permissions = [
        'administer domains',
        'access inactive domains',
      ];
      $operator = 'OR';
      return AccessResult::allowedIfHasPermissions($account, $permissions, $operator)
        ->addCacheTags([
        'url.site',
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainAccessCheck::$configFactory protected property The config factory.
DomainAccessCheck::$domainNegotiator protected property The Domain negotiator.
DomainAccessCheck::$pathMatcher protected property The path matcher service.
DomainAccessCheck::access public function
DomainAccessCheck::applies public function Declares whether the access check applies to a specific route or not. Overrides AccessCheckInterface::applies
DomainAccessCheck::checkPath public function
DomainAccessCheck::__construct public function Constructs the object.