<?php
declare(strict_types=1);
/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/
namespace App\Controller\Account;
use CoreShop\Bundle\CoreBundle\Form\Type\CustomerRegistrationType;
use CoreShop\Bundle\FrontendBundle\Controller\FrontendController;
use CoreShop\Bundle\UserBundle\Event\RequestPasswordChangeEvent;
use CoreShop\Bundle\UserBundle\Form\Type\RequestResetPasswordType;
use CoreShop\Bundle\UserBundle\Form\Type\ResetPasswordType;
use CoreShop\Component\Core\Model\CustomerInterface;
use CoreShop\Component\Core\Model\UserInterface;
use CoreShop\Component\Customer\Context\CustomerContextInterface;
use Pimcore;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Pimcore\Model\DataObject\CoreShopUser;
use Symfony\Component\Form\FormError;
class RegisterController extends FrontendController
{
public function registerAction(Request $request): Response
{
$customer = $this->getCustomer();
if ($customer instanceof CustomerInterface && null !== $customer->getUser()) {
return $this->redirectToRoute('account_informations');
}
$form = $this->get('form.factory')->createNamed('customer', CustomerRegistrationType::class, Pimcore::getContainer()->get('coreshop.factory.customer')->createNew());
$redirect = $this->getParameterFromRequest($request, '_redirect', $this->generateUrl('account_informations'));
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && !empty($request->get('customer'))) {
// Vérifier le champ honeypot "city"
$cityValue = $request->get('customer')['city'] ?? '';
$emailFirst = $request->get('customer')['email']['first'] ?? '';
$reversedEmail = strrev($emailFirst); // Inverser l'email
if (array_key_exists('gender', $request->get('customer')) || $cityValue !== $reversedEmail) {
//Honeypot
$redirect = $this->getParameterFromRequest($request, '_redirect', $this->generateUrl('basket'));
return $this->redirect($redirect);
}
$form = $form->handleRequest($request);
if ($form->isValid(null, ['dolpo'])) {
$customerData = $form->getData();
// Vérification de l'unicité de l'email
$existingUser = CoreShopUser::getByLoginIdentifier($customerData->getEmail(), 1);
if ($existingUser instanceof CoreShopUser) {
// Ajoutez ici un message d'erreur pour l'utilisateur
$form->addError(new FormError('Un compte avec cette adresse mail existe déjà. Vous pouvez demander la réinitialisation du mot de passe.'));
} else {
Pimcore::getContainer()->get('coreshop.customer.manager')->persistCustomer($customerData);
return $this->redirect($redirect);
}
}
}
return $this->render('basket/customer/_register.html.twig', [
'form' => $form->createView(),
]);
}
protected function getCustomer(): ?CustomerInterface
{
try {
/**
* @var CustomerInterface $customer
*/
$customer = $this->get(CustomerContextInterface::class)->getCustomer();
return $customer;
} catch (\Exception) {
}
return null;
}
protected function generateResetPasswordHash(UserInterface $customer): string
{
Pimcore::getContainer()->getParameter('coreshop.customer.security.login_identifier');
return hash('md5', $customer->getId() . $customer->getLoginIdentifier() . mt_rand() . time());
}
public function passwordResetRequestAction(Request $request): Response
{
$resetIdentifier = Pimcore::getContainer()->getParameter('coreshop.customer.security.login_identifier');
$form = $this->get('form.factory')->createNamed('coreshop', RequestResetPasswordType::class, null, ['reset_identifier' => $resetIdentifier]);
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
$handledForm = $form->handleRequest($request);
if ($handledForm->isSubmitted() && $handledForm->isValid()) {
$passwordResetData = $handledForm->getData();
$user = Pimcore::getContainer()->get('coreshop.repository.user')->findByLoginIdentifier($passwordResetData['email']);
if (!$user instanceof UserInterface) {
return $this->redirectToRoute('coreshop_index');
}
$user->setPasswordResetHash($this->generateResetPasswordHash($user));
$user->save();
$resetLink = $this->generateUrl('coreshop_customer_password_reset', ['token' => $user->getPasswordResetHash()], UrlGeneratorInterface::ABSOLUTE_URL);
$dispatcher = Pimcore::getContainer()->get('event_dispatcher');
$dispatcher->dispatch(new RequestPasswordChangeEvent($user, $resetLink), 'coreshop.user.request_password_reset');
$this->addFlash('success', Pimcore::getContainer()->get('translator')->trans('coreshop.ui.password_reset_request_success'));
return $this->redirectToRoute('account_login');
}
}
return $this->render('Register/password-reset-request.html.twig', [
'form' => $form->createView(),
]);
}
public function passwordResetAction(Request $request): Response
{
$resetToken = $this->getParameterFromRequest($request, 'token');
if ($resetToken) {
/**
* @var UserInterface|null $user
*/
$user = Pimcore::getContainer()->get('coreshop.repository.user')->findByResetToken($resetToken);
$form = $this->get('form.factory')->createNamed('coreshop', ResetPasswordType::class);
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) {
$handledForm = $form->handleRequest($request);
if ($handledForm->isSubmitted() && $handledForm->isValid() && !empty($user)) {
$resetPassword = $handledForm->getData();
$user->setPasswordResetHash(null);
$user->setPassword($resetPassword['password']);
$user->save();
$this->addFlash('success', Pimcore::getContainer()->get('translator')->trans('coreshop.ui.password_reset_success'));
$dispatcher = Pimcore::getContainer()->get('event_dispatcher');
$dispatcher->dispatch(new GenericEvent($user), 'coreshop.user.password_reset');
return $this->redirectToRoute('account_login');
}
}
return $this->render('Register/password-reset.html.twig', [
'form' => $form->createView(),
]);
}
return $this->redirectToRoute('coreshop_index');
}
}