3 namespace Drupal\permissions_by_entity\EventSubscriber;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\StringTranslation\TranslationInterface;
7 use Drupal\permissions_by_entity\Service\AccessCheckerInterface;
8 use Drupal\permissions_by_entity\Service\CheckedEntityCache;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
12 use Symfony\Component\HttpKernel\KernelEvents;
15 * Class PermissionsByEntityKernelEventSubscriber.
17 * @package Drupal\permissions_by_entity\EventSubscriber
19 class PermissionsByEntityKernelEventSubscriber implements EventSubscriberInterface {
24 * @var \Drupal\permissions_by_entity\Service\AccessCheckerInterface
26 private $accessChecker;
29 * The core string translator.
31 * @var \Drupal\Core\StringTranslation\TranslationInterface
36 * The cache for checked entities.
38 * @var \Drupal\permissions_by_entity\Service\CheckedEntityCache
40 private $checkedEntityCache;
43 * PermissionsByEntityKernelEventSubscriber constructor.
45 * @param \Drupal\permissions_by_entity\Service\AccessCheckerInterface $access_checker
46 * The service to check if the current user is allowed to access an entity.
47 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
48 * The core string translator.
49 * @param \Drupal\permissions_by_entity\Service\CheckedEntityCache $checked_entity_cache
50 * The cache for checked entities.
52 public function __construct(
53 AccessCheckerInterface $access_checker,
54 TranslationInterface $translation,
55 CheckedEntityCache $checked_entity_cache
57 $this->accessChecker = $access_checker;
58 $this->translation = $translation;
59 $this->checkedEntityCache = $checked_entity_cache;
65 public static function getSubscribedEvents() {
67 KernelEvents::REQUEST => ['onKernelRequest', 5],
72 * Callback method for the KernelEvents::REQUEST event.
74 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
77 public function onKernelRequest(GetResponseEvent $event) {
78 // Get the current request from the event.
79 $request = $event->getRequest();
82 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
84 if ($request->attributes->has('node')) {
85 $entity = $request->attributes->get('node');
87 elseif ($request->attributes->has('_entity')) {
88 $entity = $request->attributes->get('_entity');
91 // If there is no entity abort here.
96 // If we already checked this entity, we do nothing.
97 if ($this->checkedEntityCache->isChecked($entity)) {
101 // Add this entity to the cache.
102 $this->checkedEntityCache->add($entity);
105 // Check if the current user is allowed to access this entity.
107 $entity && $entity instanceof ContentEntityInterface &&
108 !$this->accessChecker->isAccessAllowed($entity)
111 // If the current user is not allowed to access this entity,
112 // we throw an AccessDeniedHttpException.
113 throw new AccessDeniedHttpException(
114 $this->translation->translate(
115 'You are not allowed to view content of this entity type.'