简体   繁体   中英

symfony2 disconnect/logout one user (not the current user)

In my application, i do need to disconnect a user programatically. Not the current user, but another one, knowing its id. I am using FOSUserBundle but I guess this could be good for any provider.

Token and sessions are relative to the current user and stored on its computer I guess.

So, how can I do that?

There is som related post here but the response does not match the request : FOSUserBundle/Symfony2: Force logout of a user given a user (not the currently logged in user)

It is possible only when you are NOT using native php session handler (which is default in symfony ). In this case you don't know other users session ids.

The way you could do that is using database as your session storage - there's a howto on symfony.com . Once you have session stored in database destroying user's session is just deleting proper row from database.

And yes - FosUserBundle has nothing to do here, it is not specific to this bundle

As far as I know, every time a page is loaded the user from the session is compared to the corresponding user on the database. If the comparison returns a false then the user is forced to log in again.

FOSUserBundle serializes a bunch of user data to be stored on the session including the enabled field. This mean that if you set the users enabled status to false then on the next page load the session enabled won't be equal to the database enabled meaning the users aren't equal leading to the user needing to log in again.

To do it without FOSUSerBundle you would just need to include some sort of enabled field in your users admin system and then check that on every page load in your serialize (or in the isEqualTo as shown in the docs ) and then you can log people out on next page load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM