src/Controller/SecurityController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use LogicException;
  4. use Oz\ApiNvl\Form\User\UserForgotPasswordEmailType;
  5. use Oz\ApiNvl\Form\User\UserForgotPasswordType;
  6. use Oz\ApiNvl\Model\User\UserForgotPassword;
  7. use Oz\ApiNvl\Model\User\UserForgotPasswordEmail;
  8. use Oz\ApiNvl\Service\User\UserForgotPasswordHelper;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. class SecurityController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/login", name="APP_LOGIN")
  18.      */
  19.     public function login(AuthenticationUtils $authenticationUtils): Response
  20.     {
  21.         // if ($this->getUser()) {
  22.         //     return $this->redirectToRoute('target_path');
  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('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  29.     }
  30.     /**
  31.      * @Route("/reset-password", name="RESET_PASSWORD")
  32.      */
  33.     public function resetPassword(Request $requestUserForgotPasswordHelper $userForgotPasswordHelper): Response
  34.     {
  35.         $userForgotPasswordEmail = new UserForgotPasswordEmail();
  36.         $form $this->createForm(UserForgotPasswordEmailType::class, $userForgotPasswordEmail);
  37.         $form->handleRequest($request);
  38.         if ($form->isSubmitted() && $form->isValid()) {
  39.             $userForgotPasswordHelper->sendEmail($userForgotPasswordEmail);
  40.             $this->addFlash('success''Un email vous a été envoyé pour modifier votre mot de passe.');
  41.             return $this->redirectToRoute('APP_LOGIN');
  42.         }
  43.         return $this->render('security/reset-password.html.twig', [
  44.             'form' => $form->createView(),
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/reset-password/submit/", name="RESET_PASSWORD_SUBMIT")
  49.      */
  50.     public function resetPasswordSubmit(Request $requestUserForgotPasswordHelper $userForgotPasswordHelper): Response
  51.     {
  52.         $userForgotPassword = (new UserForgotPassword())
  53.             ->setEmail($request->get('email'))
  54.             ->setToken($request->get('token'));
  55.         $form $this->createForm(UserForgotPasswordType::class, $userForgotPassword);
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             if ($userForgotPasswordHelper->changePassword($userForgotPassword)) {
  59.                 $this->addFlash('success''Votre mot de passe a bien été modifié.');
  60.             } else {
  61.                 $this->addFlash('danger''Votre mot de passe n\'a pas pu étre modifié.');
  62.             }
  63.             return $this->redirectToRoute('APP_LOGIN');
  64.         }
  65.         return $this->render('security/reset-password-submit.html.twig', [
  66.             'form' => $form->createView(),
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/admin/login", name="LOGIN_ADMIN")
  71.      * @param AuthenticationUtils $authenticationUtils
  72.      * @return Response
  73.      */
  74.     public function loginAdmin(AuthenticationUtils $authenticationUtils)
  75.     {
  76.         // if ($this->getUser()) {
  77.         //     return $this->redirectToRoute('target_path');
  78.         // }
  79.         // get the login error if there is one
  80.         $error $authenticationUtils->getLastAuthenticationError();
  81.         // last username entered by the user
  82.         $lastUsername $authenticationUtils->getLastUsername();
  83.         return $this->render('security/admin-login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  84.     }
  85.     /**
  86.      * @Route("/admin/check-login-admin", name="CHECK_LOGIN_ADMIN")
  87.      */
  88.     public function checkLoginAdmin()
  89.     {
  90.         throw new LogicException('This method can be blank - it will be intercepted by the check login.');
  91.     }
  92.     /**
  93.      * @Route("/logout", name="APP_LOGOUT")
  94.      */
  95.     public function logout(): void
  96.     {
  97.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  98.     }
  99.     /**
  100.      * @Route("/admin/logout", name="APP_ADMIN_LOGOUT")
  101.      */
  102.     public function logoutAdmin()
  103.     {
  104.         throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  105.     }
  106. }