You are here

protected function QueryAccessHandlerBase::buildEntityConditions in Entity API 8

Builds the conditions for entities that do not have an owner.

Parameters

string $operation: The access operation. Usually one of "view", "update", "duplicate", or "delete".

\Drupal\Core\Session\AccountInterface $account: The user for which to restrict access.

Return value

\Drupal\entity\QueryAccess\ConditionGroup|null The conditions, or NULL if the user doesn't have access to any entity.

2 calls to QueryAccessHandlerBase::buildEntityConditions()
QueryAccessHandler::buildEntityOwnerConditions in src/QueryAccess/QueryAccessHandler.php
Builds the conditions for entities that have an owner.
QueryAccessHandlerBase::buildConditions in src/QueryAccess/QueryAccessHandlerBase.php
Builds the conditions for the given operation and user.

File

src/QueryAccess/QueryAccessHandlerBase.php, line 254

Class

QueryAccessHandlerBase
Provides common logic for query access handlers.

Namespace

Drupal\entity\QueryAccess

Code

protected function buildEntityConditions($operation, AccountInterface $account) {
  $entity_type_id = $this->entityType
    ->id();
  $bundle_key = $this->entityType
    ->getKey('bundle');
  $conditions = new ConditionGroup('OR');
  $conditions
    ->addCacheContexts([
    'user.permissions',
  ]);

  // The $entity_type permission.
  if ($account
    ->hasPermission("{$operation} {$entity_type_id}")) {

    // The user has full access, no conditions needed.
    return $conditions;
  }
  $bundles = array_keys($this->bundleInfo
    ->getBundleInfo($entity_type_id));
  $bundles_with_any_permission = [];
  foreach ($bundles as $bundle) {
    if ($account
      ->hasPermission("{$operation} {$bundle} {$entity_type_id}")) {
      $bundles_with_any_permission[] = $bundle;
    }
  }

  // The $bundle permission.
  if ($bundles_with_any_permission) {
    $conditions
      ->addCondition($bundle_key, $bundles_with_any_permission);
  }
  return $conditions
    ->count() ? $conditions : NULL;
}