src/Eccube/Controller/EntryController.php line 151

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\Repository\PageRepository;
  22. use Eccube\Service\CartService;
  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\Authentication\Token\Storage\TokenStorageInterface;
  30. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  31. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  32. use Symfony\Component\Validator\Constraints as Assert;
  33. use Symfony\Component\Validator\Validator\ValidatorInterface;
  34. use GuzzleHttp\Client;
  35. class EntryController extends AbstractController
  36. {
  37.     /**
  38.      * @var CustomerStatusRepository
  39.      */
  40.     protected $customerStatusRepository;
  41.     /**
  42.      * @var ValidatorInterface
  43.      */
  44.     protected $recursiveValidator;
  45.     /**
  46.      * @var MailService
  47.      */
  48.     protected $mailService;
  49.     /**
  50.      * @var BaseInfo
  51.      */
  52.     protected $BaseInfo;
  53.     /**
  54.      * @var CustomerRepository
  55.      */
  56.     protected $customerRepository;
  57.     /**
  58.      * @var EncoderFactoryInterface
  59.      */
  60.     protected $encoderFactory;
  61.     /**
  62.      * @var TokenStorageInterface
  63.      */
  64.     protected $tokenStorage;
  65.     /**
  66.      * @var \Eccube\Service\CartService
  67.      */
  68.     protected $cartService;
  69.     /**
  70.      * @var PageRepository
  71.      */
  72.     protected $pageRepository;
  73.     /**
  74.      * EntryController constructor.
  75.      *
  76.      * @param CartService $cartService
  77.      * @param CustomerStatusRepository $customerStatusRepository
  78.      * @param MailService $mailService
  79.      * @param BaseInfoRepository $baseInfoRepository
  80.      * @param CustomerRepository $customerRepository
  81.      * @param EncoderFactoryInterface $encoderFactory
  82.      * @param ValidatorInterface $validatorInterface
  83.      * @param TokenStorageInterface $tokenStorage
  84.      */
  85.     public function __construct(
  86.         CartService $cartService,
  87.         CustomerStatusRepository $customerStatusRepository,
  88.         MailService $mailService,
  89.         BaseInfoRepository $baseInfoRepository,
  90.         CustomerRepository $customerRepository,
  91.         EncoderFactoryInterface $encoderFactory,
  92.         ValidatorInterface $validatorInterface,
  93.         TokenStorageInterface $tokenStorage,
  94.         PageRepository $pageRepository
  95.     ) {
  96.         $this->customerStatusRepository $customerStatusRepository;
  97.         $this->mailService $mailService;
  98.         $this->BaseInfo $baseInfoRepository->get();
  99.         $this->customerRepository $customerRepository;
  100.         $this->encoderFactory $encoderFactory;
  101.         $this->recursiveValidator $validatorInterface;
  102.         $this->tokenStorage $tokenStorage;
  103.         $this->cartService $cartService;
  104.         $this->pageRepository $pageRepository;
  105.     }
  106.     /**
  107.      * 会員登録画面.
  108.      *
  109.      * @Route("/entry", name="entry", methods={"GET", "POST"})
  110.      * @Route("/entry", name="entry_confirm", methods={"GET", "POST"})
  111.      * @Template("Entry/index.twig")
  112.      */
  113.     public function index(Request $request)
  114.     {
  115.         if ($this->isGranted('ROLE_USER')) {
  116.             log_info('認証済のためログイン処理をスキップ');
  117.             return $this->redirectToRoute('mypage');
  118.         }
  119.         /** @var $Customer \Eccube\Entity\Customer */
  120.         $Customer $this->customerRepository->newCustomer();
  121.         /* @var $builder \Symfony\Component\Form\FormBuilderInterface */
  122.         $builder $this->formFactory->createBuilder(EntryType::class, $Customer);
  123.         $event = new EventArgs(
  124.             [
  125.                 'builder' => $builder,
  126.                 'Customer' => $Customer,
  127.             ],
  128.             $request
  129.         );
  130.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_INITIALIZE);
  131.         /* @var $form \Symfony\Component\Form\FormInterface */
  132.         $form $builder->getForm();
  133.         $form->handleRequest($request);
  134.         if ($form->isSubmitted() && $form->isValid()) {
  135.             switch ($request->get('mode')) {
  136.                 case 'confirm':
  137.                     log_info('会員登録確認開始');
  138.                     log_info('会員登録確認完了');
  139.                     return $this->render(
  140.                         'Entry/confirm.twig',
  141.                         [
  142.                             'form' => $form->createView(),
  143.                             'Page' => $this->pageRepository->getPageByRoute('entry_confirm'),
  144.                         ]
  145.                     );
  146.                 case 'complete':
  147.                     log_info('会員登録開始');
  148.                     $encoder $this->encoderFactory->getEncoder($Customer);
  149.                     $salt $encoder->createSalt();
  150.                     $password $encoder->encodePassword($Customer->getPlainPassword(), $salt);
  151.                     $secretKey $this->customerRepository->getUniqueSecretKey();
  152.                     $Customer
  153.                         ->setSalt($salt)
  154.                         ->setPassword($password)
  155.                         ->setSecretKey($secretKey)
  156.                         ->setPoint(0);
  157.                     $this->entityManager->persist($Customer);
  158.                     $this->entityManager->flush();
  159.                     log_info('会員登録完了');
  160.                     $event = new EventArgs(
  161.                         [
  162.                             'form' => $form,
  163.                             'Customer' => $Customer,
  164.                         ],
  165.                         $request
  166.                     );
  167.                     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_INDEX_COMPLETE);
  168.                     $activateFlg $this->BaseInfo->isOptionCustomerActivate();
  169.                     // 仮会員設定が有効な場合は、確認メールを送信し完了画面表示.
  170.                     if ($activateFlg) {
  171.                         $activateUrl $this->generateUrl('entry_activate', ['secret_key' => $Customer->getSecretKey()], UrlGeneratorInterface::ABSOLUTE_URL);
  172.                         // メール送信
  173.                         $this->mailService->sendCustomerConfirmMail($Customer$activateUrl);
  174.                         if ($event->hasResponse()) {
  175.                             return $event->getResponse();
  176.                         }
  177.                         log_info('仮会員登録完了画面へリダイレクト');
  178.                         return $this->redirectToRoute('entry_complete');
  179.                     } else {
  180.                         // 仮会員設定が無効な場合は、会員登録を完了させる.
  181.                         $qtyInCart $this->entryActivate($request$Customer->getSecretKey());
  182.                         // URLを変更するため完了画面にリダイレクト
  183.                         return $this->redirectToRoute('entry_activate', [
  184.                             'secret_key' => $Customer->getSecretKey(),
  185.                             'qtyInCart' => $qtyInCart,
  186.                         ]);
  187.                     }
  188.             }
  189.         }
  190.         return [
  191.             'form' => $form->createView(),
  192.         ];
  193.     }
  194.     /**
  195.      * 会員登録完了画面.
  196.      *
  197.      * @Route("/entry/complete", name="entry_complete", methods={"GET"})
  198.      * @Template("Entry/complete.twig")
  199.      */
  200.     public function complete()
  201.     {
  202.         return [];
  203.     }
  204.     /**
  205.      * 会員のアクティベート(本会員化)を行う.
  206.      *
  207.      * @Route("/entry/activate/{secret_key}/{qtyInCart}", name="entry_activate", methods={"GET"})
  208.      * @Template("Entry/activate.twig")
  209.      */
  210.     public function activate(Request $request$secret_key$qtyInCart null)
  211.     {
  212.         $errors $this->recursiveValidator->validate(
  213.             $secret_key,
  214.             [
  215.                 new Assert\NotBlank(),
  216.                 new Assert\Regex(
  217.                     [
  218.                         'pattern' => '/^[a-zA-Z0-9]+$/',
  219.                     ]
  220.                 ),
  221.             ]
  222.         );
  223.         if (!$this->session->has('eccube.login.target.path')) {
  224.             $this->setLoginTargetPath($this->generateUrl('mypage', [], UrlGeneratorInterface::ABSOLUTE_URL));
  225.         }
  226.         if (!is_null($qtyInCart)) {
  227.             return [
  228.                 'qtyInCart' => $qtyInCart,
  229.             ];
  230.         } elseif ($request->getMethod() === 'GET' && count($errors) === 0) {
  231.             // 会員登録処理を行う
  232.             $qtyInCart $this->entryActivate($request$secret_key);
  233.             return [
  234.                 'qtyInCart' => $qtyInCart,
  235.             ];
  236.         }
  237.         throw new HttpException\NotFoundHttpException();
  238.     }
  239.     /**
  240.      * 会員登録処理を行う
  241.      *
  242.      * @param Request $request
  243.      * @param $secret_key
  244.      *
  245.      * @return \Eccube\Entity\Cart|mixed
  246.      */
  247.     private function entryActivate(Request $request$secret_key)
  248.     {
  249.         log_info('本会員登録開始');
  250.         $Customer $this->customerRepository->getProvisionalCustomerBySecretKey($secret_key);
  251.         if (is_null($Customer)) {
  252.             throw new HttpException\NotFoundHttpException();
  253.         }
  254.         $CustomerStatus $this->customerStatusRepository->find(CustomerStatus::REGULAR);
  255.         $Customer->setStatus($CustomerStatus);
  256.         $this->entityManager->persist($Customer);
  257.         $this->entityManager->flush();
  258.         log_info('本会員登録完了');
  259.         $event = new EventArgs(
  260.             [
  261.                 'Customer' => $Customer,
  262.             ],
  263.             $request
  264.         );
  265.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_ENTRY_ACTIVATE_COMPLETE);
  266.         // メール送信
  267.         $this->mailService->sendCustomerCompleteMail($Customer);
  268.         // Assign session carts into customer carts
  269.         $Carts $this->cartService->getCarts();
  270.         $qtyInCart 0;
  271.         foreach ($Carts as $Cart) {
  272.             $qtyInCart += $Cart->getTotalQuantity();
  273.         }
  274.         // 本会員登録してログイン状態にする
  275.         $token = new UsernamePasswordToken($Customernull'customer', ['ROLE_USER']);
  276.         $this->tokenStorage->setToken($token);
  277.         $request->getSession()->migrate(true);
  278.         if ($qtyInCart) {
  279.             $this->cartService->save();
  280.         }
  281.         // 統合DB会員登録
  282.         $em $this->entityManager;
  283.         $em->getConnection()->beginTransaction();
  284.         $conn $em->getConnection();
  285.         $stmt $conn->prepare('SELECT * FROM dtb_customer WHERE id = :id;');
  286.         $result $stmt->execute([':id' => $Customer->getId()]);
  287.         //$row = $stmt->fetch();
  288.         $row $result->fetch();
  289.         $client = new Client();
  290.         $aes_key getenv('API_AES_KEY');
  291.         $user_id openssl_encrypt($row["id"],'aes-256-ecb',$aes_key);
  292.         if($row["egicom_id"]){
  293.             $user_code openssl_encrypt($row["egicom_id"],'aes-256-ecb',$aes_key);
  294.         }else{
  295.             $user_code "";
  296.         }
  297.         $mail_address openssl_encrypt($row["email"],'aes-256-ecb',$aes_key);
  298.         $name openssl_encrypt($row["name01"].$row["name02"],'aes-256-ecb',$aes_key);
  299.         $options = [
  300.             'headers' => [
  301.                 'Content-Type' => 'application/x-www-form-urlencoded'],
  302.             'form_params' => [
  303.                 "site_id" => 1,
  304.                 "user_id" => $user_id,
  305.                 "user_code" => $user_code,
  306.                 "mail_address" => $mail_address,
  307.                 "password" => $row["password"],
  308.                 "salt" => $row["salt"],
  309.                 "name" => $name
  310.                 ]
  311.             ];
  312.         $url getenv('API_KV_CREATE');
  313.         $response $client->request('POST'$url$options);
  314.         $res json_decode($response->getBody());
  315.         if(!$res->status){
  316.             log_info('統合DB会員登録エラー');
  317.         }
  318.         // テーブル更新(本会員)
  319.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  320.         $sth $conn->prepare('UPDATE dtb_customer set customer_status_id = 2 WHERE id = :id;');
  321.         $sth_return $sth->execute([
  322.             ':id' => $this->getUser()->getId()
  323.         ]);
  324.         $em->getConnection()->commit();
  325.         log_info('ログイン済に変更', [$sth_return]);
  326.         //log_info('ログイン済に変更', [$sth->rowCount()]);
  327.         log_info('ログイン済に変更', [$sth_return->rowCount()]);
  328.         log_info('ログイン済に変更', [$this->getUser()->getId()]);
  329.         return $qtyInCart;
  330.     }
  331. }