src/Eccube/Controller/EntryController.php line 117

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\Entity\BaseInfo;
  14. use Eccube\Entity\Master\CustomerStatus;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Form\Type\Front\EntryType;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\CustomerRepository;
  20. use Eccube\Repository\Master\CustomerStatusRepository;
  21. use Eccube\Service\MailService;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpKernel\Exception as HttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  27. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  28. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  29. use Symfony\Component\Validator\Constraints as Assert;
  30. use Symfony\Component\Validator\Validator\ValidatorInterface;
  31. use Eccube\Service\CartService;
  32. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  33. class EntryController extends AbstractController
  34. {
  35.     /**
  36.      * @var CustomerStatusRepository
  37.      */
  38.     protected $customerStatusRepository;
  39.     /**
  40.      * @var ValidatorInterface
  41.      */
  42.     protected $recursiveValidator;
  43.     /**
  44.      * @var MailService
  45.      */
  46.     protected $mailService;
  47.     /**
  48.      * @var BaseInfo
  49.      */
  50.     protected $BaseInfo;
  51.     /**
  52.      * @var CustomerRepository
  53.      */
  54.     protected $customerRepository;
  55.     /**
  56.      * @var EncoderFactoryInterface
  57.      */
  58.     protected $encoderFactory;
  59.     /**
  60.      * @var TokenStorageInterface
  61.      */
  62.     protected $tokenStorage;
  63.     /**
  64.      * @var \Eccube\Service\CartService
  65.      */
  66.     protected $cartService;
  67.     /**
  68.      * EntryController constructor.
  69.      *
  70.      * @param CartService $cartService
  71.      * @param CustomerStatusRepository $customerStatusRepository
  72.      * @param MailService $mailService
  73.      * @param BaseInfoRepository $baseInfoRepository
  74.      * @param CustomerRepository $customerRepository
  75.      * @param EncoderFactoryInterface $encoderFactory
  76.      * @param ValidatorInterface $validatorInterface
  77.      * @param TokenStorageInterface $tokenStorage
  78.      */
  79.     public function __construct(
  80.         CartService $cartService,
  81.         CustomerStatusRepository $customerStatusRepository,
  82.         MailService $mailService,
  83.         BaseInfoRepository $baseInfoRepository,
  84.         CustomerRepository $customerRepository,
  85.         EncoderFactoryInterface $encoderFactory,
  86.         ValidatorInterface $validatorInterface,
  87.         TokenStorageInterface $tokenStorage
  88.     ) {
  89.         $this->customerStatusRepository $customerStatusRepository;
  90.         $this->mailService $mailService;
  91.         $this->BaseInfo $baseInfoRepository->get();
  92.         $this->customerRepository $customerRepository;
  93.         $this->encoderFactory $encoderFactory;
  94.         $this->recursiveValidator $validatorInterface;
  95.         $this->tokenStorage $tokenStorage;
  96.         $this->cartService $cartService;
  97.     }
  98.     /**
  99.      * 会員登録画面.
  100.      *
  101.      * @Route("/entry", name="entry")
  102.      * @Template("Entry/index.twig")
  103.      */
  104.     public function index(Request $request)
  105.     {
  106.         if ($this->isGranted('ROLE_USER')) {
  107.             log_info('認証済のためログイン処理をスキップ');
  108.             return $this->redirectToRoute('mypage');
  109.         }
  110.         /** @var $Customer \Eccube\Entity\Customer */
  111.         $Customer $this->customerRepository->newCustomer();
  112.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  113.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  114.         $event = new EventArgs(
  115.             [
  116.                 'builder' => $builder,
  117.                 'Customer' => $Customer,
  118.             ],
  119.             $request
  120.         );
  121.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE$event);
  122.         /* @var $form \Symfony\Component\Form\FormInterface */
  123.         $form $builder->getForm();
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             switch ($request->get('mode')) {
  127.                 case 'confirm':
  128.                     log_info('会員登録確認開始');
  129.                     log_info('会員登録確認完了');
  130.                     return $this->render(
  131.                         'Entry/confirm.twig',
  132.                         [
  133.                             'form' => $form->createView(),
  134.                         ]
  135.                     );
  136.                 case 'complete':
  137.                     log_info('会員登録開始');
  138.                     $encoder $this->encoderFactory->getEncoder($Customer);
  139.                     $salt $encoder->createSalt();
  140.                     $password $encoder->encodePassword($Customer->getPassword(), $salt);
  141.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  142.                     $Customer
  143.                         ->setSalt($salt)
  144.                         ->setPassword($password)
  145.                         ->setSecretKey($secretKey)
  146.                         ->setPoint(0);
  147.                     $this->entityManager->persist($Customer);
  148.                     $this->entityManager->flush();
  149.                     log_info('会員登録完了');
  150.                     $event = new EventArgs(
  151.                         [
  152.                             'form' => $form,
  153.                             'Customer' => $Customer,
  154.                         ],
  155.                         $request
  156.                     );
  157.                     $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE$event);
  158.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  159.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  160.                     if ($activateFlg) {
  161.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  162.                         // メール送信
  163.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  164.                         if ($event->hasResponse()) {
  165.                             return $event->getResponse();
  166.                         }
  167.                         log_info('仮会員登録完了画面へリダイレクト');
  168.                         return $this->redirectToRoute('entry_complete');
  169.                     } else {
  170.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  171.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  172.                         // URLを変更するため完了画面にリダイレクト
  173.                         return $this->redirectToRoute('entry_activate', [
  174.                             'secret_key' => $Customer->getSecretKey(),
  175.                             'qtyInCart' => $qtyInCart,
  176.                         ]);
  177.                     }
  178.             }
  179.         }
  180.         return [
  181.             'form' => $form->createView(),
  182.         ];
  183.     }
  184.     /**
  185.      * 会員登録完了画面.
  186.      *
  187.      * @Route("/entry/complete", name="entry_complete")
  188.      * @Template("Entry/complete.twig")
  189.      */
  190.     public function complete()
  191.     {
  192.         return [];
  193.     }
  194.     /**
  195.      * 会員のアクティベート(本会員化)を行う.
  196.      *
  197.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate")
  198.      * @Template("Entry/activate.twig")
  199.      */
  200.     public function activate(Request $request$secret_key$qtyInCart null)
  201.     {
  202.         $errors $this->recursiveValidator->validate(
  203.             $secret_key,
  204.             [
  205.                 new Assert\NotBlank(),
  206.                 new Assert\Regex(
  207.                     [
  208.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  209.                     ]
  210.                 ),
  211.             ]
  212.         );
  213.         if(!is_null($qtyInCart)) {
  214.             return [
  215.                 'qtyInCart' => $qtyInCart,
  216.             ];
  217.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  218.             // 会員登録処理を行う
  219.             $qtyInCart $this->entryActivate($request$secret_key);
  220.             return [
  221.                 'qtyInCart' => $qtyInCart,
  222.             ];
  223.         }
  224.         throw new HttpException\NotFoundHttpException();
  225.     }
  226.     /**
  227.      * 会員登録処理を行う
  228.      *
  229.      * @param Request $request
  230.      * @param $secret_key
  231.      * @return \Eccube\Entity\Cart|mixed
  232.      */
  233.     private function entryActivate(Request $request$secret_key)
  234.     {
  235.         log_info('本会員登録開始');
  236.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  237.         if (is_null($Customer)) {
  238.             throw new HttpException\NotFoundHttpException();
  239.         }
  240.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  241.         $Customer->setStatus($CustomerStatus);
  242.         $this->entityManager->persist($Customer);
  243.         $this->entityManager->flush();
  244.         log_info('本会員登録完了');
  245.         $event = new EventArgs(
  246.             [
  247.                 'Customer' => $Customer,
  248.             ],
  249.             $request
  250.         );
  251.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE$event);
  252.         // メール送信
  253.         $this->mailService->sendCustomerCompleteMail($Customer);
  254.         // Assign session carts into customer carts
  255.         $Carts $this->cartService->getCarts();
  256.         $qtyInCart 0;
  257.         foreach ($Carts as $Cart) {
  258.             $qtyInCart += $Cart->getTotalQuantity();
  259.         }
  260.         // 本会員登録してログイン状態にする
  261.         $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  262.         $this->tokenStorage->setToken($token);
  263.         $request->getSession()->migrate(true);
  264.         if ($qtyInCart) {
  265.             $this->cartService->save();
  266.         }
  267.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  268.         return $qtyInCart;
  269.     }
  270. }