class AccessSubscriber

Access subscriber for controller requests.

Hierarchy

Expanded class hierarchy of AccessSubscriber

File

drupal/core/lib/Drupal/Core/EventSubscriber/AccessSubscriber.php, line 20
Contains Drupal\Core\EventSubscriber\AccessSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class AccessSubscriber implements EventSubscriberInterface {

  /**
   * Constructs a new AccessSubscriber.
   *
   * @param \Drupal\Core\Access\AccessManager $access_manager
   *   The access check manager that will be responsible for applying
   *   AccessCheckers against routes.
   */
  public function __construct(AccessManager $access_manager) {
    $this->accessManager = $access_manager;
  }

  /**
   * Verifies that the current user can access the requested path.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function onKernelRequestAccessCheck(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    if (!$request->attributes
      ->has('_route')) {

      // If no Route is available it is likely a static resource and access is
      // handled elsewhere.
      return;
    }
    $this->accessManager
      ->check($request->attributes
      ->get('_route'));
  }

  /**
   * Apply access checks to routes.
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The event to process.
   */
  public function onRoutingRouteAlterSetAccessCheck(RouteBuildEvent $event) {
    $this->accessManager
      ->setChecks($event
      ->getRouteCollection());
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array(
      'onKernelRequestAccessCheck',
      30,
    );

    // Setting very low priority to ensure access checks are run after alters.
    $events[RoutingEvents::ALTER][] = array(
      'onRoutingRouteAlterSetAccessCheck',
      0,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessSubscriber::getSubscribedEvents static function Registers the methods in this class that should be listeners. Overrides EventSubscriberInterface::getSubscribedEvents
AccessSubscriber::onKernelRequestAccessCheck public function Verifies that the current user can access the requested path.
AccessSubscriber::onRoutingRouteAlterSetAccessCheck public function Apply access checks to routes.
AccessSubscriber::__construct public function Constructs a new AccessSubscriber.