Switching to production mode.
[yaffs-website] / web / modules / contrib / entity / src / EntityAccessControlHandler.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  * Controls access based on the generic entity permissions.
13  *
14  * @see \Drupal\entity\EntityPermissionProvider
15  */
16 class EntityAccessControlHandler extends CoreEntityAccessControlHandler {
17
18   /**
19    * {@inheritdoc}
20    */
21   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
22     $account = $this->prepareUser($account);
23     /** @var \Drupal\Core\Access\AccessResult $result */
24     $result = parent::checkAccess($entity, $operation, $account);
25
26     if ($result->isNeutral()) {
27       if ($entity instanceof EntityOwnerInterface) {
28         $result = $this->checkEntityOwnerPermissions($entity, $operation, $account);
29       }
30       else {
31         $result = $this->checkEntityPermissions($entity, $operation, $account);
32       }
33     }
34
35     // Ensure that access is evaluated again when the entity changes.
36     return $result->addCacheableDependency($entity);
37   }
38
39   /**
40    * Checks the entity operation and bundle permissions.
41    *
42    * @param \Drupal\Core\Entity\EntityInterface $entity
43    *   The entity for which to check access.
44    * @param string $operation
45    *   The entity operation. Usually one of 'view', 'view label', 'update' or
46    *   'delete'.
47    * @param \Drupal\Core\Session\AccountInterface $account
48    *   The user for which to check access.
49    *
50    * @return \Drupal\Core\Access\AccessResultInterface
51    *   The access result.
52    */
53   protected function checkEntityPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
54     return AccessResult::allowedIfHasPermissions($account, [
55       "$operation {$entity->getEntityTypeId()}",
56       "$operation {$entity->bundle()} {$entity->getEntityTypeId()}",
57     ], '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\Core\Entity\EntityInterface|\Drupal\user\EntityOwnerInterface $entity */
76     if (($account->id() == $entity->getOwnerId())) {
77       $result = AccessResult::allowedIfHasPermissions($account, [
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       ], 'OR');
83     }
84     else {
85       $result = AccessResult::allowedIfHasPermissions($account, [
86         "$operation any {$entity->getEntityTypeId()}",
87         "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
88       ], 'OR');
89     }
90
91     return $result->cachePerUser();
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
98     $result = parent::checkCreateAccess($account, $context, $entity_bundle);
99     if ($result->isNeutral()) {
100       $permissions = [
101         'administer ' . $this->entityTypeId,
102         'create ' . $this->entityTypeId,
103       ];
104       if ($entity_bundle) {
105         $permissions[] = 'create ' . $entity_bundle . ' ' . $this->entityTypeId;
106       }
107
108       $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
109     }
110
111     return $result;
112   }
113
114 }