<?php
declare(strict_types=1);
namespace App\Bundles\UserBundle\EventListener\UserPassword;
use App\Bundles\UserBundle\Entity\UserInterface;
use App\Bundles\UserBundle\EventListener\UserModifierListener;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
#[AsEventListener(event: RequestEvent::class, method: 'onKernelRequest', priority: 1)]
class PasswordRequestEventListener
{
public function __construct(
private readonly Security $security,
private readonly RouterInterface $router,
) {
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
if ($request->attributes->get('_route') !== UserModifierListener::CHANGE_PASSWORD_PATH_NAME) {
return;
}
/** @var UserInterface $user */
$user = $this->security->getUser();
if (!$user->hasExpiredPassword()) {
$url = $this->router->generate('users.index');
$event->setResponse(new RedirectResponse($url));
}
}
}