app/Customize/Controller/ForgotController.php line 85

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