src/EventListener/AuthenticationListener.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\EntityManager;
  5. use Psr\Log\LoggerInterface;
  6. use App\Entity\Auth\LoginAttempt;
  7. use App\Entity\User;
  8. use App\Repository\Auth\LoginAttemptRepository;
  9. use App\Service\Auth\LoginAttemptService;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Event\AuthenticationEvent;
  17. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  18. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  22. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  23. /**
  24.  * Class AuthenticationListener
  25.  * @see https://www.nomisoft.co.uk/articles/symfony-fail2ban-ip-blocking
  26.  * @see https://packagist.org/packages/anyx/login-gate-bundle#0.5
  27.  */
  28. class AuthenticationListener // implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
  29. {
  30.     /** @var null|ContainerInterface */
  31.     private $container null;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     private $em;
  36.     /**
  37.      * @var LoggerInterface
  38.      */
  39.     private $logger;
  40.     /**
  41.      * @var RequestStack
  42.      */
  43.     private $requestStack;
  44.     /**
  45.      * @var Request
  46.      */
  47.     private $request;
  48.     /**
  49.      * @var LoginAttemptService
  50.      */
  51.     private $loginAttemptService;
  52.     private $token;
  53.     /**
  54.      * @param ContainerInterface $container
  55.      * @param EntityManagerInterface $em
  56.      * @param LoggerInterface $logger
  57.      * @param RequestStack $request
  58.      */
  59.     public function __construct(ContainerInterface $containerEntityManagerInterface $emRequestStack $requestStackLoggerInterface $loggerLoginAttemptService $loginAttemptService)
  60.     {
  61.         $this->container $container;
  62.         $this->em $em;
  63.         $this->requestStack $requestStack;
  64.         $this->request $requestStack->getCurrentRequest();
  65.         $this->logger $logger;
  66.         $this->loginAttemptService $loginAttemptService;
  67.     }
  68.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  69.     {
  70.         $this->token $event->getAuthenticationToken();
  71.         // $request = $event->getRequest();
  72.         // $this->onAuthenticationSuccess($request, $token);
  73.         // $this->onAuthenticationFailure($request, null);
  74.         $canLogin $this->loginAttemptService->canLogin($this->request);
  75.         // dump(@compact('canLogin'));
  76.         $this->logger->info(sprintf("canLogin = %d"$canLogin));
  77.     }
  78.     public function onAuthenticationSuccess(AuthenticationEvent $event// , Request $request, TokenInterface $token
  79.     {
  80.         $this->token $event->getAuthenticationToken();
  81.         if ($this->token && $this->token->getUser() instanceof UserInterface) {
  82.             $loginAttempt = new LoginAttempt();
  83.             $loginAttempt->setUser($this->token->getUser());
  84.             $loginAttempt->setIpAddress($this->request->getClientIp());
  85.             $loginAttempt->setUsername($this->request->get('_username'));
  86.             $this->em->persist($loginAttempt);
  87.             $this->em->flush();
  88.             // $this->loginAttemptService->clearCountAttempts($this->request, $this->token->getUser());
  89.             $this->loginAttemptService->clearCountAttempts($this->request$this->token->getUser()->getUsername());
  90.             return new RedirectResponse($this->container->get('router')->generate('home_page'));
  91.         }
  92.         if ($this->request->headers->get('referer')) {
  93.             return new RedirectResponse($this->request->headers->get('referer'));
  94.         }
  95.         return new RedirectResponse($this->container->get('router')->generate('app_login'));
  96.     }
  97.     /**
  98.      * {@inheritdoc}
  99.      */
  100.     public function onAuthenticationFailure(AuthenticationFailureEvent $event// Request $request, AuthenticationException $exception
  101.     {
  102.         $exception $event->getAuthenticationException();
  103.         $data = [
  104.             'exception' => $exception->getMessage(),
  105.             'clientIp'  => $this->request->getClientIp(),
  106.             'sessionId' => $this->request->getSession()->getId()
  107.         ];
  108.         $ipAddress $this->request->getClientIp();
  109.         $this->logger->error('Authentication failed for IP: ' $ipAddress);
  110.         $this->loginAttemptService->addAttemptByUsername($this->request->get('_username')== null ?"" $this->request->get('_username'), $data);
  111.         return new RedirectResponse($this->container->get('router')->generate('app_login'));
  112.     }
  113.     
  114. }