setAccessDependency example



    // Set block access dependency even if we are not checking access on     // this level. The block itself may render another     // RefinableDependentAccessInterface object and need to pass on this value.     if ($block instanceof RefinableDependentAccessInterface) {
      $contexts = $event->getContexts();
      if (isset($contexts['layout_builder.entity'])) {
        if ($entity = $contexts['layout_builder.entity']->getContextValue()) {
          if ($event->inPreview()) {
            // If previewing in Layout Builder allow access.             $block->setAccessDependency(new LayoutPreviewAccessAllowed());
          }
          else {
            $block->setAccessDependency($entity);
          }
        }
      }
    }

    // Only check access if the component is not being previewed.     if ($event->inPreview()) {
      $access = AccessResult::allowed()->setCacheMaxAge(0);
    }

  public function testOnBuildRender($refinable_dependent_access) {
    $contexts = [];
    if ($refinable_dependent_access) {
      $block = $this->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class)->willImplement(PreviewFallbackInterface::class);
      $layout_entity = $this->prophesize(EntityInterface::class);
      $layout_entity = $layout_entity->reveal();
      $context = $this->prophesize(ContextInterface::class);
      $context->getContextValue()->willReturn($layout_entity);
      $contexts['layout_builder.entity'] = $context->reveal();

      $block->setAccessDependency($layout_entity)->shouldBeCalled();
    }
    else {
      $block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class);
    }
    $access_result = AccessResult::allowed();
    $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
    $block->getCacheContexts()->willReturn([]);
    $block->getCacheTags()->willReturn(['test']);
    $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
    $block->getConfiguration()->willReturn([]);
    $block->getPluginId()->willReturn('block_plugin_id');
    

    $user->addRole($this->role->id());
    $user->save();

    if ($parent_access !== NULL) {
      $parent_entity = $this->prophesize(AccessibleInterface::class);
      $expected_parent_result = new ($parent_access)();
      $parent_entity->access($operation$user, TRUE)
        ->willReturn($expected_parent_result)
        ->shouldBeCalled();

      $this->blockEntity->setAccessDependency($parent_entity->reveal());

    }
    $this->blockEntity->save();

    // Reload a previous revision.     if ($loadRevisionId !== NULL) {
      $this->blockEntity = $entityStorage->loadRevision($loadRevisionId);
    }

    $result = $this->accessControlHandler->access($this->blockEntity, $operation$user, TRUE);
    $this->assertInstanceOf($expected_access$result);
    
elseif (!empty($this->configuration['block_revision_id'])) {
        $entity = $this->entityTypeManager->getStorage('block_content')->loadRevision($this->configuration['block_revision_id']);
        $this->blockContent = $entity;
      }
      else {
        $this->blockContent = $this->entityTypeManager->getStorage('block_content')->create([
          'type' => $this->getDerivativeId(),
          'reusable' => FALSE,
        ]);
      }
      if ($this->blockContent instanceof RefinableDependentAccessInterface && $dependee = $this->getAccessDependency()) {
        $this->blockContent->setAccessDependency($dependee);
      }
    }
    return $this->blockContent;
  }

  /** * {@inheritdoc} */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form$form_state);
    if ($this->isNew) {
      
/** * Tests that the previous dependency is replaced when using set. * * @covers ::setAccessDependency * * @dataProvider providerTestSetFirst */
  public function testSetAccessDependency($use_set_first) {
    $testRefinable = new RefinableDependentAccessTraitTestClass();

    if ($use_set_first) {
      $testRefinable->setAccessDependency($this->forbidden);
    }
    else {
      $testRefinable->addAccessDependency($this->forbidden);
    }
    $accessResult = $testRefinable->getAccessDependency()->access('view', $this->account, TRUE);
    $this->assertTrue($accessResult->isForbidden());
    $this->assertEquals('Because I said so', $accessResult->getReason());

    // Calling setAccessDependency() replaces the existing dependency.     $testRefinable->setAccessDependency($this->neutral);
    $dependency = $testRefinable->getAccessDependency();
    

  }

  /** * Handles the BlockContentEvents::INLINE_BLOCK_GET_DEPENDENCY event. * * @param \Drupal\block_content\Event\BlockContentGetDependencyEvent $event * The event. */
  public function onGetDependency(BlockContentGetDependencyEvent $event) {
    if ($dependency = $this->getInlineBlockDependency($event->getBlockContentEntity())) {
      $event->setAccessDependency($dependency);
    }
  }

  /** * Get the access dependency of an inline block. * * If the block is used in an entity that entity will be returned as the * dependency. * * For revisionable entities the entity will only be returned if it is used in * the latest revision of the entity. For inline blocks that are not used in * the latest revision but are used in a previous revision the entity will not * be returned because calling * \Drupal\Core\Access\AccessibleInterface::access() will only check access on * the latest revision. Therefore if the previous revision of the entity was * returned as the dependency access would be granted to inline block * regardless of whether the user has access to the revision in which the * inline block was used. * * @param \Drupal\block_content\BlockContentInterface $block_content * The block content entity. * * @return \Drupal\Core\Entity\EntityInterface|null * Returns the layout dependency. * * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess() * @see \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray::onBuildRender() */
Home | Imprint | This part of the site doesn't use cookies.