Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / EntityAccessControlHandlerBase.php
1 <?php
2
3 namespace Drupal\entity;
4
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;
10
11 /**
12  * @internal
13  */
14 class EntityAccessControlHandlerBase extends CoreEntityAccessControlHandler {
15
16   /**
17    * {@inheritdoc}
18    */
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);
23
24     if ($result->isNeutral()) {
25       if ($entity instanceof EntityOwnerInterface) {
26         $result = $this->checkEntityOwnerPermissions($entity, $operation, $account);
27       }
28       else {
29         $result = $this->checkEntityPermissions($entity, $operation, $account);
30       }
31     }
32
33     // Ensure that access is evaluated again when the entity changes.
34     return $result->addCacheableDependency($entity);
35   }
36
37   /**
38    * Checks the entity operation and bundle permissions.
39    *
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
44    *   'delete'.
45    * @param \Drupal\Core\Session\AccountInterface $account
46    *   The user for which to check access.
47    *
48    * @return \Drupal\Core\Access\AccessResultInterface
49    *   The access result.
50    */
51   protected function checkEntityPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
52     $permissions = [
53       "$operation {$entity->getEntityTypeId()}",
54       "$operation {$entity->bundle()} {$entity->getEntityTypeId()}",
55     ];
56
57     return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
58   }
59
60   /**
61    * Checks the entity operation and bundle permissions, with owners.
62    *
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
67    *   'delete'.
68    * @param \Drupal\Core\Session\AccountInterface $account
69    *   The user for which to check access.
70    *
71    * @return \Drupal\Core\Access\AccessResultInterface
72    *   The access result.
73    */
74   protected function checkEntityOwnerPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
75     /** @var \Drupal\user\EntityOwnerInterface $entity */
76     if ($account->id() == $entity->getOwnerId()) {
77       $permissions = [
78         "$operation own {$entity->getEntityTypeId()}",
79         "$operation any {$entity->getEntityTypeId()}",
80         "$operation own {$entity->bundle()} {$entity->getEntityTypeId()}",
81         "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
82       ];
83     }
84     else {
85       $permissions = [
86         "$operation any {$entity->getEntityTypeId()}",
87         "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
88       ];
89     }
90     $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR')->cachePerUser();
91
92     return $result;
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
99     $result = parent::checkCreateAccess($account, $context, $entity_bundle);
100     if ($result->isNeutral()) {
101       $permissions = [
102         'administer ' . $this->entityTypeId,
103         'create ' . $this->entityTypeId,
104       ];
105       if ($entity_bundle) {
106         $permissions[] = 'create ' . $entity_bundle . ' ' . $this->entityTypeId;
107       }
108
109       $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
110     }
111
112     return $result;
113   }
114
115 }