src/Eccube/EventListener/SecurityListener.php line 82

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