src/Bundles/UserBundle/Controller/PasswordController.php line 78

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundles\UserBundle\Controller;
  4. use App\Bundles\UserBundle\DTO\ChangePasswordDTO;
  5. use App\Bundles\UserBundle\DTO\ForgotPasswordDTO;
  6. use App\Bundles\UserBundle\Entity\UserInterface;
  7. use App\Bundles\UserBundle\Exception\PasswordAlreadyUsedException;
  8. use App\Bundles\UserBundle\Exception\PasswordValidationException;
  9. use App\Bundles\UserBundle\Exception\UserNotFoundException;
  10. use App\Bundles\UserBundle\Form\Type\UserPassword\ChangePasswordForm;
  11. use App\Bundles\UserBundle\Form\Type\UserPassword\ForgotPasswordType;
  12. use App\Bundles\UserBundle\Service\User\UserProvider;
  13. use App\Bundles\UserBundle\Service\UserEmail\UserEmailSender;
  14. use App\Bundles\UserBundle\Service\UserPassword\ForgottenPasswordService;
  15. use App\Bundles\UserBundle\Service\UserPassword\UserPasswordService;
  16. use App\Platform\Service\SessionProvider;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpFoundation\Session\Session;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. class PasswordController extends AbstractController
  24. {
  25.     public function __construct(
  26.         private readonly UserEmailSender $userEmailSender,
  27.         private readonly UserProvider $provider,
  28.         private readonly UserPasswordService $service,
  29.         private readonly TranslatorInterface $translator,
  30.         private readonly ForgottenPasswordService $passwordService,
  31.         private readonly SessionProvider $sessionProvider,
  32.     ) {
  33.     }
  34.     #[Route(path'/password'name'app_password'methods: ['GET''POST'])]
  35.     public function changePassword(Request $request): Response
  36.     {
  37.         $form $this->createForm(ChangePasswordForm::class);
  38.         $form->handleRequest($request);
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             /** @var UserInterface $user */
  41.             $user $this->getUser();
  42.             /** @var ChangePasswordDTO $dto */
  43.             $dto $form->getData();
  44.             try {
  45.                 $this->service->setNewPasswordForUser($user$dto);
  46.             } catch (PasswordAlreadyUsedException PasswordValidationException $ex) {
  47.                 $this->addFlash('error'$this->translator->trans($ex->getMessage(), [], 'UserBundle'));
  48.             }
  49.             return $this->redirectToRoute('users.index');
  50.         }
  51.         return $this->renderForm('@User/password/change_password_form.html.twig'compact('form'));
  52.     }
  53.     #[Route(path'/password/user'name'app_request_change_password'methods: ['POST'])]
  54.     public function sendRequestForUser(Request $request): Response
  55.     {
  56.         $user $this->provider->provide((int) $request->get('id'));
  57.         $this->userEmailSender->sendEmailWithRequestForChangePassword(
  58.             $user,
  59.             'email_reset_password_by_admin',
  60.             $this->translator->trans(id'email.subject.email_reset_password_by_admin'domain'UserBundle')
  61.         );
  62.         return $this->redirectToRoute('users.show', ['id' => $user->getId()]);
  63.     }
  64.     #[Route(path'/password/forgot'name'app_forgot_password'methods: ['GET''POST'])]
  65.     public function forgotPassword(Request $request): Response
  66.     {
  67.         if ($this->getUser()) {
  68.             return $this->redirectToRoute('app.home');
  69.         }
  70.         $form $this->createForm(ForgotPasswordType::class);
  71.         $form->handleRequest($request);
  72.         if ($form->isSubmitted() && $form->isValid()) {
  73.             /** @var ForgotPasswordDTO $formData */
  74.             $formData $form->getData();
  75.             try {
  76.                 $this->passwordService->restorePassword($formData);
  77.             } catch (UserNotFoundException) {
  78.                 $this->addFlash(
  79.                     'error',
  80.                     $this->translator->trans(id'auth.password_restored.bad_credentials'domain'UserBundle')
  81.                 );
  82.                 return $this->redirectToRoute('app_forgot_password');
  83.             }
  84.             $this->addFlash('sended_email'$formData->getEmail());
  85.             return $this->redirectToRoute('app_forgot_password_success');
  86.         }
  87.         return $this->renderForm('@User/password/forgot_password_form.html.twig'compact('form'));
  88.     }
  89.     #[Route(path'/password/forgot/success'name'app_forgot_password_success'methods: ['GET''POST'])]
  90.     public function forgotPasswordEmailSuccess(Request $request): Response
  91.     {
  92.         /** @var Session $session */
  93.         $session $this->sessionProvider->provide();
  94.         $bag $session->getFlashBag()->get('sended_email');
  95.         if (empty($bag)) {
  96.             return $this->redirectToRoute('app_login');
  97.         }
  98.         $email $bag[0];
  99.         return $this->render('@User/password/restore_password_success.html.twig'compact('email'));
  100.     }
  101. }