<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Customer;
use Eccube\Entity\Member;
use Eccube\Service\CartService;
use Eccube\Service\OrderHelper;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Eccube\Repository\CustomerRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UserPasswordInitListener implements EventSubscriberInterface
{
protected $em;
protected $cartService;
protected $purchaseFlow;
protected $requestStack;
protected $container;
protected $router;
protected $security;
private $tokenStorage;
private $session;
/**
* @var CustomerRepository
*/
protected $customerRepository;
public function __construct(
EntityManagerInterface $em,
CartService $cartService,
PurchaseFlow $cartPurchaseFlow,
RequestStack $requestStack,
ContainerInterface $container,
CustomerRepository $customerRepository,
Security $security,
SessionInterface $session,
TokenStorageInterface $tokenStorage
) {
$this->em = $em;
$this->cartService = $cartService;
$this->purchaseFlow = $cartPurchaseFlow;
$this->requestStack = $requestStack;
$this->container = $container;
$this->router = $this->container->get('router');
$this->customerRepository = $customerRepository;
$this->security = $security;
$this->session = $session;
$this->tokenStorage = $tokenStorage;
}
/**
* @param InteractiveLoginEvent $event
*/
//public function onInteractiveLogin(InteractiveLoginEvent $event)
public function onKernelRequest(RequestEvent $event)
{
//$user = $event->getAuthenticationToken()->getUser();
$user = $this->security->getUser();
$attributes = $event->getRequest()->attributes;
$currentRoute = $attributes->get('_route');
if ($user instanceof Customer && is_object($user) && ($currentRoute != 'password_init' && $currentRoute != null)) {
if(!is_object($user->getChangePasswordDate())){
if($user->getResetKey() == "" || $user->getResetKey() == null){
// リセットキーの発行・有効期限の設定
$user
->setResetKey($this->customerRepository->getUniqueResetKey())
->setResetExpire(new \DateTime('9999/12/31'));
// リセットキーを更新
$this->em->persist($user);
$this->em->flush();
}
$this->tokenStorage->setToken(null);
$this->session->invalidate();
$url = $this->router->generate('password_init', ['reset_key' => $user->getResetKey()]);
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
//SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
'kernel.request' => 'onKernelRequest',
];
}
}