app/Customize/Controller/ForgotController.php line 97

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