src\Controller\SecurityController.php line 58

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