<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\ResetPasswordWithEmailType;
use App\Form\SecurityResetPasswordType;
use App\Services\MailService;
use App\Services\Security\MailSecurityService;
use Doctrine\ORM\EntityManagerInterface;
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('Security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/demande-mot-de-passe-oublie", name="utilisateur_mdp_oublie", methods={"GET", "POST"})
*/
public function demandeUtilisateurMotDePasse(
Request $request,
EntityManagerInterface $em,
MailService $mailService,
MailSecurityService $mailSecurityService
): Response
{
$form = $this->createForm(ResetPasswordWithEmailType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = $form->get('email')->getData();
$user = $em->getRepository(User::class)->findOneByUsernameOrEmail($email);
/** @var $user User */
if ($user) {
$mailSecurityService->sendResettingEmailMessage($user);
$mailService->addEmailUserReset($user);
$em->persist($user);
$em->flush();
}
return $this->redirectToRoute('utilisateur_mdp_email_sent', ['username' => $email]);
}
return $this->render('Security/Resetting/request.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/demande-mot-de-passe-envoye", name="utilisateur_mdp_email_sent", methods={"GET", "POST"})
*/
public function demandeUtilisateurMdpEnvoie(Request $request): Response
{
$username = $request->request->get('username');
return $this->render('Security/Resetting/checkEmail.html.twig', [
'email' => $username
]);
}
/**
* @Route("/reinitialiser-mot-de-passe/{token}", name="reinitialiser_mdp", methods={"GET", "POST"})
*/
public function resetPasswordAction(
Request $request,
EntityManagerInterface $entityManager,
UserPasswordHasherInterface $passwordHasher,
$token
)
{
$user = $entityManager->getRepository(User::class)->findOneByResetToken($token);
if (null === $user) {
return new RedirectResponse($this->container->get('router')->generate('app_login'));
}
/** @var User $user */
$form = $this->createForm(SecurityResetPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$passwordRepeat = $form->get('passwordRepeat')->getData();
$user->setConfirmationToken(null);
$user->setPasswordRequestedAt(null);
$newEncodedPassword = $passwordHasher->hashPassword($user, $passwordRepeat);
$user->setPassword($newEncodedPassword);
$user->setUseNewPassword(true);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('app_login');
}
return $this->render('Security/Resetting/reset.html.twig', [
'form' => $form->createView(),
]);
}
}