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