src/Controller/SecurityController.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\UserRepository;
  4. use App\Service\MailerService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. class SecurityController extends AbstractController
  15. {
  16.     private MailerService $mailer;
  17.     protected EntityManagerInterface $manager;
  18.     private UserPasswordHasherInterface $hasher;
  19.     private UserRepository $userRepository;
  20.     public function __construct(
  21.         MailerService               $mailer,
  22.         EntityManagerInterface      $manager,
  23.         UserPasswordHasherInterface $hasher,
  24.         UserRepository              $userRepository
  25.     )
  26.     {
  27.         $this->mailer $mailer;
  28.         $this->manager $manager;
  29.         $this->hasher $hasher;
  30.         $this->userRepository $userRepository;
  31.     }
  32.     /**
  33.      * @Route("/login", name="app_login")
  34.      */
  35.     public function login(AuthenticationUtils $authenticationUtils): Response
  36.     {
  37.         if ($this->getUser()) {
  38.             return $this->redirectToRoute('user.index');
  39.         }
  40.         // get the login error if there is one
  41.         $error $authenticationUtils->getLastAuthenticationError();
  42.         // last username entered by the user
  43.         $lastUsername $authenticationUtils->getLastUsername();
  44.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  45.     }
  46.     /**
  47.      * @Route("/logout", name="app_logout")
  48.      */
  49.     public function logout(): void
  50.     {
  51.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  52.     }
  53.     /**
  54.      * @Route("/activation/{token}", name="app.activation")
  55.      */
  56.     public function activation(string $tokenUserRepository $user): RedirectResponse
  57.     {
  58.         $user $user->findOneBy(['token' => $token]);
  59.         if (!$user) {
  60.             throw $this->createNotFoundException('Cet utilisateur n\'existe pas');
  61.         }
  62.         $user->setToken(null)
  63.             ->setStatus(true);
  64.         $this->manager->persist($user);
  65.         $this->manager->flush();
  66.         $this->addFlash('success''Utilisateur activé avec succès');
  67.         return $this->redirectToRoute('app_login');
  68.     }
  69.     /**
  70.      * @Route("/request-password", name="app.request.password")
  71.      * @throws TransportExceptionInterface
  72.      */
  73.     public function requestPassword(Request $request): Response
  74.     {
  75.         if ($request->isMethod('POST')) {
  76.             $this->mailer->sendForgotEmail($request);
  77.             $this->addFlash('success''Email envoyé');
  78.             return $this->redirectToRoute('app.request.password');
  79.         }
  80.         return $this->render('security/request.password.html.twig');
  81.     }
  82.     /**
  83.      * @Route("/reset-password/{token}", name="app.reset.password")
  84.      */
  85.     public function resetPassword(Request $requeststring $token): Response
  86.     {
  87.         $user $this->userRepository->findOneBy(['token' => $token]);
  88.         if ($user === null) {
  89.             $this->addFlash('error''Veuillez verifier le lien ou votre boîte mail');
  90.             return $this->redirectToRoute('app_login');
  91.         }
  92.         if ($request->isMethod('POST')) {
  93.             $newPassword = (string) $request->request->get('reset-password-new');
  94.             $confirmPassword = (string) $request->request->get('reset-password-confirm');
  95.             if ($newPassword === $confirmPassword) {
  96.                 $user->setToken(null);
  97.                 $user->setPassword($this->hasher->hashPassword($user$newPassword));
  98.                 $this->manager->persist($user);
  99.                 $this->manager->flush();
  100.                 $this->addFlash('success''Mot de passe mis à jour');
  101.                 return $this->redirectToRoute('app_login');
  102.             } else {
  103.                 $this->addFlash('danger''Les mots de passe ne se correspondent pas');
  104.             }
  105.         }
  106.         return $this->render('security/reset.password.html.twig', ['token' => $token]);
  107.     }
  108. }