src/EventSubscriber/UserSubscriber.php line 148

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\AppConstants;
  4. use App\Entity\Inscription;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  12. use Symfony\Component\Security\Http\Event\LoginFailureEvent;
  13. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Security\Http\Event\LogoutEvent;
  19. class UserSubscriber implements EventSubscriberInterface
  20. {
  21.     private ParameterBagInterface $parameterBag;
  22.     private EntityManagerInterface $em;
  23.     private UrlGeneratorInterface $urlGenerator;
  24.     public function __construct(ParameterBagInterface $parameterBagEntityManagerInterface $emUrlGeneratorInterface $urlGenerator)
  25.     {
  26.         $this->parameterBag $parameterBag;
  27.         $this->em $em;
  28.         $this->urlGenerator $urlGenerator;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             // KernelEvents::REQUEST => 'onKernelRequest',
  34.             // KernelEvents::RESPONSE => 'onKernelResponse',
  35.             InteractiveLoginEvent::class => 'onInteractiveLogin',
  36.             // LoginSuccessEvent::class => 'onLoginSuccess',
  37.             // LoginFailureEvent::class => 'onFailureSuccess',
  38.             LogoutEvent::class => 'onLogout'
  39.         ];
  40.     }
  41.     public function onInteractiveLogin(InteractiveLoginEvent $event): void
  42.     {
  43.         /** @var User $user */
  44.         $user null;
  45.         if ($event->getAuthenticationToken()) {
  46.             $user $event->getAuthenticationToken()->getUser();
  47.         }
  48.         /** @var User $user */
  49.         if ($user) {
  50.             $user->setLastLoginAt(new \DateTime());
  51.             $this->em->persist($user);
  52.             $this->em->flush();
  53.             /** @var SessionInterface $session */
  54.             $session null;
  55.             if ($user->hasRole(User::ROLE_MEMBER) && $event->getRequest() && $session $event->getRequest()->getSession()) {
  56.                 $inscription_reference null;
  57.                 if ($session->has(AppConstants::SESSION_INSCRIPTION_REFERENCE) && $inscription_reference $session->get(AppConstants::SESSION_INSCRIPTION_REFERENCE)) {
  58.                     if ($inscription_reference) {
  59.                         /** @var Inscription $inscription */
  60.                         $inscription $this->em->getRepository(Inscription::class)->findOneBy(['reference' => $inscription_reference]);
  61.                         if ($inscription && empty($inscription->getUser())) {
  62.                             $inscription->setUser($user);
  63.                             $this->em->persist($inscription);
  64.                             $this->em->flush();
  65.                             $session->remove(AppConstants::SESSION_INSCRIPTION_REFERENCE);
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.         if (function_exists('dump')) :
  72.         // dump(@compact('event', 'user'));
  73.         endif;
  74.     }
  75.     /*
  76.     public function onLoginSuccess(LoginSuccessEvent $event)
  77.     {
  78.         / ** @var User $user * /
  79.         $user = $event->getUser();
  80.         $request = $event->getRequest();
  81.         $session = $request->getSession();
  82.         if ($user->hasRole(User::ROLE_MEMBER)) {
  83.             // dd(@compact('request'));
  84.             $response = $event->getResponse();
  85.             if ($request->attributes->get('_route') === 'app_user_login') {
  86.                 if ($session && $session->has(AppConstants::SESSION_INSCRIPTION_REFERENCE) && $session->get(AppConstants::SESSION_INSCRIPTION_REFERENCE)) {
  87.                     $response = $this->guardHandler
  88.                         ->authenticateUserAndHandleSuccess(
  89.                             $user,
  90.                             $event->getRequest(),
  91.                             $this->authenticator,
  92.                             // $event->getAuthenticator(),
  93.                             'member_area'
  94.                         );
  95.                 }
  96.             }
  97.             // $event->setResponse($response);
  98.         }
  99.         $this->em->flush();
  100.         //dd($user);
  101.     }
  102.     */
  103.     /*
  104.     public function onFailureSuccess(LoginFailureEvent $event)
  105.     {
  106.         $request = $event->getRequest();
  107.     }
  108.     */
  109.     /*
  110.     public function onKernelRequest(RequestEvent $event): void
  111.     {
  112.         if (!$event->isMainRequest()) {
  113.             // don't do anything if it's not the main request
  114.             return;
  115.         }
  116.         if (function_exists('dump')) :
  117.             // dump(@compact('event'));
  118.         endif;
  119.     }
  120.     public function onKernelResponse(ResponseEvent $event): void
  121.     {
  122.         if (function_exists('dump')) :
  123.             // dump(@compact('event'));
  124.         endif;
  125.     }
  126.     */
  127.     public function onLogout(LogoutEvent $event): void
  128.     {
  129.         // get the security token of the session that is about to be logged out
  130.         // $token = $event->getToken();
  131.         // get the current request
  132.         $request $event->getRequest();
  133.         if ($session $request->getSession()) {
  134.             if ($session->has(AppConstants::SESSION_EVENEMENT_REFERENCE)) {
  135.                 $session->remove(AppConstants::SESSION_EVENEMENT_REFERENCE);
  136.             }
  137.             if ($session->has(AppConstants::SESSION_INSCRIPTION_REFERENCE)) {
  138.                 $session->remove(AppConstants::SESSION_INSCRIPTION_REFERENCE);
  139.             }
  140.         }
  141.         // get the current response, if it is already set by another listener
  142.         $response $event->getResponse();
  143.         // configure a custom logout response to the homepage
  144.         $response = new RedirectResponse(
  145.             $this->urlGenerator->generate('app_user_login'),
  146.             RedirectResponse::HTTP_SEE_OTHER
  147.         );
  148.         $event->setResponse($response);
  149.     }
  150. }