5d643ac366ba40ff95529b7f109ab1e905893afb
[yaffs-website] / web / modules / contrib / permissions_by_term / modules / permissions_by_entity / src / EventSubscriber / PermissionsByEntityKernelEventSubscriber.php
1 <?php
2
3 namespace Drupal\permissions_by_entity\EventSubscriber;
4
5 use Drupal\Core\Entity\FieldableEntityInterface;
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;
13
14 /**
15  * Class PermissionsByEntityKernelEventSubscriber.
16  *
17  * @package Drupal\permissions_by_entity\EventSubscriber
18  */
19 class PermissionsByEntityKernelEventSubscriber implements EventSubscriberInterface {
20
21   /**
22    * The access checker.
23    *
24    * @var \Drupal\permissions_by_entity\Service\AccessCheckerInterface
25    */
26   private $accessChecker;
27
28   /**
29    * The core string translator.
30    *
31    * @var \Drupal\Core\StringTranslation\TranslationInterface
32    */
33   private $translation;
34
35   /**
36    * The cache for checked entities.
37    *
38    * @var \Drupal\permissions_by_entity\Service\CheckedEntityCache
39    */
40   private $checkedEntityCache;
41
42   /**
43    * PermissionsByEntityKernelEventSubscriber constructor.
44    *
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.
51    */
52   public function __construct(
53     AccessCheckerInterface $access_checker,
54     TranslationInterface $translation,
55     CheckedEntityCache $checked_entity_cache
56   ) {
57     $this->accessChecker = $access_checker;
58     $this->translation = $translation;
59     $this->checkedEntityCache = $checked_entity_cache;
60   }
61
62   /**
63    * {@inheritdoc}
64    *
65    * @see DynamicPageCacheSubscriber
66    *
67    * This is required to run before the DynamicPageCacheSubscriber as otherwise
68    * the response would be cached which can lead to false access.
69    */
70   public static function getSubscribedEvents() {
71     return [
72       KernelEvents::REQUEST => ['onKernelRequest', 28],
73     ];
74   }
75
76   /**
77    * Callback method for the KernelEvents::REQUEST event.
78    *
79    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
80    *   The event instance.
81    */
82   public function onKernelRequest(GetResponseEvent $event) {
83     // Get the current request from the event.
84     $request = $event->getRequest();
85
86     // Get the entity.
87     /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
88     $entity = NULL;
89     if ($request->attributes->has('node')) {
90       $entity = $request->attributes->get('node');
91     }
92     elseif ($request->attributes->has('_entity')) {
93       $entity = $request->attributes->get('_entity');
94     }
95
96     // If there is no entity abort here.
97     if (!$entity instanceof FieldableEntityInterface) {
98       return;
99     }
100
101     // If we already checked this entity, we do nothing.
102     if ($this->checkedEntityCache->isChecked($entity)) {
103       return;
104     }
105
106     // Add this entity to the cache.
107     $this->checkedEntityCache->add($entity);
108
109     // Check if the current user is allowed to access this entity.
110     if (
111       $entity && $entity instanceof FieldableEntityInterface &&
112       !$this->accessChecker->isAccessAllowed($entity)
113     ) {
114
115       // If the current user is not allowed to access this entity,
116       // we throw an AccessDeniedHttpException.
117       throw new AccessDeniedHttpException(
118         $this->translation->translate(
119           'You are not allowed to view content of this entity type.'
120         )
121       );
122     }
123   }
124
125 }