vendor/arshdf/user-bundle-sf5/src/Controller/SecurityController.php line 40

Open in your IDE?
  1. <?php
  2. namespace ARSHDF\UserBundle\Controller;
  3. use ARSHDF\UserBundle\Entity\PasswordResetToken;
  4. use ARSHDF\UserBundle\Entity\UserManager;
  5. use ARSHDF\UserBundle\Form\ResetPasswordType;
  6. use ARSHDF\UserBundle\Form\UserNewPasswordType;
  7. use ARSHDF\UserBundle\Service\LogService;
  8. use ARSHDF\UserBundle\Service\PasswordService;
  9. use Exception;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\Form\FormError;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. class SecurityController extends AbstractController
  19. {
  20.     private PasswordService $passwordService;
  21.     private UserManager $userManager;
  22.     private ParameterBagInterface $parameterBag;
  23.     private LogService $logService;
  24.     public function __construct(PasswordService $passwordServiceUserManager $userManagerParameterBagInterface $parameterBagLogService $logService)
  25.     {
  26.         $this->passwordService $passwordService;
  27.         $this->userManager $userManager;
  28.         $this->parameterBag $parameterBag;
  29.         $this->logService $logService;
  30.     }
  31.     /**
  32.      * @Route("/login", name="app_login")
  33.      */
  34.     public function login(AuthenticationUtils $authenticationUtils): Response
  35.     {
  36.          if ($this->getUser()) {
  37.              return $this->redirectToRoute($this->parameterBag->get('user.gestion_admin.main_path'));
  38.          }
  39.         // get the login error if there is one
  40.         $error $authenticationUtils->getLastAuthenticationError();
  41.         // last username entered by the user
  42.         $lastUsername $authenticationUtils->getLastUsername();
  43.         return $this->render('@User/security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  44.     }
  45.     /**
  46.      * @Route("/logout", name="app_logout")
  47.      */
  48.     public function logout()
  49.     {
  50.     }
  51.     /**
  52.      * @throws Exception
  53.      */
  54.     public function reset(AuthenticationUtils $authenticationUtilsRequest $request){
  55.         $lastUsername $authenticationUtils->getLastUsername();
  56.         $error null;
  57.         $form $this->createForm(ResetPasswordType::class, null, ['lastusername' => $lastUsername]);
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted() && $form->isValid()){
  60.             try {
  61.                 $this->passwordService->resetPassword($form->get('email')->getData());
  62.                 $this->addFlash('success''Les instructions pour réinitialiser votre mot de passe vous ont été envoyées');
  63.                 return $this->redirectToRoute('arshdf_user_login');
  64.             } catch (Exception $e) {
  65.                 $error $e;
  66.             }
  67.         }
  68.         return $this->render('@User/security/password_reset.html.twig', [
  69.             'form' => $form->createView(),
  70.             'error' => $error,
  71.         ]);
  72.     }
  73.     /**
  74.      * @throws Exception
  75.      */
  76.     public function confirmReset($idstring $tokenRequest $request){
  77.         $user $this->userManager->findUserById($id);
  78.         $user_by_token $this->userManager->findOneByToken($token);
  79.         $password_expired explode("."$user->getToken())[0] === "r";
  80.         if (!$token || $this->passwordService->isExpired($user) || $user_by_token !== $user) {
  81.             $this->addFlash('error''Ce token a expiré');
  82.             return $this->redirectToRoute('arshdf_user_login');
  83.         }
  84.         $error null;
  85.         $form $this->createForm(UserNewPasswordType::class, $user);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted() && $form->isValid()) {
  88.             if (!$this->passwordService->validateStrength($form->get('plainPassword')->getData())){
  89.                 $form->get('plainPassword')->addError(new FormError($this->passwordService->getPasswordPatternMessage()));
  90.                 return $this->render('@User/security/password_reset_confirm.html.twig', [
  91.                     'error' => $error,
  92.                     'form' => $form->createView(),
  93.                     'password_expired' => $password_expired,
  94.                     'user' => $user,
  95.                 ]);
  96.             }
  97.             $this->passwordService->updatePassword($form->get('plainPassword')->getData(), $user);
  98.             $this->addFlash('success''Votre mot de passe a bien été réinitialisé');
  99.             $this->logService->generate(LogService::USER_PASSWORD_RESETED$user);
  100.             return $this->redirectToRoute('arshdf_user_login');
  101.         }
  102.         return $this->render('@User/security/password_reset_confirm.html.twig', [
  103.             'error' => $error,
  104.             'form' => $form->createView(),
  105.             'password_expired' => $password_expired,
  106.             'user' => $user,
  107.         ]);
  108.     }
  109. }