Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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\Entity\EntityPublishedInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\user\EntityOwnerInterface;
12
13 /**
14  * Controls access based on the generic entity permissions.
15  *
16  * @see \Drupal\entity\UncacheableEntityPermissionProvider
17  */
18 class EntityAccessControlHandler extends CoreEntityAccessControlHandler {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function __construct(EntityTypeInterface $entity_type) {
24     parent::__construct($entity_type);
25
26     if (!$entity_type->hasHandlerClass('permission_provider') || !is_a($entity_type->getHandlerClass('permission_provider'), EntityPermissionProvider::class, TRUE)) {
27       throw new \Exception("This entity access control handler requires the entity permissions provider: {EntityPermissionProvider::class}");
28     }
29   }
30
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
36     $account = $this->prepareUser($account);
37     /** @var \Drupal\Core\Access\AccessResult $result */
38     $result = parent::checkAccess($entity, $operation, $account);
39
40     if ($result->isNeutral()) {
41       if ($entity instanceof EntityOwnerInterface) {
42         $result = $this->checkEntityOwnerPermissions($entity, $operation, $account);
43       }
44       else {
45         $result = $this->checkEntityPermissions($entity, $operation, $account);
46       }
47     }
48
49     // Ensure that access is evaluated again when the entity changes.
50     return $result->addCacheableDependency($entity);
51   }
52
53   /**
54    * Checks the entity operation and bundle permissions.
55    *
56    * @param \Drupal\Core\Entity\EntityInterface $entity
57    *   The entity for which to check access.
58    * @param string $operation
59    *   The entity operation. Usually one of 'view', 'view label', 'update' or
60    *   'delete'.
61    * @param \Drupal\Core\Session\AccountInterface $account
62    *   The user for which to check access.
63    *
64    * @return \Drupal\Core\Access\AccessResultInterface
65    *   The access result.
66    */
67   protected function checkEntityPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
68     if ($operation === 'view') {
69       $permissions = [
70         "view {$entity->getEntityTypeId()}"
71       ];
72     }
73     else {
74       $permissions = [
75         "$operation {$entity->getEntityTypeId()}",
76         "$operation {$entity->bundle()} {$entity->getEntityTypeId()}",
77       ];
78     }
79     return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
80   }
81
82   /**
83    * Checks the entity operation and bundle permissions, with owners.
84    *
85    * @param \Drupal\Core\Entity\EntityInterface $entity
86    *   The entity for which to check access.
87    * @param string $operation
88    *   The entity operation. Usually one of 'view', 'view label', 'update' or
89    *   'delete'.
90    * @param \Drupal\Core\Session\AccountInterface $account
91    *   The user for which to check access.
92    *
93    * @return \Drupal\Core\Access\AccessResultInterface
94    *   The access result.
95    */
96   protected function checkEntityOwnerPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
97     if ($operation === 'view') {
98       if ($entity instanceof EntityPublishedInterface && !$entity->isPublished()) {
99         if (($account->id() == $entity->getOwnerId())) {
100           $permissions = [
101             "view own unpublished {$entity->getEntityTypeId()}",
102           ];
103           return AccessResult::allowedIfHasPermissions($account, $permissions)->cachePerUser();
104         }
105         return AccessResult::neutral()->cachePerUser();
106       }
107       else {
108         return AccessResult::allowedIfHasPermissions($account, [
109           "view {$entity->getEntityTypeId()}",
110         ]);
111       }
112     }
113     else {
114      if (($account->id() == $entity->getOwnerId())) {
115         $result = AccessResult::allowedIfHasPermissions($account, [
116           "$operation own {$entity->getEntityTypeId()}",
117           "$operation any {$entity->getEntityTypeId()}",
118           "$operation own {$entity->bundle()} {$entity->getEntityTypeId()}",
119           "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
120         ], 'OR');
121       }
122       else {
123         $result = AccessResult::allowedIfHasPermissions($account, [
124           "$operation any {$entity->getEntityTypeId()}",
125           "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
126         ], 'OR');
127       }
128       return $result;
129     }
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
136     $result = parent::checkCreateAccess($account, $context, $entity_bundle);
137     if ($result->isNeutral()) {
138       $permissions = [
139         'administer ' . $this->entityTypeId,
140         'create ' . $this->entityTypeId,
141       ];
142       if ($entity_bundle) {
143         $permissions[] = 'create ' . $entity_bundle . ' ' . $this->entityTypeId;
144       }
145
146       $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
147     }
148
149     return $result;
150   }
151
152 }