vendor/coreshop/core-shop/src/CoreShop/Bundle/FrontendBundle/Controller/RegisterController.php line 145

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 CoreShop\Bundle\FrontendBundle\Controller;
  17. use CoreShop\Bundle\CoreBundle\Form\Type\CustomerRegistrationType;
  18. use CoreShop\Bundle\UserBundle\Event\RequestPasswordChangeEvent;
  19. use CoreShop\Bundle\UserBundle\Form\Type\RequestResetPasswordType;
  20. use CoreShop\Bundle\UserBundle\Form\Type\ResetPasswordType;
  21. use CoreShop\Component\Core\Model\CustomerInterface;
  22. use CoreShop\Component\Core\Model\UserInterface;
  23. use CoreShop\Component\Customer\Context\CustomerContextInterface;
  24. use Symfony\Component\EventDispatcher\GenericEvent;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  29. class RegisterController extends FrontendController
  30. {
  31.     public function registerAction(Request $request): Response
  32.     {
  33.         $customer $this->getCustomer();
  34.         if ($customer instanceof CustomerInterface && null !== $customer->getUser()) {
  35.             return $this->redirectToRoute('coreshop_customer_profile');
  36.         }
  37.         $form $this->get('form.factory')->createNamed('customer'CustomerRegistrationType::class, $this->get('coreshop.factory.customer')->createNew());
  38.         $redirect $this->getParameterFromRequest($request'_redirect'$this->generateUrl('coreshop_customer_profile'));
  39.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  40.             $form $form->handleRequest($request);
  41.             if ($form->isValid()) {
  42.                 $customer $form->getData();
  43.                 $customer->setLocaleCode($this->get('coreshop.context.locale')->getLocaleCode());
  44.                 $this->get('coreshop.customer.manager')->persistCustomer($customer);
  45.                 return $this->redirect($redirect);
  46.             }
  47.         }
  48.         return $this->render($this->templateConfigurator->findTemplate('Register/register.html'), [
  49.             'form' => $form->createView(),
  50.         ]);
  51.     }
  52.     public function passwordResetRequestAction(Request $request): Response
  53.     {
  54.         $resetIdentifier $this->container->getParameter('coreshop.customer.security.login_identifier');
  55.         $form $this->get('form.factory')->createNamed('coreshop'RequestResetPasswordType::class, null, ['reset_identifier' => $resetIdentifier]);
  56.         if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  57.             $handledForm $form->handleRequest($request);
  58.             if ($handledForm->isSubmitted() && $handledForm->isValid()) {
  59.                 $passwordResetData $handledForm->getData();
  60.                 $user $this->container->get('coreshop.repository.user')->findByLoginIdentifier($passwordResetData['email']);
  61.                 if (!$user instanceof UserInterface) {
  62.                     return $this->redirectToRoute('coreshop_index');
  63.                 }
  64.                 $user->setPasswordResetHash($this->generateResetPasswordHash($user));
  65.                 $user->save();
  66.                 $resetLink $this->generateUrl('coreshop_customer_password_reset', ['token' => $user->getPasswordResetHash()], UrlGeneratorInterface::ABSOLUTE_URL);
  67.                 $dispatcher $this->container->get('event_dispatcher');
  68.                 $dispatcher->dispatch(new RequestPasswordChangeEvent($user$resetLink), 'coreshop.user.request_password_reset');
  69.                 $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.password_reset_request_success'));
  70.                 return $this->redirectToRoute('coreshop_login');
  71.             }
  72.         }
  73.         return $this->render($this->templateConfigurator->findTemplate('Register/password-reset-request.html'), [
  74.             'form' => $form->createView(),
  75.         ]);
  76.     }
  77.     public function passwordResetAction(Request $request): Response
  78.     {
  79.         $resetToken $this->getParameterFromRequest($request'token');
  80.         if ($resetToken) {
  81.             /**
  82.              * @var UserInterface $user
  83.              */
  84.             $user $this->get('coreshop.repository.user')->findByResetToken($resetToken);
  85.             if (!$user) {
  86.                 throw new NotFoundHttpException();
  87.             }
  88.             $form $this->get('form.factory')->createNamed('coreshop'ResetPasswordType::class);
  89.             if (in_array($request->getMethod(), ['POST''PUT''PATCH'], true)) {
  90.                 $handledForm $form->handleRequest($request);
  91.                 if ($handledForm->isSubmitted() && $handledForm->isValid()) {
  92.                     $resetPassword $handledForm->getData();
  93.                     $user->setPasswordResetHash(null);
  94.                     $user->setPassword($resetPassword['password']);
  95.                     $user->save();
  96.                     $this->addFlash('success'$this->get('translator')->trans('coreshop.ui.password_reset_success'));
  97.                     $dispatcher $this->container->get('event_dispatcher');
  98.                     $dispatcher->dispatch(new GenericEvent($user), 'coreshop.user.password_reset');
  99.                     return $this->redirectToRoute('coreshop_login');
  100.                 }
  101.             }
  102.             return $this->render($this->templateConfigurator->findTemplate('Register/password-reset.html'), [
  103.                 'form' => $form->createView(),
  104.             ]);
  105.         }
  106.         return $this->redirectToRoute('coreshop_index');
  107.     }
  108.     protected function getCustomer(): ?CustomerInterface
  109.     {
  110.         try {
  111.             /**
  112.              * @var CustomerInterface $customer
  113.              */
  114.             $customer $this->get(CustomerContextInterface::class)->getCustomer();
  115.             return $customer;
  116.         } catch (\Exception) {
  117.         }
  118.         return null;
  119.     }
  120.     protected function generateResetPasswordHash(UserInterface $customer): string
  121.     {
  122.         $this->container->getParameter('coreshop.customer.security.login_identifier');
  123.         return hash('md5'$customer->getId() . $customer->getLoginIdentifier() . mt_rand() . time());
  124.     }
  125. }