src/Eccube/EventListener/SecurityListener.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\Customer;
  15. use Eccube\Entity\Member;
  16. use Eccube\Service\CartService;
  17. use Eccube\Service\PurchaseFlow\PurchaseContext;
  18. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  19. use Eccube\Service\OrderHelper;
  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 SecurityListener implements EventSubscriberInterface
  27. {
  28.     protected $em;
  29.     protected $cartService;
  30.     protected $purchaseFlow;
  31.     protected $requestStack;
  32.     public function __construct(
  33.         EntityManagerInterface $em,
  34.         CartService $cartService,
  35.         PurchaseFlow $cartPurchaseFlow,
  36.         RequestStack $requestStack
  37.     ) {
  38.         $this->em $em;
  39.         $this->cartService $cartService;
  40.         $this->purchaseFlow $cartPurchaseFlow;
  41.         $this->requestStack $requestStack;
  42.     }
  43.     /**
  44.      * @param InteractiveLoginEvent $event
  45.      */
  46.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  47.     {
  48.         $user $event
  49.             ->getAuthenticationToken()
  50.             ->getUser();
  51.         if ($user instanceof Member) {
  52.             $user->setLoginDate(new \DateTime());
  53.             $this->em->persist($user);
  54.             $this->em->flush();
  55.         } elseif ($user instanceof Customer) {
  56.             $this->cartService->mergeFromPersistedCart();
  57.             foreach ($this->cartService->getCarts() as $Cart) {
  58.                 $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$user));
  59.             }
  60.             $this->cartService->save();
  61.             if (count($this->cartService->getCarts()) > 1) {
  62.                 // カートが分割されていればメッセージを表示
  63.                 $event->getRequest()->getSession()->set(OrderHelper::SESSION_CART_DIVIDE_FLAGtrue);
  64.             }
  65.         }
  66.     }
  67.     /**
  68.      * @param AuthenticationFailureEvent $event
  69.      */
  70.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  71.     {
  72.         $request $this->requestStack->getCurrentRequest();
  73.         $request->getSession()->set('_security.login_memory', (bool) $request->request->get('login_memory'0));
  74.     }
  75.     /**
  76.      * Returns an array of event names this subscriber wants to listen to.
  77.      *
  78.      * The array keys are event names and the value can be:
  79.      *
  80.      * * The method name to call (priority defaults to 0)
  81.      * * An array composed of the method name to call and the priority
  82.      * * An array of arrays composed of the method names to call and respective
  83.      *   priorities, or 0 if unset
  84.      *
  85.      * For instance:
  86.      *
  87.      * * array('eventName' => 'methodName')
  88.      * * array('eventName' => array('methodName', $priority))
  89.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  90.      *
  91.      * @return array The event names to listen to
  92.      */
  93.     public static function getSubscribedEvents()
  94.     {
  95.         return [
  96.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  97.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  98.         ];
  99.     }
  100. }