You are here

protected function TermAccessControlHandler::checkAccess in Drupal 9

Same name and namespace in other branches
  1. 8 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'.");
    default:

      // No opinion.
      return AccessResult::neutral()
        ->cachePerPermissions();
  }
}