You are here

public function ReusableBlocksController::access in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/Controller/ReusableBlocksController.php \Drupal\gutenberg\Controller\ReusableBlocksController::access()

Controller routes access callback.

Parameters

\Drupal\Core\Session\AccountInterface $account: Current user.

string $block_id: Block id from route parameter.

Return value

\Drupal\Core\Access\AccessResult Allowed access result if all conditions are met.

1 string reference to 'ReusableBlocksController::access'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/ReusableBlocksController.php, line 156

Class

ReusableBlocksController
Returns responses for our blocks routes.

Namespace

Drupal\gutenberg\Controller

Code

public function access(AccountInterface $account, $block_id) {
  $block = BlockContent::load($block_id);

  // Check if the user has the proper permissions.
  $access = AccessResult::allowedIfHasPermission($account, 'use gutenberg');
  if ($access
    ->isAllowed()) {

    // Only throw the 404 if the user is allowed to access the route.
    // Avoids anonymous users scanning for a block's existence.
    if (!$block) {
      throw new NotFoundHttpException();
    }
  }

  // Check if it's a reusable block.
  $access = $access
    ->andIf(AccessResult::allowedIf($block && $block
    ->bundle() === 'reusable_block'));

  // Add it as a cache dependency.
  $access
    ->addCacheableDependency($block);
  return $access;
}