Symfony - How to logout locked or banned User automatically
If you want to logout locked or banned User automatically, you need, first of all, add new Listener with onKernelRequest(RequestEvent $event) method. Just FYI, this method Symfony calls every time on every request, so, be careful to use this method. It can shoot yourself in the foot. For example: class SecurityUserListener { public function onKernelRequest(RequestEvent $event) { } } After that, we need to check our User on status: banned or locked (what you need). For this we need to inject Token Storage in class constructor. Token Storage can return User. So, lets do it: class SecurityUserListener { public function __construct(private TokenStorageInterface $tokenStorage) { } public function onKernelRequest(RequestEvent $event) { $user = $this->tokenStorage->getToken()?->getUser(); } } Now, you can examine the user object's status. If it meets your criteria, set the current token to null (indicating no user in the token anymore) and...
Comments