app/Customize/EventListener/CustomSecurityListener.php line 101

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.  *  *  * 2023/11/29
  13.  * 1注文1商品のため、ログイン時にカートに追加した商品とセッションにある商品を合体させず、セッションの商品だけ表示する(カートの永続化防止)
  14.  * なお、重複実行防止の為、srcフォルダのイベントリスナーは削除、またはリネームして実行されないようにする
  15.  * 
  16.  */
  17. namespace Customize\EventListener;
  18. //namespace Eccube\EventListener;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Eccube\Entity\Customer;
  21. use Eccube\Entity\Member;
  22. //use Eccube\Service\CartService;
  23. use Eccube\Service\OrderHelper;
  24. use Eccube\Service\PurchaseFlow\PurchaseContext;
  25. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\HttpFoundation\RequestStack;
  28. use Symfony\Component\Security\Core\AuthenticationEvents;
  29. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  30. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  31. use Symfony\Component\Security\Http\SecurityEvents;
  32. use Customize\Service\CustomCartService as CartService;
  33. class CustomSecurityListener implements EventSubscriberInterface
  34. {
  35.     protected $em;
  36.     protected $cartService;
  37.     protected $purchaseFlow;
  38.     protected $requestStack;
  39.     public function __construct(
  40.         EntityManagerInterface $em,
  41.         CartService $cartService,
  42.         PurchaseFlow $cartPurchaseFlow,
  43.         RequestStack $requestStack
  44.     ) {
  45.         $this->em $em;
  46.         $this->cartService $cartService;
  47.         $this->purchaseFlow $cartPurchaseFlow;
  48.         $this->requestStack $requestStack;
  49.     }
  50.     /**
  51.      * @param InteractiveLoginEvent $event
  52.      */
  53.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  54.     {
  55.         log_debug('app/onInteractiveLogin start');
  56.         //ショッピングカート画面からログインした際に実行される
  57.         $user $event
  58.             ->getAuthenticationToken()
  59.             ->getUser();
  60.         if ($user instanceof Member) {
  61.             //eccube管理者か?
  62.             $user->setLoginDate(new \DateTime());
  63.             $this->em->persist($user);
  64.             $this->em->flush();
  65.         } elseif ($user instanceof Customer) {
  66.             dump('会員の場合の処理');
  67.             //会員の場合
  68.             //カートの永続化防止
  69.             //$this->cartService->mergeFromPersistedCart();
  70.             dump($this->cartService->getCarts());
  71.             foreach ($this->cartService->getCarts() as $Cart) {
  72.                 $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$user));
  73.             }
  74.             $this->cartService->save();
  75.             dump(count($this->cartService->getCarts()));
  76.             if (count($this->cartService->getCarts()) > 1) {
  77.                 // カートが分割されていればメッセージを表示
  78.                 $event->getRequest()->getSession()->set(OrderHelper::SESSION_CART_DIVIDE_FLAGtrue);
  79.             }
  80.             log_debug('app/onInteractiveLogin end');
  81.         }
  82.     }
  83.     /**
  84.      * @param AuthenticationFailureEvent $event
  85.      */
  86.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  87.     {
  88.         $request $this->requestStack->getCurrentRequest();
  89.         $request->getSession()->set('_security.login_memory', (bool) $request->request->get('login_memory'0));
  90.     }
  91.     /**
  92.      * Returns an array of event names this subscriber wants to listen to.
  93.      *
  94.      * The array keys are event names and the value can be:
  95.      *
  96.      * * The method name to call (priority defaults to 0)
  97.      * * An array composed of the method name to call and the priority
  98.      * * An array of arrays composed of the method names to call and respective
  99.      *   priorities, or 0 if unset
  100.      *
  101.      * For instance:
  102.      *
  103.      * * array('eventName' => 'methodName')
  104.      * * array('eventName' => array('methodName', $priority))
  105.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  106.      *
  107.      * @return array The event names to listen to
  108.      */
  109.     public static function getSubscribedEvents()
  110.     {
  111.         return [
  112.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  113.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  114.         ];
  115.     }
  116. }