src/EventListener/UserListener.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Event\UserConnectWithoutADIdEvent;
  4. use App\Security\ADTokenException;
  5. use App\Security\UnknownUserAccessException;
  6. use Doctrine\ORM\EntityManager;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\Routing\RouterInterface;
  10. class UserListener
  11. {
  12.     public function __construct(private readonly EntityManager $em, private readonly RouterInterface $router)
  13.     {
  14.     }
  15.     public function onUserConnectWithoutADId(UserConnectWithoutADIdEvent $event): void
  16.     {
  17.         $user $event->getUser();
  18.         $payload $event->getPayload();
  19.         $user->setAdId($payload['adId']);
  20.         $user->setCompanyId($payload['assignedEntityId']);
  21.         $this->em->flush($user);
  22.     }
  23.     public function onUnknownUserAccess(ExceptionEvent $event): void
  24.     {
  25.         $exception $event->getThrowable();
  26.         if (!($exception instanceof UnknownUserAccessException)) {
  27.             return;
  28.         }
  29.         $response = new RedirectResponse($this->router->generate('fos_user_registration_register_pro'));
  30.         $event->setResponse($response);
  31.     }
  32.     public function onADTokenExpired(ExceptionEvent $event): void
  33.     {
  34.         $exception $event->getThrowable();
  35.         if (!($exception instanceof ADTokenException)) {
  36.             return;
  37.         }
  38.         $response = new RedirectResponse($this->router->generate('homepage_pro'));
  39.         $event->setResponse($response);
  40.     }
  41. }