Answer the question
In order to leave comments, you need to log in
How to update user session on role change in Symfony?
When changing the role of a user from the admin panel, his rights should change, but this does not happen without re-authorization and authorization of the user on his own, since his data is in the current authorization session. I tried different solutions:
1. Invalidate the session: $this->session->invalidate();
2. Refresh the user by the provider: $this->userProvider->refreshUser($user);
Has anyone done this on their project, and if so, how did you solve the problem?
Answer the question
In order to leave comments, you need to log in
Issue resolved:
class RequestSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onRequest'
];
}
/**
* @param GetResponseEvent $event
*/
public function onRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
if (!$token = $this->tokenStorage->getToken()) {
return;
}
$sessionUser = $token->getUser();
if ($sessionUser instanceof User) {
$this->tokenStorage->setToken(
new PostAuthenticationGuardToken($sessionUser, 'main', $sessionUser->getRoles())
);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question