src/Controller/Account/ForgotPassword/AskController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Account\ForgotPassword;
  3. use App\Repository\Account\UserRepository;
  4. use App\Service\Mailer\MailerService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Nellapp\Bundle\SDKBundle\Routing\Utils\ChannelMainDomainUtils;
  7. use Nellapp\Bundle\SDKBundle\Sync\Exception\ExceptionInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  14. use Symfony\Component\Uid\Uuid;
  15. class AskController extends AbstractController
  16. {
  17.     const SESSION_EMAIL_KEY __CLASS__;
  18.     public function __construct(
  19.         private AuthenticationUtils    $authenticationUtils,
  20.         private UserRepository         $userRepository,
  21.         private SessionInterface       $session,
  22.         private EntityManagerInterface $entityManager,
  23.         private MailerService          $mailerService,
  24.         private ChannelMainDomainUtils $channelMainDomainUtils,
  25.     )
  26.     {
  27.     }
  28.     #[Route(path'/forgot-password/ask'name'forgot_password_ask')]
  29.     public function __invoke(Request $request): Response
  30.     {
  31.         if ($this->isGranted('ROLE_USER')) {
  32.             return $this->redirectToRoute('account_home');
  33.         }
  34.         $email $request->request->get('email');
  35.         if ($request->isMethod(Request::METHOD_POST) && null !== $email) {
  36.             if (null === $user $this->userRepository->findOneBy(['email' => $email])) {
  37.                 $error 'app.security.forgotPassword.ask.user.notFound';
  38.             } else {
  39.                 try {
  40.                     $user->setPasswordToken(Uuid::v1());
  41.                     $user->setPasswordTokenAt(new \DateTimeImmutable());
  42.                     $this->entityManager->persist($user);
  43.                     $this->entityManager->flush();
  44.                     $sended $this->mailerService->sendForgotPasswordMail($user);
  45.                     if ($sended) {
  46.                         // Add user email to session to print it in success page.
  47.                         $this->session->set(self::SESSION_EMAIL_KEY$user->getEmail());
  48.                         return $this->redirectToRoute('forgot_password_ask_success');
  49.                     }
  50.                     // Remove token if cannot send email.
  51.                     $user->setPasswordToken(null);
  52.                     $user->setPasswordTokenAt(null);
  53.                     $this->entityManager->persist($user);
  54.                     $this->entityManager->flush();
  55.                     $error 'app.security.forgotPassword.ask.mail.cannotSend';
  56.                 } catch (ExceptionInterface $e) {
  57.                     $error 'app.security.forgotPassword.ask.token.alreadyRequested';
  58.                 }
  59.             }
  60.         }
  61.         $lastUsername $email ?: $this->authenticationUtils->getLastUsername();
  62.         return $this->render('account/ForgotPassword/ask.html.twig', [
  63.             'error' => $error ?? false,
  64.             'last_username' => $lastUsername,
  65.             'channel' => $this->channelMainDomainUtils->getChannelFromRequest(),
  66.         ]);
  67.     }
  68. }