src/Controller/User/UserController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller\User;
  3. use App\Form\Type\ChangePasswordType;
  4. use App\Form\Type\EmailRecoverPasswordType;
  5. use App\Service\User\UserService;
  6. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. class UserController extends AbstractController
  12. {
  13.     private $translator;
  14.     private $userService;
  15.     private $adminUrlGenerator;
  16.     public function __construct(
  17.         TranslatorInterface $translator,
  18.         UserService $userService,
  19.         AdminUrlGenerator $adminUrlGenerator
  20.     ) {
  21.         $this->translator $translator;
  22.         $this->userService $userService;
  23.         $this->adminUrlGenerator $adminUrlGenerator;
  24.     }
  25.     public function recoverPassword(Request $request): Response
  26.     {
  27.         $form $this->createForm(EmailRecoverPasswordType::class);
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             $email $form['email']->getData();
  31.             if (!$this->userService->checkIfExistsEmail($email) || !$this->userService->checkIfUserIsActive($email)) {
  32.                 $this->addFlash(
  33.                     'danger',
  34.                     $this->translator->trans('pages.recover_password.error-email-no-exist')
  35.                 );
  36.                 return $this->render('pages/password/recover.html.twig', [
  37.                     'form' => $form->createView(),
  38.                     'page_title' => $this->translator->trans('pages.recover_password.title'),
  39.                 ]);
  40.             }
  41.             $this->userService->assignTokenToUserByEmail($email);
  42.             $this->userService->sendRecoverPasswordEmail($email);
  43.             $this->addFlash(
  44.                 'success',
  45.                 $this->translator->trans('pages.recover_password.send-email')
  46.             );
  47.             return $this->redirectToRoute('app_login');
  48.         }
  49.         return $this->render('pages/password/recover.html.twig', [
  50.             'form' => $form->createView(),
  51.             'page_title' => $this->translator->trans('pages.recover_password.title'),
  52.         ]);
  53.     }
  54.     public function changePasswordByRecoveryEmail(Request $requeststring $token): Response
  55.     {
  56.         if (!$this->userService->checkIfCanChangePassword($token)) {
  57.             throw $this->createNotFoundException($this->translator->trans('error_pages.exception.not_found'));
  58.         }
  59.         $form $this->createForm(ChangePasswordType::class);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted() && $form->isValid()) {
  62.             $firstPassword $form['password']->getData();
  63.             if ($this->userService->isNewPasswordSameAsOldPasswordByToken($firstPassword$token)) {
  64.                 $this->addFlash(
  65.                     'danger',
  66.                     $this->translator->trans('pages.change_password.same')
  67.                 );
  68.                 return $this->render('pages/password/change.html.twig', [
  69.                     'form' => $form->createView(),
  70.                     'page_title' => $this->translator->trans('pages.recover_password.title'),
  71.                 ]);
  72.             }
  73.             $this->userService->updatePasswordByToken($firstPassword$token);
  74.             $this->addFlash(
  75.                 'success',
  76.                 $this->translator->trans('pages.change_password.correct')
  77.             );
  78.             return $this->redirectToRoute('app_login');
  79.         }
  80.         return $this->render('pages/password/change.html.twig', [
  81.             'form' => $form->createView(),
  82.             'page_title' => $this->translator->trans('pages.recover_password.title'),
  83.         ]);
  84.     }
  85. }