<?php
namespace App\Controller;
use App\Repository\UserRepository;
use App\Service\MailerService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
private MailerService $mailer;
protected EntityManagerInterface $manager;
private UserPasswordHasherInterface $hasher;
private UserRepository $userRepository;
public function __construct(
MailerService $mailer,
EntityManagerInterface $manager,
UserPasswordHasherInterface $hasher,
UserRepository $userRepository
)
{
$this->mailer = $mailer;
$this->manager = $manager;
$this->hasher = $hasher;
$this->userRepository = $userRepository;
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('user.index');
}
// 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(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/activation/{token}", name="app.activation")
*/
public function activation(string $token, UserRepository $user): RedirectResponse
{
$user = $user->findOneBy(['token' => $token]);
if (!$user) {
throw $this->createNotFoundException('Cet utilisateur n\'existe pas');
}
$user->setToken(null)
->setStatus(true);
$this->manager->persist($user);
$this->manager->flush();
$this->addFlash('success', 'Utilisateur activé avec succès');
return $this->redirectToRoute('app_login');
}
/**
* @Route("/request-password", name="app.request.password")
* @throws TransportExceptionInterface
*/
public function requestPassword(Request $request): Response
{
if ($request->isMethod('POST')) {
$this->mailer->sendForgotEmail($request);
$this->addFlash('success', 'Email envoyé');
return $this->redirectToRoute('app.request.password');
}
return $this->render('security/request.password.html.twig');
}
/**
* @Route("/reset-password/{token}", name="app.reset.password")
*/
public function resetPassword(Request $request, string $token): Response
{
$user = $this->userRepository->findOneBy(['token' => $token]);
if ($user === null) {
$this->addFlash('error', 'Veuillez verifier le lien ou votre boîte mail');
return $this->redirectToRoute('app_login');
}
if ($request->isMethod('POST')) {
$newPassword = (string) $request->request->get('reset-password-new');
$confirmPassword = (string) $request->request->get('reset-password-confirm');
if ($newPassword === $confirmPassword) {
$user->setToken(null);
$user->setPassword($this->hasher->hashPassword($user, $newPassword));
$this->manager->persist($user);
$this->manager->flush();
$this->addFlash('success', 'Mot de passe mis à jour');
return $this->redirectToRoute('app_login');
} else {
$this->addFlash('danger', 'Les mots de passe ne se correspondent pas');
}
}
return $this->render('security/reset.password.html.twig', ['token' => $token]);
}
}