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.                     $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  159.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  160.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  161.                     if ($activateFlg) {
  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.                     // 仮会員設定が無効な場合は認証URLへ遷移させ、会員登録を完了させる.
  170.                     } else {
  171.                         log_info('本会員登録画面へリダイレクト');
  172.                         return $this->redirect($activateUrl);
  173.                     }
  174.             }
  175.         }
  176.         return [
  177.             'form' => $form->createView(),
  178.         ];
  179.     }
  180.     /**
  181.      * 会員登録完了画面.
  182.      *
  183.      * @Route("/entry/complete", name="entry_complete")
  184.      * @Template("Entry/complete.twig")
  185.      */
  186.     public function complete()
  187.     {
  188.         return [];
  189.     }
  190.     /**
  191.      * 会員のアクティベート(本会員化)を行う.
  192.      *
  193.      * @Route("/entry/activate/{secret_key}", name="entry_activate")
  194.      * @Template("Entry/activate.twig")
  195.      */
  196.     public function activate(Request $request$secret_key)
  197.     {
  198.         $errors $this->recursiveValidator->validate(
  199.             $secret_key,
  200.             [
  201.                 new Assert\NotBlank(),
  202.                 new Assert\Regex(
  203.                     [
  204.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  205.                     ]
  206.                 ),
  207.             ]
  208.         );
  209.         if ($request->getMethod() === 'GET' && count($errors) === 0) {
  210.             log_info('本会員登録開始');
  211.             $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  212.             if (is_null($Customer)) {
  213.                 throw new HttpException\NotFoundHttpException();
  214.             }
  215.             $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  216.             $Customer->setStatus($CustomerStatus);
  217.             $this->entityManager->persist($Customer);
  218.             $this->entityManager->flush();
  219.             log_info('本会員登録完了');
  220.             $event = new EventArgs(
  221.                 [
  222.                     'Customer' => $Customer,
  223.                 ],
  224.                 $request
  225.             );
  226.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE$event);
  227.             // メール送信
  228.             $this->mailService->sendCustomerCompleteMail($Customer);
  229.             // Assign session carts into customer carts
  230.             $Carts $this->cartService->getCarts();
  231.             $qtyInCart array_reduce($Carts, function ($qty$Cart) {
  232.                 return $qty $Cart->getTotalQuantity();
  233.             });
  234.             // 本会員登録してログイン状態にする
  235.             $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  236.             $this->tokenStorage->setToken($token);
  237.             $request->getSession()->migrate(true);
  238.             if ($qtyInCart) {
  239.                 $this->cartService->save();
  240.             }
  241.             log_info('ログイン済に変更', [$this->getUser()->getId()]);
  242.             return [
  243.                 'qtyInCart' => $qtyInCart,
  244.             ];
  245.         }
  246.         throw new HttpException\NotFoundHttpException();
  247.     }
  248. }