app/Customize/EventListener/AuthenticationSuccessListener.php line 71

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 Customize\EventListener;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  15. use Symfony\Component\Security\Http\SecurityEvents;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Customize\Entity\LoggedinHistory;
  18. use Eccube\Entity\Customer;
  19. use Exception;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. /**
  22.  * ログイン成功時のイベントリスナー。
  23.  * ログインした時間をセッションとDBに登録する。
  24.  */
  25. class AuthenticationSuccessListener implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var EntityManagerInterface
  29.      */
  30.     protected $entityManager;
  31.     /**
  32.      * @var SessionInterface
  33.      */
  34.     private $session;
  35.     public function __construct(EntityManagerInterface $entityManagerSessionInterface $session)
  36.     {
  37.         $this->entityManager $entityManager;
  38.         $this->session $session;
  39.     }
  40.     /**
  41.      * 対応するメソッドと優先度を登録する。
  42.      * 数値が高いほど優先度高。
  43.      *
  44.      * @see https://symfony.com/doc/4.4/event_dispatcher.html
  45.      * @see https://symfony.com/doc/4.4/components/security/authentication.html
  46.      *
  47.      * @return array
  48.      */
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             SecurityEvents::INTERACTIVE_LOGIN => [
  53.                 ['registerLoggedinTime'0]
  54.             ]
  55.         ];
  56.     }
  57.     /**
  58.      * セッションにログイントークン、データベースにユーザ情報とログイントークンを保存する。
  59.      *
  60.      * @param InteractiveLoginEvent $event
  61.      * @return void
  62.      */
  63.     public function registerLoggedinTime(InteractiveLoginEvent $event)
  64.     {
  65.         try {
  66.             $request $event->getRequest();
  67.             $customer $event->getAuthenticationToken()->getUser();
  68.             if (!$customer instanceof Customer) {
  69.                 return;
  70.             }
  71.         
  72.             $loggedinHistory = new LoggedinHistory();
  73.             $loginToken md5(microtime());
  74.             $ip $request->getClientIp();
  75.             $userAgent $request->headers->get('User-Agent');
  76.             $loginDate = new \Datetime('now', new \DateTimeZone('Asia/Tokyo'));
  77.             $loggedinHistory->setCustomer($customer);
  78.             $loggedinHistory->setLoginToken($loginToken);
  79.             $loggedinHistory->setIp($ip);
  80.             $loggedinHistory->setUserAgent($userAgent);
  81.             $loggedinHistory->setLoginDate($loginDate);
  82.             $this->session->set('login_token'$loginToken);
  83.             $this->entityManager->persist($loggedinHistory);
  84.             $this->entityManager->flush();
  85.         } catch (Exception $e) {
  86.             log_error('[AuthenticationSuccessListener Error]: ' $e->getMessage());
  87.             throw $e;
  88.         }
  89.     }
  90. }