function TermAccessControlHandler::checkAccess

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/TermAccessControlHandler.php \Drupal\taxonomy\TermAccessControlHandler::checkAccess()
  2. 8.9.x core/modules/taxonomy/src/TermAccessControlHandler.php \Drupal\taxonomy\TermAccessControlHandler::checkAccess()
  3. 11.x core/modules/taxonomy/src/TermAccessControlHandler.php \Drupal\taxonomy\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 EntityAccessControlHandler::checkAccess

File

core/modules/taxonomy/src/TermAccessControlHandler.php, line 20

Class

TermAccessControlHandler
Defines the access control handler for the taxonomy term entity type.

Namespace

Drupal\taxonomy

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($account->hasPermission('administer taxonomy')) {
    return AccessResult::allowed()->cachePerPermissions();
  }
  switch ($operation) {
    case 'view':
      $access_result = AccessResult::allowedIf($account->hasPermission('access content') && $entity->isPublished())
        ->cachePerPermissions()
        ->addCacheableDependency($entity);
      if (!$access_result->isAllowed()) {
        $access_result->setReason("The 'access content' permission is required and the taxonomy term must be published.");
      }
      return $access_result;
    case 'update':
      if ($account->hasPermission("edit terms in {$entity->bundle()}")) {
        return AccessResult::allowed()->cachePerPermissions();
      }
      return AccessResult::neutral()->setReason("The following permissions are required: 'edit terms in {$entity->bundle()}' OR 'administer taxonomy'.");
    case 'delete':
      if ($account->hasPermission("delete terms in {$entity->bundle()}")) {
        return AccessResult::allowed()->cachePerPermissions();
      }
      return AccessResult::neutral()->setReason("The following permissions are required: 'delete terms in {$entity->bundle()}' OR 'administer taxonomy'.");
    case 'view revision':
    case 'view all revisions':
      if ($account->hasPermission("view term revisions in {$entity->bundle()}") || $account->hasPermission("view all taxonomy revisions")) {
        return AccessResult::allowed()->cachePerPermissions();
      }
      return AccessResult::neutral()->setReason("The following permissions are required: 'view revisions in {$entity->bundle()}' OR 'view all taxonomy revisions'.");
    case 'revert':
      if ($account->hasPermission("revert term revisions in {$entity->bundle()}") && $account->hasPermission("edit terms in {$entity->bundle()}") || $account->hasPermission("revert all taxonomy revisions")) {
        return AccessResult::allowed()->cachePerPermissions();
      }
      return AccessResult::neutral()->setReason("The following permissions are required: 'revert term revisions in {$entity->bundle()}' OR 'revert all taxonomy revisions'.");
    case 'delete revision':
      if ($account->hasPermission("delete term revisions in {$entity->bundle()}") && $account->hasPermission("delete terms in {$entity->bundle()}") || $account->hasPermission("delete all taxonomy revisions")) {
        return AccessResult::allowed()->cachePerPermissions();
      }
      return AccessResult::neutral()->setReason("The following permissions are required: 'delete term revisions in {$entity->bundle()}' OR 'delete all taxonomy revisions'.");
    default:
      // No opinion.
      return AccessResult::neutral()->cachePerPermissions();
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.