app/Customize/Controller/ForgotController.php line 86

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 Customize\Form\Type\Front\ForgotType;
  16. use Eccube\Form\Type\Front\PasswordResetType;
  17. use Eccube\Repository\CustomerRepository;
  18. use Customize\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. //追加
  28. use Eccube\Controller\AbstractController;
  29. use Eccube\Service\CartService;
  30. use Customize\Repository\CategoryRepository;
  31. class ForgotController extends AbstractController
  32. {
  33.     protected $categoryRepository;
  34.     protected $cartService;
  35.     protected $validator;
  36.     protected $mailService;
  37.     protected $customerRepository;
  38.     protected $encoderFactory;
  39.     public function __construct(
  40.         CategoryRepository $categoryRepository,
  41.         CartService $cartService,
  42.         ValidatorInterface $validator,
  43.         MailService $mailService,
  44.         CustomerRepository $customerRepository,
  45.         EncoderFactoryInterface $encoderFactory
  46.     ) {
  47.         $this->categoryRepository $categoryRepository;
  48.         $this->cartService $cartService;
  49.         $this->validator $validator;
  50.         $this->mailService $mailService;
  51.         $this->customerRepository $customerRepository;
  52.         $this->encoderFactory $encoderFactory;
  53.     }
  54.     /**
  55.      * パスワードリマインダ.
  56.      *
  57.      * @Route("/forgot", name="forgot")
  58.      * @Template("Forgot/index.twig")
  59.      */
  60.     public function index(Request $request)
  61.     {
  62.         if ($this->isGranted('ROLE_USER')) {
  63.             throw new HttpException\NotFoundHttpException();
  64.         }
  65.         // カートサービスを使用してカート情報を取得
  66.         $cart $this->cartService->getCart();
  67.         // 数量と合計金額を取得
  68.         $cartTotalQuantity 0;
  69.         $cartTotalPrice 0;
  70.         if(!empty($cart)){
  71.             $cartTotalQuantity $cart->getTotalQuantity();
  72.             $cartTotalPrice $cart->getTotalPrice();
  73.         }
  74.         $builder $this->formFactory->createNamedBuilder(''ForgotType::class);
  75.         $form $builder->getForm();
  76.         $form->handleRequest($request);
  77.         $error 0;
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $Customer $this->customerRepository->getRegularCustomerByEmail($form->get('login_email')->getData());
  80.             if (!is_null($Customer)) {
  81.                 // パスワード生成
  82.                 $pwLength 8;    //パスワードの文字数
  83.                 srand((double) microtime() * 54234853);    // 乱数表のシードを決定
  84.                 $character 'abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679';    // パスワード文字列の配列を作成
  85.                 $pw preg_split('//'$character0PREG_SPLIT_NO_EMPTY);
  86.                 $pass '';
  87.                 for ($i 0$i $pwLength$i++) {
  88.                     $pass .= $pw[array_rand($pw1)];
  89.                 }
  90.                 // パスワードの発行・更新
  91.                 $encoder $this->encoderFactory->getEncoder($Customer);
  92.                 $Customer->setPassword($pass);
  93.                 // 発行したパスワードの暗号化
  94.                 if ($Customer->getSalt() === null) {
  95.                     $Customer->setSalt($this->encoderFactory->getEncoder($Customer)->createSalt());
  96.                 }
  97.                 $encPass $encoder->encodePassword($pass$Customer->getSalt());
  98.                 // パスワードを更新
  99.                 $Customer->setPassword($encPass);
  100.                 // リセットキーをクリア
  101.                 $Customer->setResetKey(null);
  102.                 // パスワードを更新
  103.                 $this->entityManager->persist($Customer);
  104.                 $this->entityManager->flush();
  105.                 // 完了メッセージを設定
  106.                 $this->addFlash('forgot_ok'trans('front.forgot.reset_complete'));
  107.                 // メール送信
  108.                 $this->mailService->sendPasswordResetMail($Customer$pass);
  109.                 // ログ出力
  110.                 log_info('send reset password mail to:'."{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}");
  111.                 // ログインページへリダイレクト
  112.                 return $this->redirectToRoute('forgot_complete', ['id' => $Customer->getId()]);
  113.             } else {
  114.                 $error 1;
  115.                 log_warning(
  116.                     'Un active customer try send reset password email: ',
  117.                     ['Enter email' => $form->get('login_email')->getData()]
  118.                 );
  119.             }
  120.         }
  121.         // メニュー用カテゴリー一覧
  122.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  123.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  124.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  125.             return $a->getSortNo() <=> $b->getSortNo();
  126.         });
  127.         return [
  128.             'error' => $error,
  129.             'form' => $form->createView(),
  130.             'cartTotalQuantity' => $cartTotalQuantity,
  131.             'cartTotalPrice' => $cartTotalPrice,
  132.             'sortedChildren' => $sortedChildren,
  133.         ];
  134.     }
  135.     /**
  136.      * 再設定URL送信完了画面.
  137.      *
  138.      * @Route("/forgot/complete/{id}", name="forgot_complete", requirements={"id" = "\d+"})
  139.      * @Template("Forgot/complete.twig")
  140.      */
  141.     public function complete(Request $request$id null)
  142.     {
  143.         if ($this->isGranted('ROLE_USER')) {
  144.             throw new HttpException\NotFoundHttpException();
  145.         }
  146.         // カートサービスを使用してカート情報を取得
  147.         $cart $this->cartService->getCart();
  148.         // 数量と合計金額を取得
  149.         $cartTotalQuantity 0;
  150.         $cartTotalPrice 0;
  151.         if(!empty($cart)){
  152.             $cartTotalQuantity $cart->getTotalQuantity();
  153.             $cartTotalPrice $cart->getTotalPrice();
  154.         }
  155.         // メニュー用カテゴリー一覧
  156.         $Cate1st $this->categoryRepository->findOneBy(['id' => 1]);
  157.         $sortedChildren $Cate1st $Cate1st->getChildren()->toArray() : [];
  158.         usort($sortedChildren, function($a$b) {    // sort_no 昇順でソート
  159.             return $a->getSortNo() <=> $b->getSortNo();
  160.         });
  161.         return [
  162.             'cartTotalQuantity' => $cartTotalQuantity,
  163.             'cartTotalPrice' => $cartTotalPrice,
  164.             'sortedChildren' => $sortedChildren,
  165.         ];
  166.     }
  167. }