src/EventListener/SecuritySubscriber.php line 56

Open in your IDE?
  1. <?php
  2. /*
  3.  * Listens to security related events like log-ins, failed logins, etc,
  4.  * and sends them to ThisData.
  5.  *
  6.  */
  7. namespace App\EventListener;
  8. use App\Entity\Admin;
  9. use App\Entity\HistoriqueConnexion;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Security\Http\SecurityEvents;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\Security\Core\AuthenticationEvents;  
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;  
  17. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;  
  18. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  19. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;  
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;  
  21. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;  
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. class SecuritySubscriber implements EventSubscriberInterface  
  24. {
  25.     private $em;
  26.     private $tokenStorage;
  27.     private $authenticationUtils;
  28.     private $session;
  29.     private $router;
  30.     private $flashBag;
  31.     public function __construct(EntityManagerInterface $emTokenStorageInterface $tokenStorageAuthenticationUtils $authenticationUtilsSessionInterface $sessionUrlGeneratorInterface $routerFlashBagInterface $flashBag,  RequestStack $requestStack)
  32.     {
  33.         $this->em $em;
  34.         $this->tokenStorage $tokenStorage;
  35.         $this->authenticationUtils $authenticationUtils;
  36.         $this->session $session;
  37.         $this->router $router;
  38.         $this->flashBag $flashBag;
  39.         $this->requestStack $requestStack;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return array(
  44.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  45.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  46.         );
  47.     }
  48.     public function onAuthenticationFailureAuthenticationFailureEvent $event )
  49.     {
  50.         // $username = $this->authenticationUtils->getLastUsername();
  51.         // $existingUser = $this->em->getRepository(Admin::class)->findOneBy(['email' => $username]);
  52.         // if($existingUser){
  53.         //     if($existingUser instanceof \App\Entity\Admin){
  54.         //         $request = $event->getRequest();
  55.         //         $historique = new HistoriqueConnexion();
  56.         //         $historique->setAdmin($existingUser);
  57.         //         $historique->setDateConnexion(new \DateTime());
  58.         //         $historique->setCommentaire("Echec tentative connexion");
  59.         //         $historique->setIp($request->getClientIp());
  60.         //         $this->em->persist($historique);
  61.         //         $this->em->flush();
  62.         //     }
  63.         // }
  64.     }
  65.     public function onSecurityInteractiveLoginInteractiveLoginEvent $event )
  66.     {
  67.         $user $this->tokenStorage->getToken()->getUser();
  68.         if($user instanceof \App\Entity\Admin){
  69.             $request $event->getRequest();
  70.             $historique = new HistoriqueConnexion();
  71.             $historique->setAdmin($user);
  72.             $historique->setDateConnexion(new \DateTime());
  73.             $historique->setCommentaire("Connexion RĂ©ussite");
  74.             $historique->setIp($request->getClientIp());
  75.             $historique->setOnline(true);
  76.             $this->em->persist($historique);
  77.             $this->em->flush();
  78.         }
  79.     }
  80. }