BlockAccessControlHandler.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace Drupal\block;
  3. use Drupal\Component\Plugin\Exception\ContextException;
  4. use Drupal\Component\Plugin\Exception\MissingValueContextException;
  5. use Drupal\Core\Access\AccessResult;
  6. use Drupal\Core\Cache\Cache;
  7. use Drupal\Core\Cache\CacheableDependencyInterface;
  8. use Drupal\Core\Condition\ConditionAccessResolverTrait;
  9. use Drupal\Core\Entity\EntityAccessControlHandler;
  10. use Drupal\Core\Entity\EntityHandlerInterface;
  11. use Drupal\Core\Entity\EntityInterface;
  12. use Drupal\Core\Entity\EntityTypeInterface;
  13. use Drupal\Core\Plugin\Context\ContextHandlerInterface;
  14. use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
  15. use Drupal\Core\Plugin\ContextAwarePluginInterface;
  16. use Drupal\Core\Session\AccountInterface;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. /**
  19. * Defines the access control handler for the block entity type.
  20. *
  21. * @see \Drupal\block\Entity\Block
  22. */
  23. class BlockAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
  24. use ConditionAccessResolverTrait;
  25. /**
  26. * The plugin context handler.
  27. *
  28. * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
  29. */
  30. protected $contextHandler;
  31. /**
  32. * The context manager service.
  33. *
  34. * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
  35. */
  36. protected $contextRepository;
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  41. return new static(
  42. $entity_type,
  43. $container->get('context.handler'),
  44. $container->get('context.repository')
  45. );
  46. }
  47. /**
  48. * Constructs the block access control handler instance
  49. *
  50. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  51. * The entity type definition.
  52. * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
  53. * The ContextHandler for applying contexts to conditions properly.
  54. * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
  55. * The lazy context repository service.
  56. */
  57. public function __construct(EntityTypeInterface $entity_type, ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository) {
  58. parent::__construct($entity_type);
  59. $this->contextHandler = $context_handler;
  60. $this->contextRepository = $context_repository;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  66. /** @var \Drupal\block\BlockInterface $entity */
  67. if ($operation != 'view') {
  68. return parent::checkAccess($entity, $operation, $account);
  69. }
  70. // Don't grant access to disabled blocks.
  71. if (!$entity->status()) {
  72. return AccessResult::forbidden()->addCacheableDependency($entity);
  73. }
  74. else {
  75. $conditions = [];
  76. $missing_context = FALSE;
  77. $missing_value = FALSE;
  78. foreach ($entity->getVisibilityConditions() as $condition_id => $condition) {
  79. if ($condition instanceof ContextAwarePluginInterface) {
  80. try {
  81. $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
  82. $this->contextHandler->applyContextMapping($condition, $contexts);
  83. }
  84. catch (MissingValueContextException $e) {
  85. $missing_value = TRUE;
  86. }
  87. catch (ContextException $e) {
  88. $missing_context = TRUE;
  89. }
  90. }
  91. $conditions[$condition_id] = $condition;
  92. }
  93. if ($missing_context) {
  94. // If any context is missing then we might be missing cacheable
  95. // metadata, and don't know based on what conditions the block is
  96. // accessible or not. Make sure the result cannot be cached.
  97. $access = AccessResult::forbidden()->setCacheMaxAge(0);
  98. }
  99. elseif ($missing_value) {
  100. // The contexts exist but have no value. Deny access without
  101. // disabling caching. For example the node type condition will have a
  102. // missing context on any non-node route like the frontpage.
  103. $access = AccessResult::forbidden();
  104. }
  105. elseif ($this->resolveConditions($conditions, 'and') !== FALSE) {
  106. // Delegate to the plugin.
  107. $block_plugin = $entity->getPlugin();
  108. try {
  109. if ($block_plugin instanceof ContextAwarePluginInterface) {
  110. $contexts = $this->contextRepository->getRuntimeContexts(array_values($block_plugin->getContextMapping()));
  111. $this->contextHandler->applyContextMapping($block_plugin, $contexts);
  112. }
  113. $access = $block_plugin->access($account, TRUE);
  114. }
  115. catch (MissingValueContextException $e) {
  116. // The contexts exist but have no value. Deny access without
  117. // disabling caching.
  118. $access = AccessResult::forbidden();
  119. }
  120. catch (ContextException $e) {
  121. // If any context is missing then we might be missing cacheable
  122. // metadata, and don't know based on what conditions the block is
  123. // accessible or not. Make sure the result cannot be cached.
  124. $access = AccessResult::forbidden()->setCacheMaxAge(0);
  125. }
  126. }
  127. else {
  128. $access = AccessResult::forbidden();
  129. }
  130. $this->mergeCacheabilityFromConditions($access, $conditions);
  131. // Ensure that access is evaluated again when the block changes.
  132. return $access->addCacheableDependency($entity);
  133. }
  134. }
  135. /**
  136. * Merges cacheable metadata from conditions onto the access result object.
  137. *
  138. * @param \Drupal\Core\Access\AccessResult $access
  139. * The access result object.
  140. * @param \Drupal\Core\Condition\ConditionInterface[] $conditions
  141. * List of visibility conditions.
  142. */
  143. protected function mergeCacheabilityFromConditions(AccessResult $access, array $conditions) {
  144. foreach ($conditions as $condition) {
  145. if ($condition instanceof CacheableDependencyInterface) {
  146. $access->addCacheTags($condition->getCacheTags());
  147. $access->addCacheContexts($condition->getCacheContexts());
  148. $access->setCacheMaxAge(Cache::mergeMaxAges($access->getCacheMaxAge(), $condition->getCacheMaxAge()));
  149. }
  150. }
  151. }
  152. }