<?php
namespace App\Controller\Front;
use App\Entity\User;
use App\Event\Account\UserMailChangedEvent;
use App\Event\Account\UserMailChangeFinalizedEvent;
use App\Event\Account\UserPasswordChangedEvent;
use App\Event\Account\UserPasswordWantedEvent;
use App\Form\Account\LostEmailType;
use App\Form\Account\LostPasswordType;
use App\Form\Account\UpdateEmailType;
use App\Manager\UserManager;
use App\Repository\Scp\UserRepository as ScpUserRepository;
use App\Security\Front\LoginFormAuthenticator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route("/mon-compte")
*/
class AccountLostIdController extends AbstractAccountController
{
const SESSION_KEY_APP_USER = 'app.user';
/** @var ScpUserRepository */
protected $scpUserRepository;
/**
* AccountLostIdController constructor.
*/
public function __construct(
UserManager $userManager,
EventDispatcherInterface $dispatcher,
TranslatorInterface $translator,
ScpUserRepository $scpUserRepository,
TokenStorageInterface $tokenStorage,
RequestStack $requestStack
) {
parent::__construct($userManager, $dispatcher, $translator, $tokenStorage, $requestStack);
$this->scpUserRepository = $scpUserRepository;
}
/**
* @Route("/email/oublie", name="account_lost_email")
*/
public function getLostEmail(Request $request): Response
{
$data = [
'customerNumber' => null,
'connexionId' => null,
];
$form = $this->createForm(LostEmailType::class, $data);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$scpUser = $this->scpUserRepository
->findOneByCustomerNumberAndConnexionId($data['customerNumber'], $data['connexionId']);
if (null !== $scpUser) {
$user = $this->getDoctrine()->getRepository('App:User')
->findOneBy(['customerNumber' => $data['customerNumber']]);
if ($user instanceof User) {
$request->getSession()->set(self::SESSION_KEY_APP_USER, $user->getCustomerNumber());
return $this->render('front/account/email/display.html.twig', ['email' => $user->getEmail()]);
}
$request->getSession()->set(LoginFormAuthenticator::SESSION_KEY_OLD_USER, $scpUser);
return $this->redirectToRoute('account_update');
}
$this->addFlash('error', $this->transFront('account_creation.step1.message.user_not_found.html'));
}
return $this->render('front/account/email/recover.html.twig', ['form' => $form->createView()]);
}
/**
* @Route("/email/mise-a-jour", name="account_update_email")
*/
public function updateEmail(Request $request): Response
{
$user = $this->getDoctrine()->getRepository('App:User')
->findOneBy(['customerNumber' => $request->getSession()->get(self::SESSION_KEY_APP_USER)]);
$form = $this->createForm(UpdateEmailType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (null !== $this->getDoctrine()->getRepository('App:User')->findOneBy(['email' => $user->getEmail()])) {
$this->addFlash('error', $this->transFront('account_creation.step1.message.existing_email'));
} else {
$user->setPasswordCreationToken($this->userManager->generatePasswordCreationToken())
->setActivated(false);
$this->getDoctrine()->getManager()->flush();
$event = new UserMailChangedEvent($user);
$this->dispatcher->dispatch($event);
return $this->render('front/account/creation/step1_success.html.twig', [
'page_title' => $this->transFront('lost_email.page_title'),
'step_title' => $this->transFront('lost_email.step2.step_title'),
'confirmationMessage' => $this->transFront('lost_email.step2.success'),
]);
}
}
return $this->render('front/account/email/update.html.twig', ['form' => $form->createView()]);
}
/**
* @Route("/email/validation/{passwordCreationToken}", name="account_validate_email")
*/
public function validateEmailUpdate(Request $request, string $passwordCreationToken): Response
{
return $this->createPassword(
$request,
$passwordCreationToken,
$this->transFront('lost_email.page_title'),
$this->transFront('lost_email.step4.title'),
0,
0,
$this->transFront('lost_email.step4.success'),
UserMailChangeFinalizedEvent::class,
'lost_email.step4.submit',
false
);
}
/**
* @Route("/mot-de-passe/oublie", name="account_lost_password")
*/
public function getLostPassword(Request $request): Response
{
$data = ['email' => null];
$form = $this->createForm(LostPasswordType::class, $data);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$user = $this->getDoctrine()->getRepository('App:User')->findOneBy(['email' => $data['email']]);
if (!($user instanceof User)) {
$this->addFlash('error', $this->transFront('lost_password.step1.message.user_not_found.html'));
return $this->redirectToRoute('dashboard');
}
$user->setPasswordCreationToken($this->userManager->generatePasswordCreationToken());
$this->getDoctrine()->getManager()->flush();
$event = new UserPasswordWantedEvent($user);
$this->dispatcher->dispatch($event);
$this->addFlash('success', $this->transFront('lost_password.step1.message.success'));
}
return $this->render('front/account/password/recover.html.twig', ['form' => $form->createView()]);
}
/**
* @Route("/mot-de-passe/mise-a-jour/{passwordCreationToken}", name="account_password_update")
*/
public function updatePassword(Request $request, string $passwordCreationToken): Response
{
return $this->createPassword(
$request,
$passwordCreationToken,
$this->transFront('lost_password.step2.page_title'),
$this->transFront('lost_password.step2.title'),
0,
0,
$this->transFront('lost_password.step2.message.success'),
UserPasswordChangedEvent::class,
'lost_password.step2.submit',
false
);
}
/**
* @Route("/activation/perdue", name="account_lost_activation")
*/
public function getLostActivation(Request $request): Response
{
return $this->render('front/account/activation/recover.html.twig');
}
}