src/Controller/SecurityController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. class SecurityController extends AbstractController{
  11.     /**
  12.      * @Route("/login", name="login")
  13.      */
  14.     public function login(AuthenticationUtils $authenticationUtils)
  15.     {
  16.         $error$authenticationUtils->getLastAuthenticationError();
  17.         $lastUsername$authenticationUtils->getLastUsername();
  18.         return $this->render('security/login.html.twig', array('error'=>$error'lastUsername'=>$lastUsername));
  19.     }
  20.     /**
  21.      * @Route(name="api_login_check", path="/api/login_check")
  22.      * @return Jsonresponse
  23.      */
  24.     /*public function api_login(): JsonResponse
  25.     {
  26.         $user= $this->getUser();
  27.         return new Response([
  28.             'email'=> $user->getEmail(),
  29.             'roles'=> $user->getRoles(),
  30.         ]);
  31.     }*/
  32.     
  33.     /**
  34.      * @Route("/change-password", name="app_user_change_password")
  35.      */
  36.     public function changePassword(Request $requestEntityManagerInterface $emUserPasswordHasherInterface $passwordHasher): Response
  37.     {
  38.         $user $this->getUser();
  39.         if (!$user) {
  40.             return $this->redirectToRoute('login');
  41.         }
  42.         // Si déjà changé, on redirige vers la page d’accueil
  43.         /*if ($user->isPasswordChanged()) {
  44.             return $this->redirectToRoute('app_dashboard');
  45.         }*/
  46.         if ($request->isMethod('POST')) {
  47.             $newPassword $request->request->get('new_password');
  48.             $confirmPassword $request->request->get('confirm_password');
  49.             if ($newPassword && $newPassword === $confirmPassword) {
  50.                 $hashedPassword $passwordHasher->hashPassword($user$newPassword);
  51.                 $user->setPassword($hashedPassword);
  52.                 $user->setPasswordChanged(true);
  53.                 $em->flush();
  54.                 $this->addFlash('success''Votre mot de passe a été changé avec succès.');
  55.                 return $this->redirectToRoute('app_dashboard');
  56.             }
  57.             $this->addFlash('danger''Les mots de passe ne correspondent pas.');
  58.         }
  59.         return $this->render('security/change_password.html.twig', ['user' => $user]);
  60.     }
  61. }