src/Controller/SecurityController.php line 44

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\MailerService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\SecurityBundle\Security;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. class SecurityController extends AbstractController
  16. {
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $em;
  21.     /**
  22.      * @var MailerService
  23.      */
  24.     private $mailerService;
  25.     /**
  26.      * @var UserPasswordHasherInterface
  27.      */
  28.     private $passwordEncoder;
  29.     public function __construct(private Security $securityEntityManagerInterface $emMailerService $mailerServiceUserPasswordHasherInterface $passwordEncoder)
  30.     {
  31.         $this->security $security;
  32.         $this->em $em;
  33.         $this->mailerService $mailerService;
  34.         $this->passwordEncoder $passwordEncoder;
  35.     }
  36.     
  37.     #[Route(path'/login'name'app_login')]
  38.     public function login(AuthenticationUtils $authenticationUtils): Response
  39.     {
  40.         if ($this->security->isGranted('ROLE_ADMIN')) {
  41.             return $this->redirectToRoute('app_dashboard');
  42.         } else if ($this->security->isGranted('ROLE_MANAGER')) {
  43.             return $this->redirectToRoute('app_dashboard');
  44.         } else if ($this->security->isGranted('ROLE_COLLAB')) {
  45.             return $this->redirectToRoute('app_dashboard');
  46.         }
  47.         // get the login error if there is one
  48.         $error $authenticationUtils->getLastAuthenticationError();
  49.         // last username entered by the user
  50.         $lastUsername $authenticationUtils->getLastUsername();
  51.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  52.     }
  53.     #[Route(path'/logout'name'app_logout')]
  54.     public function logout(): void
  55.     {
  56.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  57.     }
  58.     #[Route(path'/oubli-mot-de-passe'name'app_forgotten_password')]
  59.     public function forgottenPassword(Request $request): Response
  60.     {
  61.         if ($request->isMethod('POST')) {
  62.             $email $request->request->get('email');
  63.             /** @var User $user */
  64.             $user $this->em->getRepository(User::class)->findOneByEmail($email);
  65.             if ($user === null) {
  66.                 return $this->redirectToRoute('app_forgotten_password', ['error' => 'Email invalide']);
  67.             }
  68.             $token md5(uniqid());
  69.             $user->setToken($token);
  70.             $this->em->flush();
  71.             $params = [
  72.                 'user' => $user,
  73.                 'url' => $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL)
  74.             ];
  75.             $this->mailerService->sendResetPassword($params);
  76.             return $this->redirectToRoute('app_login');
  77.         }
  78.         return $this->render('security/forgot-password.html.twig');
  79.     }
  80.     #[Route(path'/reinitialiser-mot-de-passe/{token}'name'app_reset_password')]
  81.     public function resetPassword(string $tokenRequest $request)
  82.     {
  83.         if ($request->isMethod('POST')) {
  84.             /* @var User $user */
  85.             $user $this->em->getRepository(User::class)->findOneBy(['token' => $token]);
  86.             
  87.             if ($user === null) {
  88.                 return $this->redirectToRoute('app_login', ['error' => 'Token invalide']);
  89.             }
  90.             if (($request->request->get('password') != $request->request->get('passwordbis')) || (strlen($request->request->get('passwordbis')) < 9)) {
  91.                 return $this->redirectToRoute('app_reset_password', ['token' => $token]);
  92.             }
  93.             $user->setToken(null);
  94.             $hashedPassword $this->passwordEncoder->hashPassword($user$request->request->get('password'));
  95.             $user->setPassword($hashedPassword);
  96.             $this->em->persist($user);
  97.             $this->em->flush();
  98.             return $this->redirectToRoute('app_login');
  99.         } else {
  100.             return $this->render('security/reset-password.html.twig', ['token' => $token]);
  101.         }
  102.     }
  103. }