src/Controller/SecurityController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ChangePasswordType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. class SecurityController extends AbstractController
  13. {
  14.     #[Route(path'/login'name'app_login')]
  15.     public function login(AuthenticationUtils $authenticationUtils): Response
  16.     {
  17.         if ($this->getUser()) {
  18.             if ($this->isGranted('ROLE_HOTESSE')) {
  19.                 return $this->redirectToRoute('app_register');
  20.             } else {
  21.                 return $this->redirectToRoute('admin_index');
  22.             }
  23.         }
  24.         // get the login error if there is one
  25.         $error $authenticationUtils->getLastAuthenticationError();
  26.         // last username entered by the user
  27.         $lastUsername $authenticationUtils->getLastUsername();
  28.         return $this->render('registration/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  29.     }
  30.     #[Route(path'/logout'name'app_logout')]
  31.     public function logout(): void
  32.     {
  33.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  34.     }
  35.     #[Route(path'/changePassword/{qrcode}/{id}/{email}'name'app_changePassword')]
  36.     public function changePassword($qrcode$id$emailEntityManagerInterface $entityManagerRequest $requestUserPasswordHasherInterface $userPasswordHasher): Response
  37.     {
  38.         //Si le QRCODE et l'ID ne sont pas des entiers on redirige.
  39.         if ($qrcode) {
  40.             $user $entityManager->getRepository(User::class)->findByQrCode($id);
  41.             if ($user->getQrCode() * 100 != $qrcode) {
  42.                 throw new \LogicException('Il y a un problème. Veuillez réessayer plus tard.');
  43.             }
  44.             if ($user->getEmail() != $email) {
  45.                 throw new \LogicException('Il y a un problème. Veuillez réessayer plus tard.');
  46.             }
  47.             if ($qrcode && $email && $id) {
  48.                 $form $this->createForm(ChangePasswordType::class, $user);
  49.                 $form->handleRequest($request);
  50.                 if ($form->isSubmitted() && $form->isValid()) {
  51.                     $user->setPassword(
  52.                         $userPasswordHasher->hashPassword(
  53.                             $user,
  54.                             $user->getPassword()
  55.                         )
  56.                     );
  57.                     $entityManager->persist($user);
  58.                     $entityManager->flush();
  59.                     return $this->redirectToRoute('changePasswordOk');
  60.                 }
  61.             }
  62.         } else {
  63.             throw new \LogicException('Il y a un problème. Veuillez réessayer plus tard.');
  64.         }
  65.         return $this->render('registration/changePassword.html.twig', [
  66.             'passwordForm' => $form->createView(),
  67.             'user' => $user,
  68.         ]);
  69.     }
  70.     #[Route(path'/changePassword/ok'name'changePasswordOk')]
  71.     public function changePasswordOk()
  72.     {
  73.         return $this->render('error/changePasswordOk.html.twig');
  74.     }
  75. }