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