src/Controller/SecurityController.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\CreatePasswordType;
  5. use App\Form\PasswordForgotType;
  6. use App\Repository\UserRepository;
  7. use App\Service\MailerService;
  8. use Doctrine\ORM\NoResultException;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. class SecurityController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/", name="app_login")
  22.      */
  23.     public function login(AuthenticationUtils $authenticationUtils): Response
  24.     {
  25.         if ($this->getUser() && $this->isGranted('ROLE_SUPERADMIN_RECYCLINK')) {
  26.             return $this->redirectToRoute('super_admin_recyclink');
  27.         }
  28.         if ($this->getUser() && $this->isGranted('ROLE_USER') || $this->getUser() && $this->isGranted('ROLE_ADMIN')) {
  29.             return $this->redirectToRoute('client_index');
  30.         }
  31.         // get the login error if there is one
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         // last username entered by the user
  34.         $lastUsername $authenticationUtils->getLastUsername();
  35.         return $this->render('security/login.html.twig', [
  36.             'last_username' => $lastUsername,
  37.             'error' => $error,
  38.         ]);
  39.     }
  40.     /**
  41.      * @Route("/logout", name="app_logout")
  42.      */
  43.     public function logout()
  44.     {
  45.         throw new \LogicException(
  46.             'This method can be blank - it will be intercepted by the logout key on your firewall.'
  47.         );
  48.     }
  49.     /**
  50.      * @Route("/create-password/{slug}", name="app_create_password")
  51.      */
  52.     public function initializePassword(User $userRequest $requestUserPasswordHasherInterface $hasher)
  53.     {
  54.         if (! $user->getCreatePassword() === false) {
  55.             throw new UnauthorizedHttpException("Vous n'êtes pas autoriser à venir ici.");
  56.         }
  57.         $form $this->createForm(CreatePasswordType::class, $user);
  58.         $form->handleRequest($request);
  59.         if (! $form->isSubmitted() || ! $form->isValid()) {
  60.             return $this->render('security/create-password.html.twig', [
  61.                 'form' => $form->createView(),
  62.             ]);
  63.         }
  64.         $user->setPassword($hasher->hashPassword($user$form->get('password')->getData()));
  65.         $this->getDoctrine()->getManager()->flush();
  66.         $this->addFlash('create_password'"Votre mot de passe à bien été créé.");
  67.         return $this->redirectToRoute('app_login');
  68.     }
  69.     #[Route("/reset-password"name"app_reset_password_request")]
  70.     public function reinitializePasswordRequest(
  71.         UserRepository $repository,
  72.         Request $request,
  73.         MailerService $mailerService,
  74.         UrlGeneratorInterface $urlGenerator
  75.     ) {
  76.         $form $this->createForm(PasswordForgotType::class);
  77.         $form->handleRequest($request);
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $user $repository->findOneBy([
  80.                 'email' => $form['email']->getData(),
  81.             ]);
  82.             $this->addFlash(
  83.                 'email',
  84.                 "Nous avons bien enregistrer votre demande, si votre email est enregistré vous recevrais un mail."
  85.             );
  86.             if ($user instanceof User) {
  87.                 $uuid $user->getResetToken() === null ?
  88.                     uniqid($user->getSlug()) :
  89.                     $user->getResetToken();
  90.                 $path $urlGenerator->generate(
  91.                     'app_reset_password_request',
  92.                     ['uuid'$uuid],
  93.                     UrlGeneratorInterface::ABSOLUTE_URL
  94.                 );
  95.                 $user->setResetToken($uuid);
  96.                 $mailerService->sendEmailToResetPassword($path$user->getEmail());
  97.                 $this->getDoctrine()->getManager()->flush();
  98.             }
  99.             return $this->redirectToRoute('app_login');
  100.         }
  101.         return $this->renderForm('security/reset-password-request.html.twig', [
  102.             'form' => $form,
  103.         ]);
  104.     }
  105.     #[Route("/reset-password/{uuid}"name"app_reset_password")]
  106.     public function reinitializePassword(
  107.         UserRepository $repository,
  108.         Request $request,
  109.         UserPasswordHasherInterface $hasher
  110.     ) {
  111.         $user $repository->findOneBy([
  112.             'resetToken' => $request->get('uuid'),
  113.         ]);
  114.         if (! $user instanceof User) {
  115.             throw new NoResultException();
  116.         }
  117.         $form $this->createForm(CreatePasswordType::class);
  118.         $form->handleRequest($request);
  119.         if ($form->isSubmitted() && $form->isValid()) {
  120.             $user->setPassword($hasher->hashPassword($user$form->get('password')->getData()));
  121.             $user->setResetToken(null);
  122.             $this->getDoctrine()->getManager()->flush();
  123.             $this->addFlash('success'"Password mis à jour.");
  124.             return $this->redirectToRoute('app_login');
  125.         }
  126.         return $this->renderForm('security/reset-password.html.twig', [
  127.             'form' => $form,
  128.         ]);
  129.     }
  130. }