vendor/roothirsch/core-bundle/UserAware/UserDetection.php line 69

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\UserAware;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Psr\Http\Message\RequestInterface;
  6. use Roothirsch\CoreBundle\Entity\User;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
  13. class UserDetection implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var ObjectManager
  17.      */
  18.     protected $entityManager;
  19.     /**
  20.      * @var RouterInterface
  21.      */
  22.     private $router;
  23.     /**
  24.      * @var TokenStorageInterface
  25.      */
  26.     private $tokenStorage;
  27.     public function __construct(
  28.         EntityManagerInterface $entityManager,
  29.         RouterInterface $router,
  30.         TokenStorageInterface $tokenStorage
  31.     ) {
  32.         $this->entityManager $entityManager;
  33.         $this->router $router;
  34.         $this->tokenStorage $tokenStorage;
  35.     }
  36.     /**
  37.      * Returns an array of event names this subscriber wants to listen to.
  38.      *
  39.      * The array keys are event names and the value can be:
  40.      *
  41.      *  * The method name to call (priority defaults to 0)
  42.      *  * An array composed of the method name to call and the priority
  43.      *  * An array of arrays composed of the method names to call and respective
  44.      *    priorities, or 0 if unset
  45.      *
  46.      * For instance:
  47.      *
  48.      *  * array('eventName' => 'methodName')
  49.      *  * array('eventName' => array('methodName', $priority))
  50.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  51.      *
  52.      * @return array The event names to listen to
  53.      */
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             KernelEvents::REQUEST => ['onKernelRequest'EventPriorities::PRE_READ],
  58.         ];
  59.     }
  60.     public function onKernelRequest(\Symfony\Component\HttpKernel\Event\RequestEvent $event)
  61.     {
  62.         if ($this->router->getContext()->getPathInfo() === '/login') {
  63.             return;
  64.         }
  65.         $filter $this->entityManager->getFilters()->enable('user_aware_filter');
  66.         if ($this->tokenStorage->getToken() && $this->tokenStorage->getToken()->getUser() instanceof User) {
  67.             $filter->setUser($this->tokenStorage->getToken()->getUser());
  68.             $filter->setRequest($event->getRequest());
  69.         }
  70.     }
  71. }