vendor/kunstmaan/admin-bundle/Controller/Authentication/PasswordResetController.php line 30

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\AdminBundle\Controller\Authentication;
  3. use Kunstmaan\AdminBundle\Entity\UserInterface;
  4. use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
  5. use Kunstmaan\AdminBundle\Form\Authentication\NewPasswordType;
  6. use Kunstmaan\AdminBundle\Form\Authentication\PasswordRequestType;
  7. use Kunstmaan\AdminBundle\Service\PasswordResetService;
  8. use Kunstmaan\AdminBundle\Service\UserManager;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  13. final class PasswordResetController extends AbstractController
  14. {
  15.     /** @var PasswordResetService */
  16.     private $passwordResetService;
  17.     /** @var UserManager */
  18.     private $userManager;
  19.     public function __construct(PasswordResetService $passwordResetService, UserManager $userManager)
  20.     {
  21.         $this->passwordResetService = $passwordResetService;
  22.         $this->userManager = $userManager;
  23.     }
  24.     #[Route(path: '/password-reset/request', name: 'kunstmaan_admin_reset_password', methods: ['GET', 'POST'])]
  25.     public function requestAction(Request $request)
  26.     {
  27.         $form = $this->createForm(PasswordRequestType::class);
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             try {
  31.                 $this->passwordResetService->processResetRequest($form->get('email')->getData(), $request->getLocale());
  32.             } catch (UserNotFoundException $e) {
  33.                 // Don't expose if the user was not found to avoid leaking info.
  34.             }
  35.             $this->addFlash(FlashTypes::SUCCESS, 'security.resetting.send_email_success');
  36.             return $this->redirectToRoute('kunstmaan_admin_reset_password');
  37.         }
  38.         return $this->render('@KunstmaanAdmin/authentication/password_reset/request.html.twig', ['form' => $form->createView()]);
  39.     }
  40.     #[Route(path: '/password-reset/confirm/{token}', name: 'kunstmaan_admin_reset_password_confirm', methods: ['GET', 'POST'])]
  41.     public function confirmAction(Request $request, string $token)
  42.     {
  43.         /** @var UserInterface|null $user */
  44.         $user = $this->userManager->findUserByConfirmationToken($token);
  45.         if (null === $user) {
  46.             return $this->redirectToRoute('kunstmaan_admin_reset_password');
  47.         }
  48.         $form = $this->createForm(NewPasswordType::class);
  49.         $form->handleRequest($request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             $response = $this->passwordResetService->resetPassword($user, $form->get('plainPassword')->getData());
  52.             $this->addFlash(FlashTypes::SUCCESS, 'security.resetting.password_set_success');
  53.             return $response;
  54.         }
  55.         return $this->render('@KunstmaanAdmin/authentication/password_reset/confirm.twig', ['form' => $form->createView()]);
  56.     }
  57. }