comparison core/modules/block_content/src/BlockContentAccessControlHandler.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php 1 <?php
2 2
3 namespace Drupal\block_content; 3 namespace Drupal\block_content;
4 4
5 use Drupal\block_content\Access\DependentAccessInterface;
6 use Drupal\block_content\Event\BlockContentGetDependencyEvent;
5 use Drupal\Core\Access\AccessResult; 7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Entity\EntityHandlerInterface;
6 use Drupal\Core\Entity\EntityInterface; 9 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityAccessControlHandler; 10 use Drupal\Core\Entity\EntityAccessControlHandler;
11 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Session\AccountInterface; 12 use Drupal\Core\Session\AccountInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9 15
10 /** 16 /**
11 * Defines the access control handler for the custom block entity type. 17 * Defines the access control handler for the custom block entity type.
12 * 18 *
13 * @see \Drupal\block_content\Entity\BlockContent 19 * @see \Drupal\block_content\Entity\BlockContent
14 */ 20 */
15 class BlockContentAccessControlHandler extends EntityAccessControlHandler { 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 }
16 52
17 /** 53 /**
18 * {@inheritdoc} 54 * {@inheritdoc}
19 */ 55 */
20 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { 56 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
21 if ($operation === 'view') { 57 if ($operation === 'view') {
22 return AccessResult::allowedIf($entity->isPublished())->addCacheableDependency($entity) 58 $access = AccessResult::allowedIf($entity->isPublished())
23 ->orIf(AccessResult::allowedIfHasPermission($account, 'administer blocks')); 59 ->orIf(AccessResult::allowedIfHasPermission($account, 'administer blocks'));
24 } 60 }
25 return parent::checkAccess($entity, $operation, $account); 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;
26 } 86 }
27 87
28 } 88 }