You are here

function permissions_by_entity_entity_access in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 modules/permissions_by_entity/permissions_by_entity.module \permissions_by_entity_entity_access()

Implements hook_entity_access().

2 calls to permissions_by_entity_entity_access()
EntityPublicationTest::testAnonymousCannotViewUnpublishedNodesWithoutTermPermissions in modules/permissions_by_entity/tests/src/Kernel/EntityPublicationTest.php
Unpublished nodes without restrictions should not be visible to anonymous users.
EntityPublicationTest::testAnonymousCanViewPublishedNodesWithoutTermPermissions in modules/permissions_by_entity/tests/src/Kernel/EntityPublicationTest.php
Published nodes without restrictions should be visible to anonymous users.

File

modules/permissions_by_entity/permissions_by_entity.module, line 16
Module file for Permission by Entity.

Code

function permissions_by_entity_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {

  /**
   * @var \Drupal\permissions_by_term\Cache\AccessResultCache $cache
   */
  $cache = \Drupal::service('permissions_by_term.access_result_cache');
  $accessResult = AccessResult::neutral();

  /** @var \Drupal\permissions_by_entity\Service\AccessChecker $accessChecker */
  if ($operation === 'view' && $entity instanceof FieldableEntityInterface && !$entity
    ->isNew()) {
    if ($cache
      ->hasAccessResultsCache($account
      ->id(), $entity
      ->id())) {
      return $cache
        ->getAccessResultsCache($account
        ->id(), $entity
        ->id());
    }
    $accessChecker = \Drupal::service('permissions_by_entity.access_checker');

    // Check if the entity is even using term based access control.
    if ($accessChecker
      ->isAccessControlled($entity)) {

      // Do not just return a neutral result if access allowed by the module.
      $accessResult = $accessChecker
        ->isAccessAllowed($entity, $account
        ->id()) ? AccessResult::allowed() : AccessResult::forbidden('Access revoked by permissions_by_entity module.');
    }
    $cache
      ->setAccessResultsCache($account
      ->id(), $entity
      ->id(), $accessResult);
  }
  return $accessResult;
}