src/StartPlatz/Bundle/UserBundle/EventSubscriber/ForcePasswordResetSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\StartPlatz\Bundle\UserBundle\EventSubscriber;
  3. use App\StartPlatz\Bundle\UserBundle\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. class ForcePasswordResetSubscriber implements EventSubscriberInterface
  11. {
  12.     private const ALLOWED_ROUTES = [
  13.         'password_reset_set',
  14.         'password_reset_submit',
  15.         'logout',
  16.         '_wdt',
  17.         '_profiler',
  18.     ];
  19.     public function __construct(
  20.         private readonly Security $security,
  21.         private readonly RouterInterface $router,
  22.     ) {
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::REQUEST => ['onKernelRequest', -1],
  28.         ];
  29.     }
  30.     public function onKernelRequest(RequestEvent $event): void
  31.     {
  32.         if (!$event->isMainRequest()) {
  33.             return;
  34.         }
  35.         $user $this->security->getUser();
  36.         if (!$user instanceof User || !$user->getMustResetPassword()) {
  37.             return;
  38.         }
  39.         $route $event->getRequest()->attributes->get('_route');
  40.         if (in_array($routeself::ALLOWED_ROUTEStrue)) {
  41.             return;
  42.         }
  43.         $event->getRequest()->getSession()->getFlashBag()->add(
  44.             'notice',
  45.             'Aus Sicherheitsgründen musst du ein neues Passwort setzen.'
  46.         );
  47.         $url $this->router->generate('password_reset_set');
  48.         $event->setResponse(new RedirectResponse($url));
  49.     }
  50. }