3 namespace Drupal\entity;
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;
12 * Controls access based on the uncacheable entity permissions.
14 * @see \Drupal\entity\UncacheableEntityPermissionProvider
16 * Note: this access control handler will cause pages to be cached per user.
18 class UncacheableEntityAccessControlHandler extends EntityAccessControlHandlerBase {
23 public function __construct(EntityTypeInterface $entity_type) {
24 parent::__construct($entity_type);
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.');
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();
43 "view own unpublished {$entity->getEntityTypeId()}",
45 $result = AccessResult::allowedIfHasPermissions($account, $permissions)->cachePerUser();
48 $result = parent::checkEntityOwnerPermissions($entity, $operation, $account);