Hello!
Désolé pour le déterrage de post mais je me suis ENFIN occupé de ça.
Voici le code pour le fichier CustomerController.php du module importT1 :
<?php
namespace ImportT1\Controller\Front;
use ImportT1\Model\CustomerTemp;
use ImportT1\Model\CustomerTempQuery;
use Front\Front;
use Thelia\Core\Event\Customer\CustomerEvent;
use Thelia\Core\Event\Customer\CustomerLoginEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator;
use Thelia\Core\Security\Exception\AuthenticationException;
use Thelia\Core\Security\Exception\CustomerNotConfirmedException;
use Thelia\Core\Security\Exception\UsernameNotFoundException;
use Thelia\Core\Security\Exception\WrongPasswordException;
use Thelia\Form\CustomerLogin;
use Thelia\Form\Definition\FrontForm;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Log\Tlog;
use Thelia\Model\Customer;
use Thelia\Model\CustomerQuery;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CustomerController extends \Front\Controller\CustomerController
{
/**
* Perform user login. On a successful login, the user is redirected to the URL
* found in the success_url form parameter, or / if none was found.
*
* If login is not successfull, the same view is displayed again.
*
*/
public function loginAction(EventDispatcherInterface $eventDispatcher)
{
if (!$this->getSecurityContext()->hasCustomerUser()) {
$request = $this->getRequest();
$customerLoginForm = $this->createForm(CustomerLogin::class);
try {
$form = $this->validateForm($customerLoginForm, "post");
// If User is a new customer
if ($form->get('account')->getData() == 0 && $form->get("email")->getErrors()->count() == 0) {
return $this->generateRedirectFromRoute(
"customer.create.process",
["email" => $form->get("email")->getData()]
);
} else {
$customerTemp = CustomerTempQuery::create()
->where('`customer_temp`.email = ?', $form->get('email')->getData(), \PDO::PARAM_STR)
->where('`customer_temp`.password = CONCAT(\'*\', UPPER(SHA1(UNHEX(SHA1(?)))))', $form->get('password')->getData(), \PDO::PARAM_STR)
->where('`customer_temp`.processed = 0')
->findOne();
if (null !== $customerTemp) {
$customer = CustomerQuery::create()
->findOneByEmail($form->get('email')->getData());
if (null !== $customer) {
$customer->setPassword($form->get('password')->getData())
->save();
$customerTemp
->setProcessed(true)
->save();
$this->dispatch(TheliaEvents::CUSTOMER_LOGIN, new CustomerLoginEvent($customer));
$successUrl = $customerLoginForm->getSuccessUrl();
return RedirectResponse::create($successUrl);
}
}
try {
$authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm);
/** @var Customer $customer */
$customer = $authenticator->getAuthentifiedUser();
$this->processLogin($eventDispatcher, $customer);
if (intval($form->get('remember_me')->getData()) > 0) {
// If a remember me field if present and set in the form, create
// the cookie thant store "remember me" information
$this->createRememberMeCookie(
$customer,
$this->getRememberMeCookieName(),
$this->getRememberMeCookieExpiration()
);
}
return $this->generateSuccessRedirect($customerLoginForm);
} catch (UsernameNotFoundException $e) {
$message = $this->getTranslator()->trans(
"Wrong email or password. Please try again",
[],
Front::MESSAGE_DOMAIN
);
} catch (WrongPasswordException $e) {
$message = $this->getTranslator()->trans(
"Wrong email or password. Please try again",
[],
Front::MESSAGE_DOMAIN
);
} catch (CustomerNotConfirmedException $e) {
if ($e->getUser() !== null) {
// Send the confirmation email again
$this->getDispatcher()->dispatch(
TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL,
new CustomerEvent($e->getUser())
);
}
$message = $this->getTranslator()->trans(
"Your account is not yet confirmed. A confirmation email has been sent to your email address, please check your mailbox",
[],
Front::MESSAGE_DOMAIN
);
} catch (AuthenticationException $e) {
$message = $this->getTranslator()->trans(
"Wrong email or password. Please try again",
[],
Front::MESSAGE_DOMAIN
);
}
}
} catch (FormValidationException $e) {
$message = $this->getTranslator()->trans(
"Please check your input: %s",
['%s' => $e->getMessage()],
Front::MESSAGE_DOMAIN
);
} catch (\Exception $e) {
$message = $this->getTranslator()->trans(
"Sorry, an error occured: %s",
['%s' => $e->getMessage()],
Front::MESSAGE_DOMAIN
);
}
Tlog::getInstance()->error(
sprintf(
"Error during customer login process : %s. Exception was %s",
$message,
$e->getMessage()
)
);
$customerLoginForm->setErrorMessage($message);
$this->getParserContext()->addForm($customerLoginForm);
if ($customerLoginForm->hasErrorUrl()) {
return $this->generateErrorRedirect($customerLoginForm);
}else{
return $this->generateRedirectFromRoute("customer.login.view");
}
}
}
}
Last edited by Elyos (08-02-2024 14:30:37)