/core/modules/block/src/BlockAccessControlHandler.php

https://gitlab.com/reasonat/test8 · PHP · 171 lines · 91 code · 17 blank · 63 comment · 9 complexity · 64d6d07e4d21276bfd737ae782a5941a MD5 · raw file

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