<?php declare(strict_types=1);
namespace App\Subscriber;
use App\Entity\User;
use App\Event\UserRolesChangedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class UserRolesChangedSubscriber implements EventSubscriberInterface
{
private TokenStorageInterface $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function onRolesChanged(UserRolesChangedEvent $event): void
{
if (null === $token = $this->tokenStorage->getToken()) {
return;
}
$user = $token->getUser();
if (!$user instanceof User) {
return;
}
if ($user->getId() !== $event->getUser()->getId()) {
return;
}
if (!method_exists($token, 'getFirewallName')) {
return;
}
// use token user here, in case there are some references left.
$user->reset();
$this->tokenStorage->setToken(new UsernamePasswordToken($event->getUser(), $token->getFirewallName(), $user->getRoles()));
}
/**
* @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
*/
public static function getSubscribedEvents(): array
{
return [
UserRolesChangedEvent::class => 'onRolesChanged',
];
}
}