src/Controller/Security/SecurityController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Entity\Users;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  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.     private UserPasswordHasherInterface $passwordEncoder;
  15.     public function __construct(UserPasswordHasherInterface $passwordEncoder)
  16.     {
  17.         $this->passwordEncoder $passwordEncoder;
  18.     }
  19.     #[Route(path'/login'name'app_login'options: ['expose' => true])]
  20.     public function login(AuthenticationUtils $authenticationUtils): Response
  21.     {
  22.         if ($this->getUser()) {
  23.             return $this->redirectToRoute('app_logout');
  24.         }
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  30.     }
  31.     #[Route(path'/app_changement_password'name'app_changement_password'options: ['expose' => true])]
  32.     public function app_changement_password(Request $requestManagerRegistry $doctrine): Response
  33.     {
  34.         $em $doctrine->getManager();
  35.         $user $em->getRepository(Users::class)->find($this->getUser()->getId());
  36.         if(!$this->passwordEncoder->isPasswordValid($user$request->get("mdp_current"))) {
  37.             return new JsonResponse("Votre mot de passe actuel est incorrect !"500);
  38.         }
  39.         // dd($request->get('mdp'), $request->get('mdp_confirmation'));
  40.         if($request->get('mdp') === $request->get('mdp_confirmation')) {
  41.             $user->setPassword($this->passwordEncoder->hashPassword(
  42.                 $user,
  43.                 $request->get('mdp_confirmation')
  44.             ));
  45.             
  46.             $user->setUserUpdated($this->getUser());
  47.             $user->setUpdated(new \DateTime());
  48.             $em->flush();
  49.             return new JsonResponse("Bien Enregistre!"200);
  50.         }
  51.         return new JsonResponse('Les mots de passe ne correspondent pas'500);
  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. }