src/Eccube/EventListener/LoginHistoryListener.php line 81

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\EventListener;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Entity\LoginHistory;
  15. use Eccube\Entity\Master\LoginHistoryStatus;
  16. use Eccube\Entity\Member;
  17. use Eccube\Repository\Master\LoginHistoryStatusRepository;
  18. use Eccube\Repository\MemberRepository;
  19. use Eccube\Request\Context;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\Security\Core\AuthenticationEvents;
  23. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  24. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  25. use Symfony\Component\Security\Http\SecurityEvents;
  26. class LoginHistoryListener implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var EntityManagerInterface
  30.      */
  31.     private $entityManager;
  32.     /**
  33.      * @var RequestStack
  34.      */
  35.     private $requestStack;
  36.     /**
  37.      * @var Context
  38.      */
  39.     private $requestContext;
  40.     /**
  41.      * @var MemberRepository
  42.      */
  43.     private $memberRepository;
  44.     /**
  45.      * @var LoginHistoryStatusRepository
  46.      */
  47.     private $loginHistoryStatusRepository;
  48.     public function __construct(
  49.         EntityManagerInterface $em,
  50.         RequestStack $requestStack,
  51.         Context $requestContext,
  52.         MemberRepository $memberRepository,
  53.         LoginHistoryStatusRepository $loginHistoryStatusRepository
  54.     ) {
  55.         $this->entityManager $em;
  56.         $this->requestStack $requestStack;
  57.         $this->requestContext $requestContext;
  58.         $this->memberRepository $memberRepository;
  59.         $this->loginHistoryStatusRepository $loginHistoryStatusRepository;
  60.     }
  61.     /**
  62.      * @return array
  63.      */
  64.     public static function getSubscribedEvents()
  65.     {
  66.         return [
  67.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  68.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  69.         ];
  70.     }
  71.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  72.     {
  73.         $request $event->getRequest();
  74.         $user $event
  75.             ->getAuthenticationToken()
  76.             ->getUser();
  77.         $Status $this->loginHistoryStatusRepository->find(LoginHistoryStatus::SUCCESS);
  78.         if (is_null($Status)) {
  79.             return;
  80.         }
  81.         if ($user instanceof Member) {
  82.             $LoginHistory = new LoginHistory();
  83.             $LoginHistory
  84.                 ->setLoginUser($user)
  85.                 ->setUserName($user->getUsername())
  86.                 ->setStatus($Status)
  87.                 ->setClientIp($request->getClientIp());
  88.             $this->entityManager->persist($LoginHistory);
  89.             $this->entityManager->flush();
  90.         }
  91.     }
  92.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  93.     {
  94.         $request $this->requestStack->getCurrentRequest();
  95.         if (!$this->requestContext->isAdmin()) {
  96.             return;
  97.         }
  98.         $Status $this->loginHistoryStatusRepository->find(LoginHistoryStatus::FAILURE);
  99.         if (is_null($Status)) {
  100.             return;
  101.         }
  102.         $userName $event->getAuthenticationToken()->getUsername();
  103.         $Member null;
  104.         if ($userName) {
  105.             $Member $this->memberRepository->findOneBy(['login_id' => $userName]);
  106.         }
  107.         $LoginHistory = new LoginHistory();
  108.         $LoginHistory
  109.             ->setLoginUser($Member)
  110.             ->setUserName($userName)
  111.             ->setStatus($Status)
  112.             ->setClientIp($request->getClientIp());
  113.         $this->entityManager->persist($LoginHistory);
  114.         $this->entityManager->flush();
  115.     }
  116. }