src/Controller/AuthentificationController.php line 113

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Doctrine\ORM\EntityRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use App\Entity\User;
  13. use Symfony\Component\Security\Http\Attribute\IsGranted;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. class AuthentificationController extends AbstractController
  18. {
  19.     #[Route('/'name'connexion')]
  20.     public function index(AuthenticationUtils $authUtils): Response
  21.     {
  22.         if ($this->getUser()) {
  23.             return $this->redirectToRoute('navigation');
  24.         }
  25.         return $this->render('auth/login.html.twig', [
  26.             'error'         => $authUtils->getLastAuthenticationError(),
  27.             'last_username' => $authUtils->getLastUsername(),
  28.         ]);
  29.     }
  30.     #[Route('/app_connexion'name'app_connexion')]
  31.     public function authentification(Request $request)
  32.     {
  33.         $login $request->request->get('login');
  34.         $password $request->request->get('password');
  35.         if (!$login || !$password) {
  36.             
  37.             $this->addFlash('warning'"login et mot de passe requis");
  38.             return $this->redirectToRoute('connexion');
  39.             
  40.         }
  41.         $user $em->getRepository(User::class)->findOneBy(['email' => $login]);
  42.         if($user->getStatus() ==0)
  43.         {
  44.             $erreur=1;
  45.             $this->addFlash('warning'"Utilisateur inactif");
  46.             return $this->redirectToRoute('connexion');
  47.             
  48.         }elseif($user->getStatus() == 2)
  49.         {
  50.             $erreur=1;
  51.             return $this->render('auth/login.html.twig', [
  52.                 'controller_name' => 'AuthentificationController',
  53.                 'erreur' => $erreur,
  54.                 'messae' => 'Compte bloque',
  55.             ]);
  56.         }
  57.         // dd($user,$passwordHasher->isPasswordValid($user, $password));
  58.         if (!$user || !$passwordHasher->isPasswordValid($user$password)) {
  59.             $erreur=1;
  60.             return $this->render('auth/login.html.twig', [
  61.                 'controller_name' => 'AuthentificationController',
  62.                 'erreur' => $erreur,
  63.                 'messae' => 'Identifiants invalides',
  64.             ]);
  65.         }
  66.         if ($user->isPremiereConnexion() == ) {
  67.             return $this->render('auth/changerPassword.html.twig', [
  68.                 'controller_name' => 'AuthentificationController',
  69.                 'erreur' => $erreur,
  70.                 'messae' => "Veuillez changer votre mot de passe",
  71.             ]);
  72.         }
  73.         return $this->redirectToRoute('navigation');
  74.     }
  75.     #[Route('/changerPassword'name'changer_password'methods: ['GET''POST'])]
  76.     public function changerPassword(Request $requestEntityManagerInterface $emUserPasswordHasherInterface $passwordHasher): Response
  77.     {
  78.         $user $this->getUser();
  79.         if (!$user instanceof User) {
  80.             return $this->redirectToRoute('connexion');
  81.         }
  82.         if ($request->isMethod('POST')) {
  83.             $newPassword     $request->request->get('new_password');
  84.             $confirmPassword $request->request->get('confirm_password');
  85.             if (!$newPassword || $newPassword !== $confirmPassword) {
  86.                 $this->addFlash('warning''Les mots de passe ne correspondent pas.');
  87.                 return $this->redirectToRoute('changer_password');
  88.             }
  89.             $user->setPassword($passwordHasher->hashPassword($user$newPassword));
  90.             $user->setPremiereConnexion(true);
  91.             $em->flush();
  92.             return $this->redirectToRoute('navigation');
  93.         }
  94.         return $this->render('auth/changerPassword.html.twig');
  95.     }
  96.     #[Route('/passOublier'name'passOublier')]
  97.     public function PassOublier(): Response
  98.     {
  99.         return $this->render('auth/forget_password.html.twig', [
  100.             'controller_name' => 'AuthentificationController',
  101.         ]);
  102.     }
  103.     /**
  104.     * @Route("/avantdeconnexion", name="avantdeconnexion")
  105.     */
  106.     public function avantdeconnexion()
  107.     {
  108.         return $this->redirectToRoute('deconnexion');
  109.     }
  110.    /**
  111.     * @Route("/deconnexion", name="deconnexion")
  112.     */
  113.     public function deconnexion()
  114.     {
  115.     
  116.     }
  117. }