You are here

class DomainAccessViewsAccess in Domain Access 8

Class DomainAccessViewsAccess.

@package Drupal\domain_access\Access

Hierarchy

Expanded class hierarchy of DomainAccessViewsAccess

1 string reference to 'DomainAccessViewsAccess'
domain_access.services.yml in domain_access/domain_access.services.yml
domain_access/domain_access.services.yml
1 service uses DomainAccessViewsAccess
access_check.domain_access_views in domain_access/domain_access.services.yml
Drupal\domain_access\Access\DomainAccessViewsAccess

File

domain_access/src/Access/DomainAccessViewsAccess.php, line 17

Namespace

Drupal\domain_access\Access
View source
class DomainAccessViewsAccess implements AccessCheckInterface {

  /**
   * The key used by the routing requirement.
   *
   * @var string
   */
  protected $requirementsKey = '_domain_access_views';

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The Domain storage handler.
   *
   * @var \Drupal\domain\DomainStorageInterface
   */
  protected $domainStorage;

  /**
   * User storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * The Domain access manager.
   *
   * @var \Drupal\domain_access\DomainAccessManagerInterface
   */
  protected $manager;

  /**
   * Constructs a DomainToken object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\domain_access\DomainAccessManagerInterface $manager
   *   The domain access manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, DomainAccessManagerInterface $manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->domainStorage = $this->entityTypeManager
      ->getStorage('domain');
    $this->userStorage = $this->entityTypeManager
      ->getStorage('user');
    $this->manager = $manager;
  }

  /**
   * {@inheritdoc}
   */
  public function access(Route $route, AccountInterface $account, $arg_0 = NULL) {

    // Permissions are stored on the route defaults.
    $permission = $route
      ->getDefault('domain_permission');
    $allPermission = $route
      ->getDefault('domain_all_permission');

    // Users with this permission can see any domain content lists, and it is
    // required to view all affiliates.
    if ($account
      ->hasPermission($allPermission)) {
      return AccessResult::allowed();
    }

    // Load the domain from the passed argument. In testing, this passed NULL
    // in some instances.
    if (!is_null($arg_0)) {
      $domain = $this->domainStorage
        ->load($arg_0);
    }

    // Domain found, check user permissions.
    if (!empty($domain)) {
      if ($this->manager
        ->hasDomainPermissions($account, $domain, [
        $permission,
      ])) {
        return AccessResult::allowed();
      }
    }
    return AccessResult::forbidden();
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
DomainAccessViewsAccess::$domainStorage protected property The Domain storage handler.
DomainAccessViewsAccess::$entityTypeManager protected property The entity type manager.
DomainAccessViewsAccess::$manager protected property The Domain access manager.
DomainAccessViewsAccess::$requirementsKey protected property The key used by the routing requirement.
DomainAccessViewsAccess::$userStorage protected property User storage.
DomainAccessViewsAccess::access public function
DomainAccessViewsAccess::applies public function Declares whether the access check applies to a specific route or not. Overrides AccessCheckInterface::applies
DomainAccessViewsAccess::__construct public function Constructs a DomainToken object.