You are here

public function DomainAccessManager::hasDomainPermissions in Domain Access 8

Checks that a user belongs to the domain and has a set of permissions.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user account.

\Drupal\domain\DomainInterface $domain: The domain being checked.

array $permissions: The relevant permissions to check.

string $conjunction: The conjunction AND|OR to use when checking permissions.

Return value

bool Returns TRUE if the user is assigned to the domain and has the necessary permissions.

Overrides DomainAccessManagerInterface::hasDomainPermissions

File

domain_access/src/DomainAccessManager.php, line 135

Class

DomainAccessManager
Checks the access status of entities based on domain settings.

Namespace

Drupal\domain_access

Code

public function hasDomainPermissions(AccountInterface $account, DomainInterface $domain, array $permissions, $conjunction = 'AND') {

  // Assume no access.
  $access = FALSE;

  // In the case of multiple AND permissions, assume access and then deny if
  // any check fails.
  if ($conjunction == 'AND' && !empty($permissions)) {
    $access = TRUE;
    foreach ($permissions as $permission) {
      if (!($permission_access = $account
        ->hasPermission($permission))) {
        $access = FALSE;
        break;
      }
    }
  }
  else {
    foreach ($permissions as $permission) {
      if ($permission_access = $account
        ->hasPermission($permission)) {
        $access = TRUE;
        break;
      }
    }
  }

  // Validate that the user is assigned to the domain. If not, deny.
  $user = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->load($account
    ->id());
  $allowed = $this
    ->getAccessValues($user);
  if (!isset($allowed[$domain
    ->id()]) && empty($this
    ->getAllValue($user))) {
    $access = FALSE;
  }
  return $access;
}