src/Eccube/Controller/ForgotController.php line 79

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\Controller;
  13. use Eccube\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Eccube\Form\Type\Front\ForgotType;
  16. use Eccube\Form\Type\Front\PasswordResetType;
  17. use Eccube\Repository\CustomerRepository;
  18. use Eccube\Service\MailService;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception as HttpException;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  25. use Symfony\Component\Validator\Constraints as Assert;
  26. use Symfony\Component\Validator\Validator\ValidatorInterface;
  27. class ForgotController extends AbstractController
  28. {
  29.     /**
  30.      * @var ValidatorInterface
  31.      */
  32.     protected $validator;
  33.     /**
  34.      * @var MailService
  35.      */
  36.     protected $mailService;
  37.     /**
  38.      * @var CustomerRepository
  39.      */
  40.     protected $customerRepository;
  41.     /**
  42.      * @var EncoderFactoryInterface
  43.      */
  44.     protected $encoderFactory;
  45.     /**
  46.      * ForgotController constructor.
  47.      *
  48.      * @param ValidatorInterface $validator
  49.      * @param MailService $mailService
  50.      * @param CustomerRepository $customerRepository
  51.      * @param EncoderFactoryInterface $encoderFactory
  52.      */
  53.     public function __construct(
  54.         ValidatorInterface $validator,
  55.         MailService $mailService,
  56.         CustomerRepository $customerRepository,
  57.         EncoderFactoryInterface $encoderFactory
  58.     ) {
  59.         $this->validator $validator;
  60.         $this->mailService $mailService;
  61.         $this->customerRepository $customerRepository;
  62.         $this->encoderFactory $encoderFactory;
  63.     }
  64.     /**
  65.      * パスワードリマインダ.
  66.      *
  67.      * @Route("/forgot", name="forgot", methods={"GET", "POST"})
  68.      * @Template("Forgot/index.twig")
  69.      */
  70.     public function index(Request $request)
  71.     {
  72.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  73.             throw new HttpException\NotFoundHttpException();
  74.         }
  75.         $builder $this->formFactory
  76.             ->createNamedBuilder(''ForgotType::class);
  77.         $event = new EventArgs(
  78.             [
  79.                 'builder' => $builder,
  80.             ],
  81.             $request
  82.         );
  83.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_INDEX_INITIALIZE);
  84.         $form $builder->getForm();
  85.         $form->handleRequest($request);
  86.         if ($form->isSubmitted() && $form->isValid()) {
  87.             $Customer $this->customerRepository
  88.                 ->getRegularCustomerByEmail($form->get('login_email')->getData());
  89.             if (!is_null($Customer)) {
  90.                 // リセットキーの発行・有効期限の設定
  91.                 $Customer
  92.                     ->setResetKey($this->customerRepository->getUniqueResetKey())
  93.                     ->setResetExpire(new \DateTime('+'.$this->eccubeConfig['eccube_customer_reset_expire'].' min'));
  94.                 // リセットキーを更新
  95.                 $this->entityManager->persist($Customer);
  96.                 $this->entityManager->flush();
  97.                 $event = new EventArgs(
  98.                     [
  99.                         'form' => $form,
  100.                         'Customer' => $Customer,
  101.                     ],
  102.                     $request
  103.                 );
  104.                 $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_INDEX_COMPLETE);
  105.                 // 完了URLの生成
  106.                 $reset_url $this->generateUrl('forgot_reset', ['reset_key' => $Customer->getResetKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  107.                 // プラザの場合はフラグを追加
  108.                 $layout $request->getSession()->get('header_footer_layout''palace');
  109.                 if ($layout === 'plaza') {
  110.                     $reset_url .= '?plaza=1';
  111.                 }
  112.                 // メール送信
  113.                 $this->mailService->sendPasswordResetNotificationMail($Customer$reset_url);
  114.                 // ログ出力
  115.                 log_info('send reset password mail to:'."{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}");
  116.             } else {
  117.                 log_warning(
  118.                     'Un active customer try send reset password email: ',
  119.                     ['Enter email' => $form->get('login_email')->getData()]
  120.                 );
  121.             }
  122.             return $this->redirectToRoute('forgot_complete');
  123.         }
  124.         return [
  125.             'form' => $form->createView(),
  126.         ];
  127.     }
  128.     /**
  129.      * 再設定URL送信完了画面.
  130.      *
  131.      * @Route("/forgot/complete", name="forgot_complete", methods={"GET"})
  132.      * @Template("Forgot/complete.twig")
  133.      */
  134.     public function complete(Request $request)
  135.     {
  136.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  137.             throw new HttpException\NotFoundHttpException();
  138.         }
  139.         return [];
  140.     }
  141.     /**
  142.      * パスワード再発行実行画面.
  143.      *
  144.      * @Route("/forgot/reset/{reset_key}", name="forgot_reset", methods={"GET", "POST"})
  145.      * @Template("Forgot/reset.twig")
  146.      */
  147.     public function reset(Request $request$reset_key)
  148.     {
  149.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  150.             throw new HttpException\NotFoundHttpException();
  151.         }
  152.         // プラザフラグをセッションにセット
  153.         if ($request->query->get('plaza') == '1') {
  154.             $request->getSession()->set('header_footer_layout''plaza');
  155.             log_info('パスワード再発行:プラザフラグをセッションにセット');
  156.         }
  157.         $errors $this->validator->validate(
  158.             $reset_key,
  159.             [
  160.                 new Assert\NotBlank(),
  161.                 new Assert\Regex(
  162.                     [
  163.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  164.                     ]
  165.                 ),
  166.             ]
  167.         );
  168.         if (count($errors) > 0) {
  169.             // リセットキーに異常がある場合
  170.             throw new HttpException\NotFoundHttpException();
  171.         }
  172.         $Customer $this->customerRepository
  173.             ->getRegularCustomerByResetKey($reset_key);
  174.         if (null === $Customer) {
  175.             // リセットキーから会員データが取得できない場合
  176.             throw new HttpException\NotFoundHttpException();
  177.         }
  178.         $builder $this->formFactory
  179.             ->createNamedBuilder(''PasswordResetType::class);
  180.         $form $builder->getForm();
  181.         $form->handleRequest($request);
  182.         $error null;
  183.         if ($form->isSubmitted() && $form->isValid()) {
  184.             // リセットキー・入力メールアドレスで会員情報検索
  185.             $Customer $this->customerRepository
  186.                 ->getRegularCustomerByResetKey($reset_key$form->get('login_email')->getData());
  187.             if ($Customer) {
  188.                 // パスワードの発行・更新
  189.                 $encoder $this->encoderFactory->getEncoder($Customer);
  190.                 $pass $form->get('password')->getData();
  191.                 $Customer->setPassword($pass);
  192.                 // 発行したパスワードの暗号化
  193.                 if ($Customer->getSalt() === null) {
  194.                     $Customer->setSalt($this->encoderFactory->getEncoder($Customer)->createSalt());
  195.                 }
  196.                 $encPass $encoder->encodePassword($pass$Customer->getSalt());
  197.                 // パスワードを更新
  198.                 $Customer->setPassword($encPass);
  199.                 // リセットキーをクリア
  200.                 $Customer->setResetKey(null);
  201.                 // パスワードを更新
  202.                 $this->entityManager->persist($Customer);
  203.                 $this->entityManager->flush();
  204.                 $event = new EventArgs(
  205.                     [
  206.                         'Customer' => $Customer,
  207.                     ],
  208.                     $request
  209.                 );
  210.                 $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_RESET_COMPLETE);
  211.                 // 完了メッセージを設定
  212.                 $this->addFlash('password_reset_complete'trans('front.forgot.reset_complete'));
  213.                 // ログインページへリダイレクト
  214.                 return $this->redirectToRoute('mypage_login');
  215.             } else {
  216.                 // リセットキー・メールアドレスから会員データが取得できない場合
  217.                 $error trans('front.forgot.reset_not_found');
  218.             }
  219.         }
  220.         return [
  221.             'error' => $error,
  222.             'form' => $form->createView(),
  223.         ];
  224.     }
  225. }