Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / UncacheableEntityAccessControlHandler.php
1 <?php
2
3 namespace Drupal\entity;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityPublishedInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Session\AccountInterface;
10
11 /**
12  * Controls access based on the uncacheable entity permissions.
13  *
14  * @see \Drupal\entity\UncacheableEntityPermissionProvider
15  *
16  * Note: this access control handler will cause pages to be cached per user.
17  */
18 class UncacheableEntityAccessControlHandler extends EntityAccessControlHandlerBase {
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'), UncacheableEntityPermissionProvider::class, TRUE)) {
27       throw new \Exception('\Drupal\entity\UncacheableEntityAccessControlHandler requires the \Drupal\entity\UncacheableEntityPermissionProvider permission provider.');
28     }
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function checkEntityOwnerPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
35     /** @var \Drupal\user\EntityOwnerInterface $entity */
36     if ($operation === 'view' && $entity instanceof EntityPublishedInterface && !$entity->isPublished()) {
37       if ($account->id() != $entity->getOwnerId()) {
38         // There's no permission for viewing other user's unpublished entity.
39         return AccessResult::neutral()->cachePerUser();
40       }
41
42       $permissions = [
43         "view own unpublished {$entity->getEntityTypeId()}",
44       ];
45       $result = AccessResult::allowedIfHasPermissions($account, $permissions)->cachePerUser();
46     }
47     else {
48       $result = parent::checkEntityOwnerPermissions($entity, $operation, $account);
49     }
50
51     return $result;
52   }
53
54 }