Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / src / BlockContentAccessControlHandler.php
1 <?php
2
3 namespace Drupal\block_content;
4
5 use Drupal\block_content\Access\DependentAccessInterface;
6 use Drupal\block_content\Event\BlockContentGetDependencyEvent;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Entity\EntityHandlerInterface;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Entity\EntityAccessControlHandler;
11 use Drupal\Core\Entity\EntityTypeInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16 /**
17  * Defines the access control handler for the custom block entity type.
18  *
19  * @see \Drupal\block_content\Entity\BlockContent
20  */
21 class BlockContentAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
22
23   /**
24    * The event dispatcher.
25    *
26    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
27    */
28   protected $eventDispatcher;
29
30   /**
31    * BlockContentAccessControlHandler constructor.
32    *
33    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
34    *   The entity type.
35    * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
36    *   The event dispatcher.
37    */
38   public function __construct(EntityTypeInterface $entity_type, EventDispatcherInterface $dispatcher) {
39     parent::__construct($entity_type);
40     $this->eventDispatcher = $dispatcher;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
47     return new static(
48       $entity_type,
49       $container->get('event_dispatcher')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
57     if ($operation === 'view') {
58       $access = AccessResult::allowedIf($entity->isPublished())
59         ->orIf(AccessResult::allowedIfHasPermission($account, 'administer blocks'));
60     }
61     else {
62       $access = parent::checkAccess($entity, $operation, $account);
63     }
64     // Add the entity as a cacheable dependency because access will at least be
65     // determined by whether the block is reusable.
66     $access->addCacheableDependency($entity);
67     /** @var \Drupal\block_content\BlockContentInterface $entity */
68     if ($entity->isReusable() === FALSE) {
69       if (!$entity instanceof DependentAccessInterface) {
70         throw new \LogicException("Non-reusable block entities must implement \Drupal\block_content\Access\DependentAccessInterface for access control.");
71       }
72       $dependency = $entity->getAccessDependency();
73       if (empty($dependency)) {
74         // If an access dependency has not been set let modules set one.
75         $event = new BlockContentGetDependencyEvent($entity);
76         $this->eventDispatcher->dispatch(BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY, $event);
77         $dependency = $event->getAccessDependency();
78         if (empty($dependency)) {
79           return AccessResult::forbidden("Non-reusable blocks must set an access dependency for access control.");
80         }
81       }
82       /** @var \Drupal\Core\Entity\EntityInterface $dependency */
83       $access = $access->andIf($dependency->access($operation, $account, TRUE));
84     }
85     return $access;
86   }
87
88 }