EntityTestAccessControlHandler.php in Drupal 10
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\Entity\EntityPublishedInterface;
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()) {
if ($entity instanceof EntityPublishedInterface && !$entity
->isPublished()) {
return AccessResult::allowedIfHasPermission($account, 'view unpublished test entity translations');
}
else {
return AccessResult::allowedIfHasPermission($account, 'view test entity translations');
}
}
return AccessResult::allowedIfHasPermission($account, 'view test entity');
}
elseif (in_array($operation, [
'update',
'delete',
])) {
$access = AccessResult::allowedIfHasPermission($account, 'administer entity_test content');
if (!$access
->isAllowed() && $operation === 'update' && $account
->hasPermission('edit own entity_test content')) {
$access = $access
->orIf(AccessResult::allowedIf($entity
->getOwnerId() === $account
->id()))
->cachePerUser()
->addCacheableDependency($entity);
}
return $access;
}
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');
}
}