3 namespace Drupal\entity;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityAccessControlHandler as CoreEntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\user\EntityOwnerInterface;
14 class EntityAccessControlHandlerBase extends CoreEntityAccessControlHandler {
19 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
20 $account = $this->prepareUser($account);
21 /** @var \Drupal\Core\Access\AccessResult $result */
22 $result = parent::checkAccess($entity, $operation, $account);
24 if ($result->isNeutral()) {
25 if ($entity instanceof EntityOwnerInterface) {
26 $result = $this->checkEntityOwnerPermissions($entity, $operation, $account);
29 $result = $this->checkEntityPermissions($entity, $operation, $account);
33 // Ensure that access is evaluated again when the entity changes.
34 return $result->addCacheableDependency($entity);
38 * Checks the entity operation and bundle permissions.
40 * @param \Drupal\Core\Entity\EntityInterface $entity
41 * The entity for which to check access.
42 * @param string $operation
43 * The entity operation. Usually one of 'view', 'view label', 'update' or
45 * @param \Drupal\Core\Session\AccountInterface $account
46 * The user for which to check access.
48 * @return \Drupal\Core\Access\AccessResultInterface
51 protected function checkEntityPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
53 "$operation {$entity->getEntityTypeId()}",
54 "$operation {$entity->bundle()} {$entity->getEntityTypeId()}",
57 return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
61 * Checks the entity operation and bundle permissions, with owners.
63 * @param \Drupal\Core\Entity\EntityInterface $entity
64 * The entity for which to check access.
65 * @param string $operation
66 * The entity operation. Usually one of 'view', 'view label', 'update' or
68 * @param \Drupal\Core\Session\AccountInterface $account
69 * The user for which to check access.
71 * @return \Drupal\Core\Access\AccessResultInterface
74 protected function checkEntityOwnerPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
75 /** @var \Drupal\user\EntityOwnerInterface $entity */
76 if ($account->id() == $entity->getOwnerId()) {
78 "$operation own {$entity->getEntityTypeId()}",
79 "$operation any {$entity->getEntityTypeId()}",
80 "$operation own {$entity->bundle()} {$entity->getEntityTypeId()}",
81 "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
86 "$operation any {$entity->getEntityTypeId()}",
87 "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
90 $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR')->cachePerUser();
98 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
99 $result = parent::checkCreateAccess($account, $context, $entity_bundle);
100 if ($result->isNeutral()) {
102 'administer ' . $this->entityTypeId,
103 'create ' . $this->entityTypeId,
105 if ($entity_bundle) {
106 $permissions[] = 'create ' . $entity_bundle . ' ' . $this->entityTypeId;
109 $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');