src\Controller\SecurityController.php line 121

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use App\Entity\User;
  5. use App\Form\LoginType;
  6. use App\Handler\RecoverConfirmHandler;
  7. use App\Handler\RecoverHandler;
  8. use App\Handler\RegisterHandler;
  9. use Doctrine\ORM\NonUniqueResultException;
  10. use Exception;
  11. use Psr\Cache\InvalidArgumentException;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. /**
  19.  * Class SecurityController.
  20.  *
  21.  * @Route("", name="security_", options={"sitemap": false})
  22.  */
  23. final class SecurityController extends AbstractController
  24. {
  25.     /**
  26.      * @var RegisterHandler
  27.      */
  28.     private $registerHandler;
  29.     /**
  30.      * @var RecoverHandler
  31.      */
  32.     private $recoverHandler;
  33.     /**
  34.      * @var RecoverConfirmHandler
  35.      */
  36.     private $recoverConfirmHandler;
  37.     /**
  38.      * SecurityController constructor.
  39.      */
  40.     public function __construct(RecoverHandler        $recoverHandler,
  41.                                 RecoverConfirmHandler $recoverConfirmHandler,
  42.                                 RegisterHandler       $registerHandler,
  43.     )
  44.     {
  45.         $this->registerHandler $registerHandler;
  46.         $this->recoverHandler $recoverHandler;
  47.         $this->recoverConfirmHandler $recoverConfirmHandler;
  48.     }
  49.     /**
  50.      * @Route("/inscription/", name="register")
  51.      *
  52.      * @throws Exception
  53.      */
  54.     public function register(Request $request)
  55.     {
  56.         $this->registerHandler->process();
  57.         if (!is_null($this->registerHandler->getEmailState())) {
  58.             $session $request->getSession();
  59.             // Récupérer l'URL stockée par Symfony
  60.             $targetPath $session->get('_security.main.target_path');
  61.             if ($targetPath) {
  62.                 return $this->redirect($targetPath);
  63.             }
  64.             return $this->redirectToRoute('espace_client_index');
  65.         } else {
  66.             return
  67.                 $this->render('security/register.html.twig', [
  68.                     'form' => $this->registerHandler->getForm()->createView(),
  69.                 ]);
  70.         }
  71.     }
  72.     /**
  73.      * @Route("/connexion/", name="login")
  74.      *
  75.      * @throws InvalidArgumentException
  76.      */
  77.     public function login(Request $requestAuthenticationUtils $authenticationUtils): Response
  78.     {
  79.         $cache = new FilesystemAdapter();
  80.         $error $authenticationUtils->getLastAuthenticationError();
  81.         if ($cache->hasItem('emailResetPassword')) {
  82.             $cache->deleteItem('emailResetPassword');
  83.         }
  84.         $form $this->createForm(LoginType::class, null, [
  85.             'action' => $this->generateUrl('security_login'),
  86.             'method' => 'POST',
  87.         ]);
  88.         $form->handleRequest($request);
  89.         return $this->render('security/login.html.twig', [
  90.             'error' => $error,
  91.             'form' => $form->createView(),
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route("/recover/", name="recover")
  96.      *
  97.      * @throws InvalidArgumentException
  98.      * @throws Exception
  99.      */
  100.     public function recover(): Response
  101.     {
  102.         $cache = new FilesystemAdapter();
  103.         $this->recoverHandler->process();
  104.         if ($this->recoverHandler->getEmailState() > 0) {
  105.             $emailCached $cache->getItem('emailResetPassword');
  106.             $emailCached->set($this->recoverHandler->getForm()->get('email')->getData());
  107.             $cache->save($emailCached);
  108.             return $this->redirectToRoute('security_recover_confirm');
  109.         } else {
  110.             /* $this->getSession()
  111.             ->getBag('flashes')
  112.             ->add('danger', "L'adresse e-mail n'existe pas.")
  113.             ; */
  114.             return $this->render('security/recover.html.twig', [
  115.                 'form' => $this->recoverHandler->getForm()->createView(),
  116.                 'emailState' => $this->recoverHandler->getEmailState(),
  117.             ]);
  118.         }
  119.     }
  120.     /**
  121.      * @Route("/recover/confirm/", name="recover_confirm")
  122.      *
  123.      * @throws InvalidArgumentException
  124.      * @throws NonUniqueResultException
  125.      */
  126.     public function recoverConfirm(): Response
  127.     {
  128.         $cache = new FilesystemAdapter();
  129.         $this->recoverConfirmHandler->process();
  130.         if ($cache->hasItem('emailResetPassword') && !$this->recoverConfirmHandler->getForm()->isSubmitted()) {
  131.             $email $cache->getItem('emailResetPassword')->get();
  132.             $this->recoverConfirmHandler->getForm()->get('email')->setData($email);
  133.         }
  134.         return $this->recoverConfirmHandler->getEmailState() > 0
  135.             $this->redirectToRoute('security_login')
  136.             : $this->render('security/recover_confirm.html.twig', [
  137.                 'form' => $this->recoverConfirmHandler->getForm()->createView(),
  138.             ]);
  139.     }
  140.     /**
  141.      * @Route("/recover/cancel/{token}", name="recover_cancel")
  142.      *
  143.      * @param $token
  144.      */
  145.     public function recoverCancel($token): Response
  146.     {
  147. //        $recoverConfirmHandler = new RecoverConfirmHandler(
  148. //            $this->createForm(RecoverConfirmType::class, null),
  149. //            $this->get('request_stack')->getCurrentRequest(),
  150. //            $this->getDoctrine()->getManager());
  151.         /* return $this->render('security/recover_confirm.html.twig', [
  152.         //            'form' => $recoverConfirmHandler->getForm()->createView(),
  153.         'token' => $token,
  154.         ]); */
  155.         $this->addFlash('danger''Suite à votre signalement et pour des raisons de sécurité, nous vous invitons à modifier votre mot de passe.');
  156.         return $this->redirectToRoute('security_recover_confirm', ['token' => $token]);
  157.     }
  158. }