src/Controller/SecurityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User\User;
  4. use App\Entity\User\UserStatus;
  5. use App\Service\User\ResetUser;
  6. use App\Entity\Email\TemplateEmail;
  7. use Symfony\Component\Form\FormError;
  8. use App\Repository\User\UserRepository;
  9. use App\Form\Security\ResetPasswordType;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Form\Security\ChangePasswordType;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use App\Service\Manager\Email\TemplateMailerManager;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. class SecurityController extends AbstractController
  20. {
  21.     #[Route(path'/login'name'app_login')]
  22.     public function login(AuthenticationUtils $authenticationUtils): Response
  23.     {
  24.         if ($this->getUser()) {
  25.             return $this->redirectToRoute('app_admin_index');
  26.         }
  27.         // get the login error if there is one
  28.         $error $authenticationUtils->getLastAuthenticationError();
  29.         // last username entered by the user
  30.         $lastUsername $authenticationUtils->getLastUsername();
  31.         return $this->render('security/login.html.twig', [
  32.             'last_username' => $lastUsername,
  33.             'error' => $error
  34.         ]);
  35.     }
  36.     #[Route(path'/logout'name'app_logout')]
  37.     public function logout(): void
  38.     {
  39.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  40.     }
  41.     /**
  42.      * @Route("/forgotten-password", name="app_forgotten_password")
  43.      */
  44.     public function forgottenPassword(
  45.         Request $request,
  46.         UserRepository $userRepository,
  47.         TemplateMailerManager $templateMailer,
  48.         ResetUser $resetUser
  49.     ) {
  50.         if ($this->getUser()) {
  51.             return $this->redirectToRoute('app_admin_index');
  52.         }
  53.         $form $this->createForm(ResetPasswordType::class);
  54.         $form->handleRequest($request);
  55.         if ($form->isSubmitted() && $form->isValid()) {
  56.             $error 1;
  57.             $email $form->get('email')->getData();
  58.             /** @var User $user */
  59.             $user $userRepository->findOneByEmail($email);
  60.             if ($user) {
  61.                 if (
  62.                     !$user->getDeletedAt() &&
  63.                     $user->getStatus() &&
  64.                     $user->getStatus()->getValue() == UserStatus::VALID
  65.                 ) {
  66.                     $reset $resetUser->resetPassword($user);
  67.                     if ($reset) {
  68.                         $templateMailer->selectTemplateAndSendEmail(
  69.                             TemplateEmail::TYPE_FORGOTTEN_PASSWORD,
  70.                             $user,
  71.                             ['reset_password_link' => true]
  72.                         );
  73.                         $error 0;
  74.                         $this->addFlash('success''Un email de réinitialisation de mot de passe a été envoyé à l\'adresse email indiquée.');
  75.                     }
  76.                 }
  77.             }
  78.             if ($error) {
  79.                 $form
  80.                     ->get('email')
  81.                     ->addError(new FormError('Email inconnu'));
  82.             } else {
  83.                 return $this->redirectToRoute('app_forgotten_password');
  84.             }
  85.         }
  86.         return $this->render('security/forgotten_password.html.twig', [
  87.             'form' => $form->createView()
  88.         ]);
  89.     }
  90.     /**
  91.      * @Route("/reset-password/{token}", name="app_reset_password")
  92.      */
  93.     public function resetPassword(
  94.         string $token,
  95.         Request $request,
  96.         UserRepository $userRepository,
  97.         UserPasswordHasherInterface $passwordHasher,
  98.         EntityManagerInterface $entityManager
  99.     ) {
  100.         if ($this->getUser()) {
  101.             return $this->redirectToRoute('app_admin_index');
  102.         }
  103.         /** @var User $user */
  104.         $user $userRepository->findOneByResetToken($token);
  105.         if ($user === null) {
  106.             $this->addFlash('danger''Lien invalide, merci de faire une nouvelle demande.');
  107.             return $this->redirectToRoute('app_admin_index');
  108.         }
  109.         $form $this->createForm(ChangePasswordType::class);
  110.         $form->handleRequest($request);
  111.         if ($form->isSubmitted() && $form->isValid()) {
  112.             $user->setPassword($passwordHasher->hashPassword($user$form->get('password')->getData()));
  113.             $user->setResetToken(null);
  114.             $entityManager->persist($user);
  115.             $entityManager->flush();
  116.             $this->addFlash('success''Votre nouveau mot de passe a été créé.');
  117.             return $this->redirectToRoute('app_forgotten_password');
  118.         }
  119.         return $this->render('security/reset_password.html.twig', [
  120.             'form' => $form->createView(),
  121.             'user' => $user
  122.         ]);
  123.     }
  124. }