src/EventSubscriber/UserCheckerSubscriber.php line 29

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  5. use Symfony\Component\Routing\RouterInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Bundle\SecurityBundle\Security;
  11. class UserCheckerSubscriber implements EventSubscriberInterface {
  12.     protected $router;
  13.     protected $security;
  14.     protected $authorizationChecker;
  15.     protected $managerRegistry;
  16.     public function __construct(RouterInterface $routerSecurity $securityAuthorizationCheckerInterface $authorizationCheckerManagerRegistry $managerRegistry) {
  17.         $this->router $router;
  18.         $this->security $security;
  19.         $this->authorizationChecker $authorizationChecker;
  20.         $this->managerRegistry $managerRegistry;
  21.     }
  22.     public function onKernelResponse(ResponseEvent $event) {
  23.         $currentRoute $event->getRequest()->get('_route');
  24.         if (str_starts_with($currentRoute'api-')) return;
  25.         if ($this->authorizationChecker->isGranted('ROLE_CUSTOMER') && $this->authorizationChecker->isGranted('ROLE_NO_PROFILE') && $currentRoute !== 'customer_profile_register') :
  26.             return $event->setResponse(new RedirectResponse($this->router->generate('customer_profile_register')));
  27.         endif;
  28.         if ($this->authorizationChecker->isGranted('ROLE_CUSTOMER') && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) :
  29.             if ($event->getRequest()->getSession()->get('username''') != '') :
  30.                 $customer $this->managerRegistry->getRepository(\App\Entity\Customer::class)->findOneBy(['user' => $this->security->getUser()]);
  31.                 if ($customer) :
  32.                     $event->getRequest()->getSession()->set('username'$customer->getUsername());
  33.                     $event->getRequest()->getSession()->set('usertype''customer');
  34.                     $event->getRequest()->getSession()->set('userurl'$this->router->generate('customer_default',[], \Symfony\Component\Routing\RouterInterface::ABSOLUTE_URL));
  35.                 else :
  36.                     $event->getRequest()->getSession()->set('username''');
  37.                     $event->getRequest()->getSession()->set('usertype''');
  38.                     $event->getRequest()->getSession()->set('userurl''');
  39.                 endif;
  40.             endif;
  41.             $notifications $this->managerRegistry->getRepository(\App\Entity\Notification::class)->findBy(['user' => $this->security->getUser()], ['createdAt' => 'DESC'], 5);
  42.             $notifyArray = [];
  43.             foreach ($notifications as $notify) :
  44.                 $notifyArray[] = ['id' => $notify->getID(), 'message' => $notify->getMessage(), 'type' => $notify->getType()];
  45.             endforeach;
  46.             $event->getRequest()->getSession()->set('notifications'$notifyArray);
  47.         endif;
  48.         if ($this->authorizationChecker->isGranted('ROLE_WRITER') && $this->authorizationChecker->isGranted('ROLE_NO_PROFILE') && $currentRoute !== 'writer_profile_register') :
  49.             return $event->setResponse(new RedirectResponse($this->router->generate('writer_profile_register')));
  50.         endif;
  51.         if ($this->authorizationChecker->isGranted('ROLE_WRITER') && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) :
  52.             if ($event->getRequest()->getSession()->get('username''') != '') :
  53.                 $writer $this->managerRegistry->getRepository(\App\Entity\Writer::class)->findOneBy(['user' => $this->security->getUser()]);
  54.                 if ($writer) :
  55.                     $event->getRequest()->getSession()->set('username'$writer->getUsername());
  56.                     $event->getRequest()->getSession()->set('usertype''writer');
  57.                     $event->getRequest()->getSession()->set('userurl'$this->router->generate('writer_default',[], \Symfony\Component\Routing\RouterInterface::ABSOLUTE_URL));
  58.                 else :
  59.                     $event->getRequest()->getSession()->set('username''');
  60.                     $event->getRequest()->getSession()->set('usertype''');
  61.                     $event->getRequest()->getSession()->set('userurl''');
  62.                 endif;
  63.             endif;
  64.             $notifications $this->managerRegistry->getRepository(\App\Entity\Notification::class)->findBy(['user' => $this->security->getUser()], ['createdAt' => 'DESC'], 5);
  65.             $notifyArray = [];
  66.             foreach ($notifications as $notify) :
  67.                 $notifyArray[] = ['id' => $notify->getID(), 'message' => $notify->getMessage(), 'type' => $notify->getType()];
  68.             endforeach;
  69.             $event->getRequest()->getSession()->set('notifications'$notifyArray);
  70.         endif;
  71.         if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) :
  72.             $event->getRequest()->getSession()->set('username''');
  73.             $event->getRequest()->getSession()->set('usertype''');
  74.             $event->getRequest()->getSession()->set('userurl''');
  75.             $event->getRequest()->getSession()->set('notifications', []);
  76.         endif;
  77.     }
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  82.             KernelEvents::RESPONSE => [['onKernelResponse', -10]],
  83.         ];
  84.     }
  85. }