src/EventListener/ForgotPasswordEventListener.php line 19

  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  5. use Symfony\Bundle\SecurityBundle\Security;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class ForgotPasswordEventListener
  8. {
  9.     private $security;
  10.     public function __construct(Security $security)
  11.     {
  12.         $this->security $security;
  13.     }
  14.     public function onKernelRequest(RequestEvent $event): void
  15.     {
  16.         if (!$event->isMainRequest()
  17.             || !preg_match('/^forgot_password/i'$event->getRequest()->get('_route'))
  18.         ) {
  19.             return;
  20.         }
  21.         // User should not be authenticated on forgot password
  22.         if (null !== $this->security->getToken() && $this->security->getUser() instanceof UserInterface) {
  23.             throw new AccessDeniedHttpException();
  24.         }
  25.     }
  26. }