app/Customize/Controller/ForgotController.php line 83

Open in your IDE?
  1. <?php
  2.     /**
  3.      * @version EC=CUBE4
  4.      * @copyright 株式会社 翔 kakeru.co.jp
  5.      * @author
  6.      * 2021年12月17日作成
  7.      *
  8.      * app\Customize\Controller\ForgotController.php
  9.      *
  10.      *
  11.      * ForgotController.phpのカスタマイズ
  12.      *
  13.      *
  14.      *
  15.      *                              C= C= C= ┌(;・_・)┘トコトコ
  16.      ******************************************************/
  17.     namespace Customize\Controller;
  18.     use Customize\Service\Google\ReCaptchaService;
  19.     use Eccube\Event\EccubeEvents;
  20.     use Eccube\Event\EventArgs;
  21.     use Eccube\Form\Type\Front\ForgotType;
  22. #    use Eccube\Form\Type\Front\PasswordResetType;
  23.     use Eccube\Repository\CustomerRepository;
  24.     use Eccube\Service\MailService;
  25.     use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  26.     use Symfony\Component\HttpFoundation\Request;
  27.     use Symfony\Component\HttpKernel\Exception as HttpException;
  28.     use Symfony\Component\Routing\Annotation\Route;
  29.     use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30.     use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  31.     use Symfony\Component\Validator\Constraints as Assert;
  32.     use Symfony\Component\Validator\Validator\ValidatorInterface;
  33.     use Customize\Form\Type\Front\PasswordResetType;
  34.     use Eccube\Entity\Master\job;
  35.     use Customize\Service\CartService;
  36.     use Customize\Service\CustomerService;
  37.     use Customize\Converter\CustomerConverter;
  38.     use Symfony\Component\HttpFoundation\Session\SessionInterface;
  39.     use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  40.     use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  41.     class ForgotController extends \Eccube\Controller\ForgotController{
  42.         protected $CustomerService;
  43.         protected $CustomerConverter;
  44.         protected $tokenStorage;
  45.         protected $session;
  46.         protected $cartService;
  47.         protected $reCaptchaService;
  48.     public function __construct(
  49.         ValidatorInterface $validator,
  50.         MailService $mailService,
  51.         CustomerRepository $customerRepository,
  52.         EncoderFactoryInterface $encoderFactory,
  53.         CustomerService $CustomerService,
  54.         CustomerConverter $CustomerConverter,
  55.         TokenStorageInterface $tokenStorage,
  56.         SessionInterface $session,
  57.         CartService $cartService,
  58.         ReCaptchaService $reCaptchaService
  59.     ) {
  60.         $this->validator $validator;
  61.         $this->mailService $mailService;
  62.         $this->customerRepository $customerRepository;
  63.         $this->encoderFactory  $encoderFactory;
  64.         $this->CustomerService $CustomerService;
  65.         $this->CustomerConverter $CustomerConverter;
  66.         $this->tokenStorage $tokenStorage;
  67.         $this->session $session;
  68.         $this->cartService $cartService;
  69.         $this->reCaptchaService $reCaptchaService;
  70.     }
  71.  /**
  72.      * パスワードリマインダ.
  73.      *
  74.      * @Route("/forgot/", name="forgot", methods={"GET", "POST"})
  75.      * @Template("Forgot/index.twig")
  76.      */
  77.     public function index(Request $request)
  78.     {
  79.         if ($this->isGranted('ROLE_USER')) {
  80.             throw new HttpException\NotFoundHttpException();
  81.         }
  82.         if ($request->getMethod() === 'GET') {
  83.             $referer $request->headers->get('referer');
  84.             $this->session->set('referer'$referer);
  85.         }
  86.         $builder $this->formFactory
  87.             ->createNamedBuilder(''ForgotType::class);
  88.         $event = new EventArgs(
  89.             [
  90.                 'builder' => $builder,
  91.             ],
  92.             $request
  93.         );
  94.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_INDEX_INITIALIZE);
  95.         $form $builder->getForm();
  96.         $form->handleRequest($request);
  97.         if ($form->isSubmitted() && $form->isValid()) {
  98.             $this->reCaptchaService->handleRequest($request);
  99.             $Customer $this->customerRepository
  100.                 ->getRegularCustomerByEmail($form->get('login_email')->getData());
  101.             if (is_null($Customer)) {
  102.                 if ($LmCustomer $this->CustomerService->GetLandMarkCustomerEmail($form->get('login_email')->getData())){
  103.                    list($Customer,$RetKey) = $this->CustomerService->SetCustomer($Customer,$LmCustomer,false);
  104.                 }
  105.             }
  106.             if (!is_null($Customer)) {
  107.                 // リセットキーの発行・有効期限の設定
  108.                 $Customer
  109.                     ->setResetKey($this->customerRepository->getUniqueResetKey())
  110.                     ->setResetExpire(new \DateTime('+'.$this->eccubeConfig['eccube_customer_reset_expire'].' min'));
  111.                 // リセットキーを更新
  112.                 $this->entityManager->persist($Customer);
  113.                 $this->entityManager->flush();
  114.                 $event = new EventArgs(
  115.                     [
  116.                         'form' => $form,
  117.                         'Customer' => $Customer,
  118.                     ],
  119.                     $request
  120.                 );
  121.                 $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_INDEX_COMPLETE);
  122.                 // 完了URLの生成
  123.                 $reset_url $this->generateUrl('forgot_reset', ['reset_key' => $Customer->getResetKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  124.                 // メール送信
  125.                 $this->mailService->sendPasswordResetNotificationMail($Customer$reset_url);
  126.                 // ログ出力
  127.                 log_info('send reset password mail to:'."{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}");
  128.             } else {
  129.                 log_warning(
  130.                     'Un active customer try send reset password email: ',
  131.                     ['Enter email' => $form->get('login_email')->getData()]
  132.                 );
  133.             }
  134.             return $this->redirectToRoute('forgot_complete');
  135.         }
  136.         return [
  137.             'form' => $form->createView(),
  138.             'BreadCrumbs' => [],
  139.             'reCaptchaSiteKey' => $this->reCaptchaService->getReCaptchaSiteKey()
  140.         ];
  141.     }
  142.   /**
  143.      * 再設定URL送信完了画面.
  144.      *
  145.      * @Route("/forgot/complete/", name="forgot_complete", methods={"GET"})
  146.      * @Template("Forgot/complete.twig")
  147.      */
  148.     public function complete(Request $request)
  149.     {
  150.         if ($this->isGranted('ROLE_USER')) {
  151.             throw new HttpException\NotFoundHttpException();
  152.         }
  153.         return [
  154.             'BreadCrumbs' => [],
  155.         ];
  156.     }
  157.     /**
  158.      * パスワード再発行実行画面.
  159.      *
  160.      * @Route("/forgot/reset/{reset_key}/", name="forgot_reset", methods={"GET", "POST"})
  161.      * @Template("Forgot/reset.twig")
  162.      */
  163.     public function reset(Request $request$reset_key)
  164.     {
  165.         if ($this->isGranted('ROLE_USER')) {
  166.             throw new HttpException\NotFoundHttpException();
  167.         }
  168.         $carts $this->cartService->getCarts();
  169.         $referer $this->session->get('referer'null);
  170.         if ($request->getMethod() === 'GET' && is_null($referer)) {
  171.             $referer $request->headers->get('referer');
  172.             $this->session->set('referer'$referer);
  173.         }
  174.         $errors $this->validator->validate(
  175.             $reset_key,
  176.             [
  177.                 new Assert\NotBlank(),
  178.                 new Assert\Regex(
  179.                     [
  180.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  181.                     ]
  182.                 ),
  183.             ]
  184.         );
  185.         if (count($errors) > 0) {
  186.             // リセットキーに異常がある場合
  187.             throw new HttpException\NotFoundHttpException();
  188.         }
  189.         $Customer $this->customerRepository
  190.             ->getRegularCustomerByResetKey($reset_key);
  191.         if (null === $Customer) {
  192.             // リセットキーから会員データが取得できない場合
  193.             throw new HttpException\NotFoundHttpException();
  194.         }
  195.         $builder $this->formFactory
  196.             ->createNamedBuilder(''PasswordResetType::class);
  197.         $form $builder->getForm();
  198.         #2021/12/20 kakeru
  199.         $this->CustomerService->SetFirstLoginFlg(0);
  200.         if (!$Customer->getPassword()){
  201.             $this->CustomerService->SetFirstLoginFlg(1);
  202.             if($Email=$this->session->get(CustomerService::LM_Customer_Email )){
  203.                 $this->CustomerService->SetFirstLoginFlg(2);
  204.                 $form->get('login_email')->setData($Email);
  205.                 $this->session->set(CustomerService::LM_Customer_Email,null);
  206.             }
  207.         }
  208.         $form->handleRequest($request);
  209.         $error null;
  210.         if ($form->isSubmitted() && $form->isValid()) {
  211.             // リセットキー・入力メールアドレスで会員情報検索
  212.             $Customer $this->customerRepository
  213.                 ->getRegularCustomerByResetKey($reset_key$form->get('login_email')->getData());
  214.             if ($Customer) {
  215.                 // パスワードの発行・更新
  216.                 $encoder $this->encoderFactory->getEncoder($Customer);
  217.                 $pass $form->get('password')->getData();
  218.                 $Customer->setPassword($pass);
  219.                 // 発行したパスワードの暗号化
  220.                 if ($Customer->getSalt() === null) {
  221.                     $Customer->setSalt($this->encoderFactory->getEncoder($Customer)->createSalt());
  222.                 }
  223.                 $encPass $encoder->encodePassword($pass$Customer->getSalt());
  224.                 // パスワードを更新
  225.                 $Customer->setPassword($encPass);
  226.                 // リセットキーをクリア
  227.                 $Customer->setResetKey(null);
  228.                 #2021/12/20 kakeru 業種の登録
  229.                 if ($Job=$form->get('job')->getData()){
  230.                     $Customer->setJob($Job);
  231.                 }
  232.                 // パスワードを更新
  233.                 $this->entityManager->persist($Customer);
  234.                 $this->entityManager->flush();
  235.                 #2021/12/22 kakeru LMDATAの送信
  236.                 $this->CustomerConverter->SetCustomer($Customer);
  237.                 $event = new EventArgs(
  238.                     [
  239.                         'Customer' => $Customer,
  240.                     ],
  241.                     $request
  242.                 );
  243.                 $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_FORGOT_RESET_COMPLETE);
  244.                 // 完了メッセージを設定
  245.                 $this->addFlash('password_reset_complete'trans('front.forgot.reset_complete'));
  246.                 $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  247.                 $this->tokenStorage->setToken($token);
  248.                 $request->getSession()->migrate(true);
  249.                 if (is_object($carts) || is_array($carts)) {
  250.                     foreach ($carts as $cart) {
  251.                         $persistedCarts $this->cartService->getPersistedCarts();
  252.                         // if cart type is catalog, remove old persisted cart of user
  253.                         if ($cart->getCartType() === CartService::CartTypeCatalog && $token->getUser() && (is_object($persistedCarts) || is_array($persistedCarts))) {
  254.                             $this->removePersistedCart($persistedCarts);
  255.                         }
  256.                         $cartKeys $this->session->get('cart_keys', []);
  257.                         $isCatalogCart false;
  258.                         if ((count($persistedCarts) > 0) && !in_array($persistedCarts[0]->getCartKey(), $cartKeystrue)) {
  259.                             foreach ($persistedCarts as $cartPersisted) {
  260.                                 if ($cartPersisted->getCartType() === CartService::CartTypeCatalog) {
  261.                                     $isCatalogCart true;
  262.                                     break;
  263.                                 }
  264.                             }
  265.                             if ($isCatalogCart) {
  266.                                 $this->removePersistedCart($persistedCarts);
  267.                             }
  268.                         }
  269.                         $cart->setCustomer($this->getUser());
  270.                         $this->entityManager->persist($cart);
  271.                         $this->entityManager->flush();
  272.                     }
  273.                 }
  274.                 $referer $this->session->get('referer');
  275.                 $domain $this->eccubeConfig['UT_URL'];
  276.                 if ($referer && strpos($referer$domain) !== false) {
  277.                     return $this->redirect($referer);
  278.                 } elseif ($referer == "shopping" || is_object($carts) || is_array($carts)) {
  279.                     return $this->redirectToRoute('shopping');
  280.                 }
  281.                 // ログインページへリダイレクト
  282.                 return $this->redirectToRoute('mypage_login');
  283.             } else {
  284.                 // リセットキー・メールアドレスから会員データが取得できない場合
  285.                 $error trans('front.forgot.reset_not_found');
  286.             }
  287.         }
  288.         return [
  289.             'error' => $error,
  290.             'form' => $form->createView(),
  291.             'Flg'  =>$this->CustomerService->GetFirstLoginFlg(),
  292.         ];
  293.     }
  294.     private function removePersistedCart($persistedCarts)
  295.     {
  296.         foreach ($persistedCarts as $persistedCart){
  297.             $this->entityManager->remove($persistedCart);
  298.             $this->entityManager->flush();
  299.         }
  300.     }
  301. }