src/Controller/Front/AccountLostIdController.php line 142

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\User;
  4. use App\Event\Account\UserMailChangedEvent;
  5. use App\Event\Account\UserMailChangeFinalizedEvent;
  6. use App\Event\Account\UserPasswordChangedEvent;
  7. use App\Event\Account\UserPasswordWantedEvent;
  8. use App\Form\Account\LostEmailType;
  9. use App\Form\Account\LostPasswordType;
  10. use App\Form\Account\UpdateEmailType;
  11. use App\Manager\UserManager;
  12. use App\Repository\Scp\UserRepository as ScpUserRepository;
  13. use App\Security\Front\LoginFormAuthenticator;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. /**
  22.  * @Route("/mon-compte")
  23.  */
  24. class AccountLostIdController extends AbstractAccountController
  25. {
  26.     const SESSION_KEY_APP_USER 'app.user';
  27.     /** @var ScpUserRepository */
  28.     protected $scpUserRepository;
  29.     /**
  30.      * AccountLostIdController constructor.
  31.      */
  32.     public function __construct(
  33.         UserManager $userManager,
  34.         EventDispatcherInterface $dispatcher,
  35.         TranslatorInterface $translator,
  36.         ScpUserRepository $scpUserRepository,
  37.         TokenStorageInterface $tokenStorage,
  38.         RequestStack $requestStack
  39.     ) {
  40.         parent::__construct($userManager$dispatcher$translator$tokenStorage$requestStack);
  41.         $this->scpUserRepository $scpUserRepository;
  42.     }
  43.     /**
  44.      * @Route("/email/oublie", name="account_lost_email")
  45.      */
  46.     public function getLostEmail(Request $request): Response
  47.     {
  48.         $data = [
  49.             'customerNumber' => null,
  50.             'connexionId'    => null,
  51.         ];
  52.         $form $this->createForm(LostEmailType::class, $data);
  53.         $form->handleRequest($request);
  54.         if ($form->isSubmitted() && $form->isValid()) {
  55.             $data $form->getData();
  56.             $scpUser $this->scpUserRepository
  57.                 ->findOneByCustomerNumberAndConnexionId($data['customerNumber'], $data['connexionId']);
  58.             if (null !== $scpUser) {
  59.                 $user $this->getDoctrine()->getRepository('App:User')
  60.                     ->findOneBy(['customerNumber' => $data['customerNumber']]);
  61.                 if ($user instanceof User) {
  62.                     $request->getSession()->set(self::SESSION_KEY_APP_USER$user->getCustomerNumber());
  63.                     return $this->render('front/account/email/display.html.twig', ['email' => $user->getEmail()]);
  64.                 }
  65.                 $request->getSession()->set(LoginFormAuthenticator::SESSION_KEY_OLD_USER$scpUser);
  66.                 return $this->redirectToRoute('account_update');
  67.             }
  68.             $this->addFlash('error'$this->transFront('account_creation.step1.message.user_not_found.html'));
  69.         }
  70.         return $this->render('front/account/email/recover.html.twig', ['form' => $form->createView()]);
  71.     }
  72.     /**
  73.      * @Route("/email/mise-a-jour", name="account_update_email")
  74.      */
  75.     public function updateEmail(Request $request): Response
  76.     {
  77.         $user $this->getDoctrine()->getRepository('App:User')
  78.             ->findOneBy(['customerNumber' => $request->getSession()->get(self::SESSION_KEY_APP_USER)]);
  79.         $form $this->createForm(UpdateEmailType::class, $user);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             if (null !== $this->getDoctrine()->getRepository('App:User')->findOneBy(['email' => $user->getEmail()])) {
  83.                 $this->addFlash('error'$this->transFront('account_creation.step1.message.existing_email'));
  84.             } else {
  85.                 $user->setPasswordCreationToken($this->userManager->generatePasswordCreationToken())
  86.                     ->setActivated(false);
  87.                 $this->getDoctrine()->getManager()->flush();
  88.                 $event = new UserMailChangedEvent($user);
  89.                 $this->dispatcher->dispatch($event);
  90.                 return $this->render('front/account/creation/step1_success.html.twig', [
  91.                     'page_title'          => $this->transFront('lost_email.page_title'),
  92.                     'step_title'          => $this->transFront('lost_email.step2.step_title'),
  93.                     'confirmationMessage' => $this->transFront('lost_email.step2.success'),
  94.                 ]);
  95.             }
  96.         }
  97.         return $this->render('front/account/email/update.html.twig', ['form' => $form->createView()]);
  98.     }
  99.     /**
  100.      * @Route("/email/validation/{passwordCreationToken}", name="account_validate_email")
  101.      */
  102.     public function validateEmailUpdate(Request $requeststring $passwordCreationToken): Response
  103.     {
  104.         return $this->createPassword(
  105.             $request,
  106.             $passwordCreationToken,
  107.             $this->transFront('lost_email.page_title'),
  108.             $this->transFront('lost_email.step4.title'),
  109.             0,
  110.             0,
  111.             $this->transFront('lost_email.step4.success'),
  112.             UserMailChangeFinalizedEvent::class,
  113.             'lost_email.step4.submit',
  114.             false
  115.         );
  116.     }
  117.     /**
  118.      * @Route("/mot-de-passe/oublie", name="account_lost_password")
  119.      */
  120.     public function getLostPassword(Request $request): Response
  121.     {
  122.         $data = ['email' => null];
  123.         $form $this->createForm(LostPasswordType::class, $data);
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             $data $form->getData();
  127.             $user $this->getDoctrine()->getRepository('App:User')->findOneBy(['email' => $data['email']]);
  128.             if (!($user instanceof User)) {
  129.                 $this->addFlash('error'$this->transFront('lost_password.step1.message.user_not_found.html'));
  130.                 return $this->redirectToRoute('dashboard');
  131.             }
  132.             $user->setPasswordCreationToken($this->userManager->generatePasswordCreationToken());
  133.             $this->getDoctrine()->getManager()->flush();
  134.             $event = new UserPasswordWantedEvent($user);
  135.             $this->dispatcher->dispatch($event);
  136.             $this->addFlash('success'$this->transFront('lost_password.step1.message.success'));
  137.         }
  138.         return $this->render('front/account/password/recover.html.twig', ['form' => $form->createView()]);
  139.     }
  140.     /**
  141.      * @Route("/mot-de-passe/mise-a-jour/{passwordCreationToken}", name="account_password_update")
  142.      */
  143.     public function updatePassword(Request $requeststring $passwordCreationToken): Response
  144.     {
  145.         return $this->createPassword(
  146.             $request,
  147.             $passwordCreationToken,
  148.             $this->transFront('lost_password.step2.page_title'),
  149.             $this->transFront('lost_password.step2.title'),
  150.             0,
  151.             0,
  152.             $this->transFront('lost_password.step2.message.success'),
  153.             UserPasswordChangedEvent::class,
  154.             'lost_password.step2.submit',
  155.             false
  156.         );
  157.     }
  158.     /**
  159.      * @Route("/activation/perdue", name="account_lost_activation")
  160.      */
  161.     public function getLostActivation(Request $request): Response
  162.     {
  163.         return $this->render('front/account/activation/recover.html.twig');
  164.     }
  165. }