app/Customize/EventListener/UserPasswordInitListener.php line 91

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 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 Eccube\Repository\CustomerRepository;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpFoundation\RequestStack;
  23. use Symfony\Component\HttpKernel\Event\RequestEvent;
  24. use Symfony\Component\Security\Core\AuthenticationEvents;
  25. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  26. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  27. use Symfony\Component\Security\Http\SecurityEvents;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. use Symfony\Component\DependencyInjection\ContainerInterface;
  30. use Symfony\Component\HttpFoundation\RedirectResponse;
  31. use Symfony\Component\Security\Core\Security;
  32. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  33. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  34. class UserPasswordInitListener implements EventSubscriberInterface
  35. {
  36.     protected $em;
  37.     protected $cartService;
  38.     protected $purchaseFlow;
  39.     protected $requestStack;
  40.     protected $container;
  41.     protected $router;
  42.     protected $security;
  43.     private $tokenStorage;
  44.     private $session;
  45.     /**
  46.      * @var CustomerRepository
  47.      */
  48.     protected $customerRepository;
  49.     public function __construct(
  50.         EntityManagerInterface $em,
  51.         CartService $cartService,
  52.         PurchaseFlow $cartPurchaseFlow,
  53.         RequestStack $requestStack,
  54.         ContainerInterface $container,
  55.         CustomerRepository $customerRepository,
  56.         Security $security,
  57.         SessionInterface $session,
  58.         TokenStorageInterface $tokenStorage
  59.     ) {
  60.         $this->em $em;
  61.         $this->cartService $cartService;
  62.         $this->purchaseFlow $cartPurchaseFlow;
  63.         $this->requestStack $requestStack;
  64.         $this->container $container;
  65.         $this->router $this->container->get('router');
  66.         $this->customerRepository $customerRepository;
  67.         $this->security $security;
  68.         $this->session $session;
  69.         $this->tokenStorage $tokenStorage;
  70.     }
  71.     /**
  72.      * @param InteractiveLoginEvent $event
  73.      */
  74.     //public function onInteractiveLogin(InteractiveLoginEvent $event)
  75.     public function onKernelRequest(RequestEvent $event)
  76.     {
  77.         //$user = $event->getAuthenticationToken()->getUser();
  78.         $user $this->security->getUser();
  79.         $attributes $event->getRequest()->attributes;
  80.         $currentRoute $attributes->get('_route');
  81.         if ($user instanceof Customer && is_object($user) && ($currentRoute != 'password_init' && $currentRoute != null)) {
  82.             if(!is_object($user->getChangePasswordDate())){
  83.                 if($user->getResetKey() == "" || $user->getResetKey() == null){
  84.                     // リセットキーの発行・有効期限の設定
  85.                     $user
  86.                     ->setResetKey($this->customerRepository->getUniqueResetKey())
  87.                     ->setResetExpire(new \DateTime('9999/12/31'));
  88.                     // リセットキーを更新
  89.                     $this->em->persist($user);
  90.                     $this->em->flush();
  91.                 }
  92.                 $this->tokenStorage->setToken(null);
  93.                 $this->session->invalidate();
  94.                 $url $this->router->generate('password_init', ['reset_key' => $user->getResetKey()]);
  95.                 $response = new RedirectResponse($url);
  96.                 $event->setResponse($response);
  97.             }
  98.         }
  99.     }
  100.     /**
  101.      * Returns an array of event names this subscriber wants to listen to.
  102.      *
  103.      * The array keys are event names and the value can be:
  104.      *
  105.      * * The method name to call (priority defaults to 0)
  106.      * * An array composed of the method name to call and the priority
  107.      * * An array of arrays composed of the method names to call and respective
  108.      *   priorities, or 0 if unset
  109.      *
  110.      * For instance:
  111.      *
  112.      * * array('eventName' => 'methodName')
  113.      * * array('eventName' => array('methodName', $priority))
  114.      * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
  115.      *
  116.      * @return array The event names to listen to
  117.      */
  118.     public static function getSubscribedEvents()
  119.     {
  120.         return [
  121.             //SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  122.             'kernel.request' => 'onKernelRequest',
  123.         ];
  124.     }
  125. }