function ContactAccessControlHandler::checkAccess

Link the activities to the permissions. checkAccess() is called with the $operation as defined in the routing.yml file.

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

modules/content_entity_example/src/ContactAccessControlHandler.php, line 21

Class

ContactAccessControlHandler
Access controller for the contact entity.

Namespace

Drupal\content_entity_example

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  // Check the admin_permission as defined in your @ContentEntityType
  // annotation.
  $admin_permission = $this->entityType
    ->getAdminPermission();
  if ($account->hasPermission($admin_permission)) {
    return AccessResult::allowed();
  }
  switch ($operation) {
    case 'view':
      return AccessResult::allowedIfHasPermission($account, 'view contact entity');
    case 'update':
      return AccessResult::allowedIfHasPermission($account, 'edit contact entity');
    case 'delete':
      return AccessResult::allowedIfHasPermission($account, 'delete contact entity');
  }
  return AccessResult::neutral();
}