function ContactAccessControlHandler::checkAccess

Same name in other branches
  1. 3.x modules/content_entity_example/src/ContactAccessControlHandler.php \Drupal\content_entity_example\ContactAccessControlHandler::checkAccess()

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

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();
}