You are here

class AccessAwareRouter in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Routing/AccessAwareRouter.php \Drupal\Core\Routing\AccessAwareRouter

A router class for Drupal with access check and upcasting.

Hierarchy

Expanded class hierarchy of AccessAwareRouter

1 file declares its use of AccessAwareRouter
AccessAwareRouterTest.php in core/tests/Drupal/Tests/Core/Routing/AccessAwareRouterTest.php
Contains \Drupal\Tests\Core\Routing\AccessAwareRouterTest.
1 string reference to 'AccessAwareRouter'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses AccessAwareRouter
router in core/core.services.yml
Drupal\Core\Routing\AccessAwareRouter

File

core/lib/Drupal/Core/Routing/AccessAwareRouter.php, line 20
Contains \Drupal\Core\Routing\AccessAwareRouter.

Namespace

Drupal\Core\Routing
View source
class AccessAwareRouter implements AccessAwareRouterInterface {

  /**
   * The chain router doing the actual routing.
   *
   * @var \Symfony\Cmf\Component\Routing\ChainRouter
   */
  protected $chainRouter;

  /**
   * The access manager.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * The account to use in access checks.
   *
   * @var \Drupal\Core\Session\AccountInterface;
   */
  protected $account;

  /**
   * Constructs a router for Drupal with access check and upcasting.
   *
   * @param \Symfony\Cmf\Component\Routing\ChainRouter $chain_router
   *   The chain router doing the actual routing.
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
   *   The access manager.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to use in access checks.
   */
  public function __construct(ChainRouter $chain_router, AccessManagerInterface $access_manager, AccountInterface $account) {
    $this->chainRouter = $chain_router;
    $this->accessManager = $access_manager;
    $this->account = $account;
  }

  /**
   * {@inheritdoc}
   */
  public function __call($name, $arguments) {

    // Ensure to call every other function to the chained router.
    // @todo Sadly does the ChainRouter not implement an interface in CMF.
    return call_user_func_array([
      $this->chainRouter,
      $name,
    ], $arguments);
  }

  /**
   * {@inheritdoc}
   */
  public function setContext(SymfonyRequestContext $context) {
    $this->chainRouter
      ->setContext($context);
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    return $this->chainRouter
      ->getContext();
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   Thrown when access checking failed.
   */
  public function matchRequest(Request $request) {
    $parameters = $this->chainRouter
      ->matchRequest($request);
    $request->attributes
      ->add($parameters);
    $this
      ->checkAccess($request);

    // We can not return $parameters because the access check can change the
    // request attributes.
    return $request->attributes
      ->all();
  }

  /**
   * Apply access check service to the route and parameters in the request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request to access check.
   */
  protected function checkAccess(Request $request) {

    // The cacheability (if any) of this request's access check result must be
    // applied to the response.
    $access_result = $this->accessManager
      ->checkRequest($request, $this->account, TRUE);

    // Allow a master request to set the access result for a subrequest: if an
    // access result attribute is already set, don't overwrite it.
    if (!$request->attributes
      ->has(AccessAwareRouterInterface::ACCESS_RESULT)) {
      $request->attributes
        ->set(AccessAwareRouterInterface::ACCESS_RESULT, $access_result);
    }
    if (!$access_result
      ->isAllowed()) {
      throw new AccessDeniedHttpException();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteCollection() {
    return $this->chainRouter
      ->getRouteCollection();
  }

  /**
   * {@inheritdoc}
   */
  public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) {
    return $this->chainRouter
      ->generate($name, $parameters, $referenceType);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   Thrown when access checking failed.
   */
  public function match($pathinfo) {
    return $this
      ->matchRequest(Request::create($pathinfo));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessAwareRouter::$accessManager protected property The access manager.
AccessAwareRouter::$account protected property The account to use in access checks.
AccessAwareRouter::$chainRouter protected property The chain router doing the actual routing.
AccessAwareRouter::checkAccess protected function Apply access check service to the route and parameters in the request.
AccessAwareRouter::generate public function Generates a URL or path for a specific route based on the given parameters. Overrides UrlGeneratorInterface::generate
AccessAwareRouter::getContext public function Gets the request context. Overrides RequestContextAwareInterface::getContext
AccessAwareRouter::getRouteCollection public function Gets the RouteCollection instance associated with this Router. Overrides RouterInterface::getRouteCollection
AccessAwareRouter::match public function Overrides AccessAwareRouterInterface::match
AccessAwareRouter::matchRequest public function Overrides AccessAwareRouterInterface::matchRequest
AccessAwareRouter::setContext public function Sets the request context. Overrides RequestContextAwareInterface::setContext
AccessAwareRouter::__call public function
AccessAwareRouter::__construct public function Constructs a router for Drupal with access check and upcasting.
AccessAwareRouterInterface::ACCESS_RESULT constant Attribute name of the access result for the request..
UrlGeneratorInterface::ABSOLUTE_PATH constant Generates an absolute path, e.g. "/dir/file".
UrlGeneratorInterface::ABSOLUTE_URL constant Generates an absolute URL, e.g. "http://example.com/dir/file".
UrlGeneratorInterface::NETWORK_PATH constant Generates a network path, e.g. "//example.com/dir/file". Such reference reuses the current scheme but specifies the host.
UrlGeneratorInterface::RELATIVE_PATH constant Generates a relative path based on the current request path, e.g. "../parent-file".