You are here

permissions_by_entity.module in Permissions by Term 8.2

Same filename and directory in other branches
  1. 8 modules/permissions_by_entity/permissions_by_entity.module

Module file for Permission by Entity.

File

modules/permissions_by_entity/permissions_by_entity.module
View source
<?php

/**
 * @file
 * Module file for Permission by Entity.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_entity_access().
 */
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;
}

Functions