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.                 $this->mailService->sendPasswordResetNotificationMail($Customer$reset_url);
  109.                 // ログ出力
  110.                 log_info('send reset password mail to:'."{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}");
  111.             } else {
  112.                 log_warning(
  113.                     'Un active customer try send reset password email: ',
  114.                     ['Enter email' => $form->get('login_email')->getData()]
  115.                 );
  116.             }
  117.             return $this->redirectToRoute('forgot_complete');
  118.         }
  119.         return [
  120.             'form' => $form->createView(),
  121.         ];
  122.     }
  123.     /**
  124.      * 再設定URL送信完了画面.
  125.      *
  126.      * @Route("/forgot/complete", name="forgot_complete", methods={"GET"})
  127.      * @Template("Forgot/complete.twig")
  128.      */
  129.     public function complete(Request $request)
  130.     {
  131.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  132.             throw new HttpException\NotFoundHttpException();
  133.         }
  134.         return [];
  135.     }
  136.     /**
  137.      * パスワード再発行実行画面.
  138.      *
  139.      * @Route("/forgot/reset/{reset_key}", name="forgot_reset", methods={"GET", "POST"})
  140.      * @Template("Forgot/reset.twig")
  141.      */
  142.     public function reset(Request $request$reset_key)
  143.     {
  144.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  145. //            throw new HttpException\NotFoundHttpException();
  146.             return $this->render("Forgot/timeout.twig");
  147.         }
  148.         $errors $this->validator->validate(
  149.             $reset_key,
  150.             [
  151.                 new Assert\NotBlank(),
  152.                 new Assert\Regex(
  153.                     [
  154.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  155.                     ]
  156.                 ),
  157.             ]
  158.         );
  159.         if (count($errors) > 0) {
  160.             // リセットキーに異常がある場合
  161.             throw new HttpException\NotFoundHttpException();
  162.         }
  163.         $Customer $this->customerRepository
  164.             ->getRegularCustomerByResetKey($reset_key);
  165.         if (null === $Customer) {
  166.             // リセットキーから会員データが取得できない場合
  167. //            throw new HttpException\NotFoundHttpException();
  168.             return $this->render("Forgot/timeout.twig");
  169.         }
  170.         $builder $this->formFactory
  171.             ->createNamedBuilder(''PasswordResetType::class);
  172.         $form $builder->getForm();
  173.         $form->handleRequest($request);
  174.         $error null;
  175.         if ($form->isSubmitted() && $form->isValid()) {
  176.             // リセットキー・入力メールアドレスで会員情報検索
  177.             $Customer $this->customerRepository
  178.                 ->getRegularCustomerByResetKey($reset_key$form->get('login_email')->getData());
  179.             if ($Customer) {
  180.                 // パスワードの発行・更新
  181.                 $encoder $this->encoderFactory->getEncoder($Customer);
  182.                 $pass $form->get('password')->getData();
  183.                 $Customer->setPassword($pass);
  184.                 // 発行したパスワードの暗号化
  185.                 if ($Customer->getSalt() === null) {
  186.                     $Customer->setSalt($this->encoderFactory->getEncoder($Customer)->createSalt());
  187.                 }
  188.                 $encPass $encoder->encodePassword($pass$Customer->getSalt());
  189.                 // パスワードを更新
  190.                 $Customer->setPassword($encPass);
  191.                 // リセットキーをクリア
  192.                 $Customer->setResetKey(null);
  193.                 // パスワードを更新
  194.                 $this->entityManager->persist($Customer);
  195.                 $this->entityManager->flush();
  196.                 $event = new EventArgs(
  197.                     [
  198.                         'Customer' => $Customer,
  199.                     ],
  200.                     $request
  201.                 );
  202.                 $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_RESET_COMPLETE);
  203.                 // 完了メッセージを設定
  204.                 $this->addFlash('password_reset_complete'trans('front.forgot.reset_complete'));
  205.                 // ログインページへリダイレクト
  206.                 return $this->redirectToRoute('mypage_login');
  207.             } else {
  208.                 // リセットキー・メールアドレスから会員データが取得できない場合
  209.                 $error trans('front.forgot.reset_not_found');
  210.             }
  211.         }
  212.         return [
  213.             'error' => $error,
  214.             'form' => $form->createView(),
  215.         ];
  216.     }
  217. }