src/Eccube/EventListener/SecurityListener.php line 90

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\OrderHelper;
  18. use Eccube\Service\PurchaseFlow\PurchaseContext;
  19. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  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.             //buyflgが0と1両方のカートをマージする
  57.             for($i 0$i <= 1$i++){
  58.                 $this->cartService->mergeFromPersistedCart($i 1);
  59.                 foreach ($this->cartService->getCarts(false $i) as $Cart) {
  60.                     $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$user));
  61.                 }
  62.                 $this->cartService->save($i);
  63.             }
  64.             // $this->cartService->mergeFromPersistedCart();
  65.             // foreach ($this->cartService->getCarts() as $Cart) {
  66.             //     $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart, $user));
  67.             // }
  68.             // $this->cartService->save();
  69.             // if (count($this->cartService->getCarts()) > 1) {
  70.             //     // カートが分割されていればメッセージを表示
  71.             //     $event->getRequest()->getSession()->set(OrderHelper::SESSION_CART_DIVIDE_FLAG, true);
  72.             // }
  73.         }
  74.     }
  75.     /**
  76.      * @param AuthenticationFailureEvent $event
  77.      */
  78.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  79.     {
  80.         $request $this->requestStack->getCurrentRequest();
  81.         $request->getSession()->set('_security.login_memory', (bool) $request->request->get('login_memory'0));
  82.     }
  83.     /**
  84.      * Returns an array of event names this subscriber wants to listen to.
  85.      *
  86.      * The array keys are event names and the value can be:
  87.      *
  88.      * * The method name to call (priority defaults to 0)
  89.      * * An array composed of the method name to call and the priority
  90.      * * An array of arrays composed of the method names to call and respective
  91.      *   priorities, or 0 if unset
  92.      *
  93.      * For instance:
  94.      *
  95.      * * array('eventName' => 'methodName')
  96.      * * array('eventName' => array('methodName', $priority))
  97.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  98.      *
  99.      * @return array The event names to listen to
  100.      */
  101.     public static function getSubscribedEvents()
  102.     {
  103.         return [
  104.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  105.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  106.         ];
  107.     }
  108. }