Security update for permissions_by_term
[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\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;
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   public static function getSubscribedEvents() {
66     return [
67       KernelEvents::REQUEST => ['onKernelRequest', 5],
68     ];
69   }
70
71   /**
72    * Callback method for the KernelEvents::REQUEST event.
73    *
74    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
75    *   The event instance.
76    */
77   public function onKernelRequest(GetResponseEvent $event) {
78     // Get the current request from the event.
79     $request = $event->getRequest();
80
81     // Get the entity.
82     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
83     $entity = NULL;
84     if ($request->attributes->has('node')) {
85       $entity = $request->attributes->get('node');
86     }
87     elseif ($request->attributes->has('_entity')) {
88       $entity = $request->attributes->get('_entity');
89     }
90
91     // If there is no entity abort here.
92     if (!$entity) {
93       return;
94     }
95
96     // If we already checked this entity, we do nothing.
97     if ($this->checkedEntityCache->isChecked($entity)) {
98       return;
99     }
100     else {
101       // Add this entity to the cache.
102       $this->checkedEntityCache->add($entity);
103     }
104
105     // Check if the current user is allowed to access this entity.
106     if (
107       $entity && $entity instanceof ContentEntityInterface &&
108       !$this->accessChecker->isAccessAllowed($entity)
109     ) {
110
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.'
116         )
117       );
118     }
119   }
120
121 }