You are here

class PermissionsByEntityKernelEventSubscriber in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 modules/permissions_by_entity/src/EventSubscriber/PermissionsByEntityKernelEventSubscriber.php \Drupal\permissions_by_entity\EventSubscriber\PermissionsByEntityKernelEventSubscriber

Class PermissionsByEntityKernelEventSubscriber.

@package Drupal\permissions_by_entity\EventSubscriber

Hierarchy

Expanded class hierarchy of PermissionsByEntityKernelEventSubscriber

1 string reference to 'PermissionsByEntityKernelEventSubscriber'
permissions_by_entity.services.yml in modules/permissions_by_entity/permissions_by_entity.services.yml
modules/permissions_by_entity/permissions_by_entity.services.yml
1 service uses PermissionsByEntityKernelEventSubscriber
permissions_by_entity.kernel_event_subscriber in modules/permissions_by_entity/permissions_by_entity.services.yml
Drupal\permissions_by_entity\EventSubscriber\PermissionsByEntityKernelEventSubscriber

File

modules/permissions_by_entity/src/EventSubscriber/PermissionsByEntityKernelEventSubscriber.php, line 20

Namespace

Drupal\permissions_by_entity\EventSubscriber
View source
class PermissionsByEntityKernelEventSubscriber implements EventSubscriberInterface {

  /**
   * The access checker.
   *
   * @var \Drupal\permissions_by_entity\Service\AccessCheckerInterface
   */
  private $accessChecker;

  /**
   * The core string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  private $translation;

  /**
   * The cache for checked entities.
   *
   * @var \Drupal\permissions_by_entity\Service\CheckedEntityCache
   */
  private $checkedEntityCache;

  /**
   * PermissionsByEntityKernelEventSubscriber constructor.
   *
   * @param \Drupal\permissions_by_entity\Service\AccessCheckerInterface $access_checker
   *   The service to check if the current user is allowed to access an entity.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The core string translator.
   * @param \Drupal\permissions_by_entity\Service\CheckedEntityCache $checked_entity_cache
   *   The cache for checked entities.
   */
  public function __construct(AccessCheckerInterface $access_checker, TranslationInterface $translation, CheckedEntityCache $checked_entity_cache) {
    $this->accessChecker = $access_checker;
    $this->translation = $translation;
    $this->checkedEntityCache = $checked_entity_cache;
  }

  /**
   * {@inheritdoc}
   *
   * @see DynamicPageCacheSubscriber
   *
   * This is required to run before the DynamicPageCacheSubscriber as otherwise
   * the response would be cached which can lead to false access.
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST => [
        'onKernelRequest',
        28,
      ],
    ];
  }

  /**
   * Callback method for the KernelEvents::REQUEST event.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event instance.
   */
  public function onKernelRequest(GetResponseEvent $event) {

    // Only act on the master request.
    if ($event
      ->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
      return;
    }

    // Get the current request from the event.
    $request = $event
      ->getRequest();

    // Get the entity.

    /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
    $entity = NULL;
    if ($request->attributes
      ->has('node')) {
      $entity = $request->attributes
        ->get('node');
    }
    elseif ($request->attributes
      ->has('_entity')) {
      $entity = $request->attributes
        ->get('_entity');
    }

    // If there is no entity abort here.
    if (!$entity instanceof FieldableEntityInterface) {
      return;
    }

    // If we already checked this entity, we do nothing.
    if ($this->checkedEntityCache
      ->isChecked($entity)) {
      return;
    }

    // Add this entity to the cache.
    $this->checkedEntityCache
      ->add($entity);

    // Check if the current user is allowed to access this entity.
    if ($entity && $entity instanceof FieldableEntityInterface && $this->accessChecker
      ->isAccessControlled($entity) && !$this->accessChecker
      ->isAccessAllowed($entity)) {

      // If the current user is not allowed to access this entity,
      // we throw an AccessDeniedHttpException.
      throw new AccessDeniedHttpException($this->translation
        ->translate('You are not allowed to view content of this entity type.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PermissionsByEntityKernelEventSubscriber::$accessChecker private property The access checker.
PermissionsByEntityKernelEventSubscriber::$checkedEntityCache private property The cache for checked entities.
PermissionsByEntityKernelEventSubscriber::$translation private property The core string translator.
PermissionsByEntityKernelEventSubscriber::getSubscribedEvents public static function This is required to run before the DynamicPageCacheSubscriber as otherwise the response would be cached which can lead to false access.
PermissionsByEntityKernelEventSubscriber::onKernelRequest public function Callback method for the KernelEvents::REQUEST event.
PermissionsByEntityKernelEventSubscriber::__construct public function PermissionsByEntityKernelEventSubscriber constructor.