You are here

protected function TermAccessControlHandler::checkAccess in Taxonomy access fix 8.3

Same name and namespace in other branches
  1. 8.2 src/TermAccessControlHandler.php \Drupal\taxonomy_access_fix\TermAccessControlHandler::checkAccess()

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

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

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides TermAccessControlHandler::checkAccess

File

src/TermAccessControlHandler.php, line 18

Class

TermAccessControlHandler
Extends Core's access control handler with a view permission by bundle.

Namespace

Drupal\taxonomy_access_fix

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\taxonomy\TermInterface $entity */
  if ($operation !== 'view') {
    return parent::checkAccess($entity, $operation, $account);
  }
  if ($account
    ->hasPermission('administer taxonomy')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  $access_result = AccessResult::allowedIfHasPermission($account, "view terms in {$entity->bundle()}")
    ->andIf(AccessResult::allowedIf($entity
    ->isPublished()))
    ->cachePerPermissions()
    ->addCacheableDependency($entity);
  if (!$access_result
    ->isAllowed()) {

    /** @var \Drupal\Core\Access\AccessResultReasonInterface $access_result */
    $access_result
      ->setReason("The 'view terms in {$entity->bundle()}' OR 'administer taxonomy' permission is required and the taxonomy term must be published.");
  }
  return $access_result;
}