<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\User;
use App\Form\LoginType;
use App\Handler\RecoverConfirmHandler;
use App\Handler\RecoverHandler;
use App\Handler\RegisterHandler;
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* Class SecurityController.
*
* @Route("", name="security_", options={"sitemap": false})
*/
final class SecurityController extends AbstractController
{
/**
* @var RegisterHandler
*/
private $registerHandler;
/**
* @var RecoverHandler
*/
private $recoverHandler;
/**
* @var RecoverConfirmHandler
*/
private $recoverConfirmHandler;
/**
* SecurityController constructor.
*/
public function __construct(RecoverHandler $recoverHandler,
RecoverConfirmHandler $recoverConfirmHandler,
RegisterHandler $registerHandler,
)
{
$this->registerHandler = $registerHandler;
$this->recoverHandler = $recoverHandler;
$this->recoverConfirmHandler = $recoverConfirmHandler;
}
/**
* @Route("/inscription/", name="register")
*
* @throws Exception
*/
public function register(Request $request)
{
$this->registerHandler->process();
if (!is_null($this->registerHandler->getEmailState())) {
$session = $request->getSession();
// Récupérer l'URL stockée par Symfony
$targetPath = $session->get('_security.main.target_path');
if ($targetPath) {
return $this->redirect($targetPath);
}
return $this->redirectToRoute('espace_client_index');
} else {
return
$this->render('security/register.html.twig', [
'form' => $this->registerHandler->getForm()->createView(),
]);
}
}
/**
* @Route("/connexion/", name="login")
*
* @throws InvalidArgumentException
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils): Response
{
$cache = new FilesystemAdapter();
$error = $authenticationUtils->getLastAuthenticationError();
if ($cache->hasItem('emailResetPassword')) {
$cache->deleteItem('emailResetPassword');
}
$form = $this->createForm(LoginType::class, null, [
'action' => $this->generateUrl('security_login'),
'method' => 'POST',
]);
$form->handleRequest($request);
return $this->render('security/login.html.twig', [
'error' => $error,
'form' => $form->createView(),
]);
}
/**
* @Route("/recover/", name="recover")
*
* @throws InvalidArgumentException
* @throws Exception
*/
public function recover(): Response
{
$cache = new FilesystemAdapter();
$this->recoverHandler->process();
if ($this->recoverHandler->getEmailState() > 0) {
$emailCached = $cache->getItem('emailResetPassword');
$emailCached->set($this->recoverHandler->getForm()->get('email')->getData());
$cache->save($emailCached);
return $this->redirectToRoute('security_recover_confirm');
} else {
/* $this->getSession()
->getBag('flashes')
->add('danger', "L'adresse e-mail n'existe pas.")
; */
return $this->render('security/recover.html.twig', [
'form' => $this->recoverHandler->getForm()->createView(),
'emailState' => $this->recoverHandler->getEmailState(),
]);
}
}
/**
* @Route("/recover/confirm/", name="recover_confirm")
*
* @throws InvalidArgumentException
* @throws NonUniqueResultException
*/
public function recoverConfirm(): Response
{
$cache = new FilesystemAdapter();
$this->recoverConfirmHandler->process();
if ($cache->hasItem('emailResetPassword') && !$this->recoverConfirmHandler->getForm()->isSubmitted()) {
$email = $cache->getItem('emailResetPassword')->get();
$this->recoverConfirmHandler->getForm()->get('email')->setData($email);
}
return $this->recoverConfirmHandler->getEmailState() > 0
? $this->redirectToRoute('security_login')
: $this->render('security/recover_confirm.html.twig', [
'form' => $this->recoverConfirmHandler->getForm()->createView(),
]);
}
/**
* @Route("/recover/cancel/{token}", name="recover_cancel")
*
* @param $token
*/
public function recoverCancel($token): Response
{
// $recoverConfirmHandler = new RecoverConfirmHandler(
// $this->createForm(RecoverConfirmType::class, null),
// $this->get('request_stack')->getCurrentRequest(),
// $this->getDoctrine()->getManager());
/* return $this->render('security/recover_confirm.html.twig', [
// 'form' => $recoverConfirmHandler->getForm()->createView(),
'token' => $token,
]); */
$this->addFlash('danger', 'Suite à votre signalement et pour des raisons de sécurité, nous vous invitons à modifier votre mot de passe.');
return $this->redirectToRoute('security_recover_confirm', ['token' => $token]);
}
}