src/EventSubscriber/CurrentUserSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\EventSubscriber;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Harmonizely\Model\UserInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. final class CurrentUserSubscriber implements EventSubscriberInterface
  11. {
  12.     private $tokenStorage;
  13.     private $entityManager;
  14.     private const ROUTES_IGNORE_THIS_FILTER = [
  15.         'panel.api.event_type.list',
  16.         'panel.api.poll.create',
  17.         'panel.api.poll.list',
  18.         'panel.api.availability_rule',
  19.         'panel.api.poll.get',
  20.         'panel.api.event.edit',
  21.         'panel.api.event.no_show.list',
  22.         'panel.api.chart.list_status',
  23.         'panel.api.calendar_integration.calendar_account.list',
  24.         'panel.api.calendar_integration.calendar.default',
  25.         'panel.api.event.resend_notification',
  26.         'hrm_routing_form_index',
  27.         'api_organization_delete_member',
  28.         'panel.api.organization.leave',
  29.         'panel.organization.delete',
  30.         'api_organization_members_list',
  31.         'panel.api.zapier.event.mock',
  32.         'panel.api.routing_form.field.delete',
  33.         'panel.api.routing_form.create',
  34.         'panel.api.routing_form.field.create',
  35.         'panel.api.routing_form.route.create',
  36.         'panel.api.routing_form.delete',
  37.         'panel.api.routing_form.route.delete',
  38.         'panel.api.routing_form.get',
  39.         'panel.api.routing_form.route.get',
  40.         'panel.api.routing_form.list',
  41.         'panel.api.routing_form_result.list',
  42.         'panel.api.routing_form.update',
  43.         'panel.api.routing_form.field.update',
  44.         'panel.api.routing_form.route.update',
  45.     ];
  46.     public function __construct(EntityManagerInterface $entityManagerTokenStorageInterface $tokenStorage)
  47.     {
  48.         $this->entityManager $entityManager;
  49.         $this->tokenStorage $tokenStorage;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             KernelEvents::REQUEST => ['setCurrentUser'],
  58.         ];
  59.     }
  60.     public function setCurrentUser(RequestEvent $event)
  61.     {
  62.         if (!in_array($event->getRequest()->get('_route'), self::ROUTES_IGNORE_THIS_FILTER)) {
  63.             $token $this->tokenStorage->getToken();
  64.             if ($token && $token->getUser() instanceof UserInterface) {
  65.                 $this->entityManager
  66.                     ->getFilters()
  67.                     ->enable('current_user')
  68.                     ->setParameter('user_id'$token->getUser()->getId());
  69.             }
  70.         }
  71.     }
  72. }