LayoutSectionAccessCheck.php 1.28 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

namespace Drupal\layout_builder\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
9
use Drupal\layout_builder\SectionStorageInterface;
10 11 12 13 14 15 16 17 18

/**
 * Provides an access check for the Layout Builder UI.
 *
 * @internal
 */
class LayoutSectionAccessCheck implements AccessInterface {

  /**
19
   * Checks routing access to the layout.
20 21 22 23 24 25 26 27 28 29
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(RouteMatchInterface $route_match, AccountInterface $account) {
30
    $section_storage = $route_match->getParameter('section_storage');
31

32
    if (empty($section_storage)) {
33 34 35
      return AccessResult::forbidden()->addCacheContexts(['route']);
    }

36
    if (!$section_storage instanceof SectionStorageInterface) {
37 38 39 40 41 42
      $access = AccessResult::forbidden();
    }
    else {
      $access = AccessResult::allowedIfHasPermission($account, 'configure any layout');
    }

43
    return $access->addCacheableDependency($section_storage);
44 45 46
  }

}