src/Controller/Account/RegisterController.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * CoreShop
  5.  *
  6.  * This source file is available under two different licenses:
  7.  *  - GNU General Public License version 3 (GPLv3)
  8.  *  - CoreShop Commercial License (CCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.org)
  13.  * @license    https://www.coreshop.org/license     GPLv3 and CCL
  14.  *
  15.  */
  16. namespace App\Controller\Account;
  17. use CoreShop\Bundle\CoreBundle\Form\Type\CustomerRegistrationType;
  18. use CoreShop\Bundle\FrontendBundle\Controller\FrontendController;
  19. use CoreShop\Bundle\UserBundle\Event\RequestPasswordChangeEvent;
  20. use CoreShop\Bundle\UserBundle\Form\Type\RequestResetPasswordType;
  21. use CoreShop\Bundle\UserBundle\Form\Type\ResetPasswordType;
  22. use CoreShop\Component\Core\Model\CustomerInterface;
  23. use CoreShop\Component\Core\Model\UserInterface;
  24. use CoreShop\Component\Customer\Context\CustomerContextInterface;
  25. use Pimcore;
  26. use Symfony\Component\EventDispatcher\GenericEvent;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  30. use Pimcore\Model\DataObject\CoreShopUser;
  31. use Symfony\Component\Form\FormError;
  32. class RegisterController extends FrontendController
  33. {
  34.     public function registerAction(Request $request): Response
  35.     {
  36.         $customer = $this->getCustomer();
  37.         if ($customer instanceof CustomerInterface && null !== $customer->getUser()) {
  38.             return $this->redirectToRoute('account_informations');
  39.         }
  40.         $form = $this->get('form.factory')->createNamed('customer', CustomerRegistrationType::class, Pimcore::getContainer()->get('coreshop.factory.customer')->createNew());
  41.         $redirect = $this->getParameterFromRequest($request, '_redirect', $this->generateUrl('account_informations'));
  42.         if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && !empty($request->get('customer'))) {
  43.             // Vérifier le champ honeypot "city"
  44.             $cityValue = $request->get('customer')['city'] ?? '';
  45.             $emailFirst = $request->get('customer')['email']['first'] ?? '';
  46.             $reversedEmail = strrev($emailFirst); // Inverser l'email
  47.             
  48.             if (array_key_exists('gender', $request->get('customer')) || $cityValue !== $reversedEmail) {
  49.                 //Honeypot
  50.                 $redirect = $this->getParameterFromRequest($request, '_redirect', $this->generateUrl('basket'));
  51.                 return $this->redirect($redirect);
  52.             }
  53.             $form = $form->handleRequest($request);
  54.             if ($form->isValid(null, ['dolpo'])) {
  55.                 $customerData = $form->getData();
  56.                             // Vérification de l'unicité de l'email
  57.                 $existingUser = CoreShopUser::getByLoginIdentifier($customerData->getEmail(), 1);
  58.                 if ($existingUser instanceof CoreShopUser) {
  59.                     // Ajoutez ici un message d'erreur pour l'utilisateur
  60.                     $form->addError(new FormError('Un compte avec cette adresse mail existe déjà. Vous pouvez demander la réinitialisation du mot de passe.'));
  61.                 } else {
  62.                     Pimcore::getContainer()->get('coreshop.customer.manager')->persistCustomer($customerData);
  63.                     return $this->redirect($redirect);
  64.                 }
  65.             }
  66.         }
  67.         return $this->render('basket/customer/_register.html.twig', [
  68.             'form' => $form->createView(),
  69.         ]);
  70.     }
  71.     protected function getCustomer(): ?CustomerInterface
  72.     {
  73.         try {
  74.             /**
  75.              * @var CustomerInterface $customer
  76.              */
  77.             $customer = $this->get(CustomerContextInterface::class)->getCustomer();
  78.             return $customer;
  79.         } catch (\Exception) {
  80.         }
  81.         return null;
  82.     }
  83.     protected function generateResetPasswordHash(UserInterface $customer): string
  84.     {
  85.         Pimcore::getContainer()->getParameter('coreshop.customer.security.login_identifier');
  86.         return hash('md5', $customer->getId() . $customer->getLoginIdentifier() . mt_rand() . time());
  87.     }
  88.     public function passwordResetRequestAction(Request $request): Response
  89.     {
  90.         $resetIdentifier = Pimcore::getContainer()->getParameter('coreshop.customer.security.login_identifier');
  91.         $form = $this->get('form.factory')->createNamed('coreshop', RequestResetPasswordType::class, null, ['reset_identifier' => $resetIdentifier]);
  92.         if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
  93.             $handledForm = $form->handleRequest($request);
  94.             if ($handledForm->isSubmitted() && $handledForm->isValid()) {
  95.                 $passwordResetData = $handledForm->getData();
  96.                 $user = Pimcore::getContainer()->get('coreshop.repository.user')->findByLoginIdentifier($passwordResetData['email']);
  97.                 if (!$user instanceof UserInterface) {
  98.                     return $this->redirectToRoute('coreshop_index');
  99.                 }
  100.                 $user->setPasswordResetHash($this->generateResetPasswordHash($user));
  101.                 $user->save();
  102.                 $resetLink = $this->generateUrl('coreshop_customer_password_reset', ['token' => $user->getPasswordResetHash()], UrlGeneratorInterface::ABSOLUTE_URL);
  103.                 $dispatcher = Pimcore::getContainer()->get('event_dispatcher');
  104.                 $dispatcher->dispatch(new RequestPasswordChangeEvent($user, $resetLink), 'coreshop.user.request_password_reset');
  105.                 $this->addFlash('success', Pimcore::getContainer()->get('translator')->trans('coreshop.ui.password_reset_request_success'));
  106.                 return $this->redirectToRoute('account_login');
  107.             }
  108.         }
  109.         return $this->render('Register/password-reset-request.html.twig', [
  110.             'form' => $form->createView(),
  111.         ]);
  112.     }
  113.     public function passwordResetAction(Request $request): Response
  114.     {
  115.         $resetToken = $this->getParameterFromRequest($request, 'token');
  116.         if ($resetToken) {
  117.             /**
  118.              * @var UserInterface|null $user
  119.              */
  120.             $user = Pimcore::getContainer()->get('coreshop.repository.user')->findByResetToken($resetToken);
  121.             $form = $this->get('form.factory')->createNamed('coreshop', ResetPasswordType::class);
  122.             if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
  123.                 $handledForm = $form->handleRequest($request);
  124.                 if ($handledForm->isSubmitted() && $handledForm->isValid() && !empty($user)) {
  125.                     $resetPassword = $handledForm->getData();
  126.                     $user->setPasswordResetHash(null);
  127.                     $user->setPassword($resetPassword['password']);
  128.                     $user->save();
  129.                     $this->addFlash('success', Pimcore::getContainer()->get('translator')->trans('coreshop.ui.password_reset_success'));
  130.                     $dispatcher = Pimcore::getContainer()->get('event_dispatcher');
  131.                     $dispatcher->dispatch(new GenericEvent($user), 'coreshop.user.password_reset');
  132.                     return $this->redirectToRoute('account_login');
  133.                 }
  134.             }
  135.             return $this->render('Register/password-reset.html.twig', [
  136.                 'form' => $form->createView(),
  137.             ]);
  138.         }
  139.         return $this->redirectToRoute('coreshop_index');
  140.     }
  141. }