EntityTestAccessControlHandler.php in Drupal 8
File
core/modules/system/tests/modules/entity_test/src/EntityTestAccessControlHandler.php
View source
<?php
namespace Drupal\entity_test;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_test\Entity\EntityTestLabel;
class EntityTestAccessControlHandler extends EntityAccessControlHandler {
protected $viewLabelOperation = TRUE;
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($entity
->label() == 'forbid_access') {
return AccessResult::forbidden();
}
if ($operation === 'view label' && $entity instanceof EntityTestLabel) {
return AccessResult::allowed();
}
elseif (in_array($operation, [
'view',
'view label',
])) {
if (!$entity
->isDefaultTranslation()) {
return AccessResult::allowedIfHasPermission($account, 'view test entity translations');
}
return AccessResult::allowedIfHasPermission($account, 'view test entity');
}
elseif (in_array($operation, [
'update',
'delete',
])) {
return AccessResult::allowedIfHasPermission($account, 'administer entity_test content');
}
return AccessResult::neutral();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermissions($account, [
'administer entity_test content',
'administer entity_test_with_bundle content',
'create ' . $entity_bundle . ' entity_test_with_bundle entities',
], 'OR');
}
}