src/EventSubscriber/UserCheckerSubscriber.php line 29
<?phpnamespace App\EventSubscriber;use Doctrine\Persistence\ManagerRegistry;use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpKernel\KernelEvents;use Symfony\Bundle\SecurityBundle\Security;class UserCheckerSubscriber implements EventSubscriberInterface {protected $router;protected $security;protected $authorizationChecker;protected $managerRegistry;public function __construct(RouterInterface $router, Security $security, AuthorizationCheckerInterface $authorizationChecker, ManagerRegistry $managerRegistry) {$this->router = $router;$this->security = $security;$this->authorizationChecker = $authorizationChecker;$this->managerRegistry = $managerRegistry;}public function onKernelResponse(ResponseEvent $event) {$currentRoute = $event->getRequest()->get('_route');if (str_starts_with($currentRoute, 'api-')) return;if ($this->authorizationChecker->isGranted('ROLE_CUSTOMER') && $this->authorizationChecker->isGranted('ROLE_NO_PROFILE') && $currentRoute !== 'customer_profile_register') :return $event->setResponse(new RedirectResponse($this->router->generate('customer_profile_register')));endif;if ($this->authorizationChecker->isGranted('ROLE_CUSTOMER') && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) :if ($event->getRequest()->getSession()->get('username', '') != '') :$customer = $this->managerRegistry->getRepository(\App\Entity\Customer::class)->findOneBy(['user' => $this->security->getUser()]);if ($customer) :$event->getRequest()->getSession()->set('username', $customer->getUsername());$event->getRequest()->getSession()->set('usertype', 'customer');$event->getRequest()->getSession()->set('userurl', $this->router->generate('customer_default',[], \Symfony\Component\Routing\RouterInterface::ABSOLUTE_URL));else :$event->getRequest()->getSession()->set('username', '');$event->getRequest()->getSession()->set('usertype', '');$event->getRequest()->getSession()->set('userurl', '');endif;endif;$notifications = $this->managerRegistry->getRepository(\App\Entity\Notification::class)->findBy(['user' => $this->security->getUser()], ['createdAt' => 'DESC'], 5);$notifyArray = [];foreach ($notifications as $notify) :$notifyArray[] = ['id' => $notify->getID(), 'message' => $notify->getMessage(), 'type' => $notify->getType()];endforeach;$event->getRequest()->getSession()->set('notifications', $notifyArray);endif;if ($this->authorizationChecker->isGranted('ROLE_WRITER') && $this->authorizationChecker->isGranted('ROLE_NO_PROFILE') && $currentRoute !== 'writer_profile_register') :return $event->setResponse(new RedirectResponse($this->router->generate('writer_profile_register')));endif;if ($this->authorizationChecker->isGranted('ROLE_WRITER') && !$this->authorizationChecker->isGranted('ROLE_ADMIN')) :if ($event->getRequest()->getSession()->get('username', '') != '') :$writer = $this->managerRegistry->getRepository(\App\Entity\Writer::class)->findOneBy(['user' => $this->security->getUser()]);if ($writer) :$event->getRequest()->getSession()->set('username', $writer->getUsername());$event->getRequest()->getSession()->set('usertype', 'writer');$event->getRequest()->getSession()->set('userurl', $this->router->generate('writer_default',[], \Symfony\Component\Routing\RouterInterface::ABSOLUTE_URL));else :$event->getRequest()->getSession()->set('username', '');$event->getRequest()->getSession()->set('usertype', '');$event->getRequest()->getSession()->set('userurl', '');endif;endif;$notifications = $this->managerRegistry->getRepository(\App\Entity\Notification::class)->findBy(['user' => $this->security->getUser()], ['createdAt' => 'DESC'], 5);$notifyArray = [];foreach ($notifications as $notify) :$notifyArray[] = ['id' => $notify->getID(), 'message' => $notify->getMessage(), 'type' => $notify->getType()];endforeach;$event->getRequest()->getSession()->set('notifications', $notifyArray);endif;if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) :$event->getRequest()->getSession()->set('username', '');$event->getRequest()->getSession()->set('usertype', '');$event->getRequest()->getSession()->set('userurl', '');$event->getRequest()->getSession()->set('notifications', []);endif;}public static function getSubscribedEvents(): array{return [// must be registered before (i.e. with a higher priority than) the default Locale listenerKernelEvents::RESPONSE => [['onKernelResponse', -10]],];}}