logout a user by another user(admin) in laravel 5.

2019-02-20 00:59发布

lets say three user is currently logged from three device. one of the user(Admin) want to forcely logout user2 , how can it be achieved in laravel 5.2 ??

Database table structure :: users -> id|name|email|password|remember_token

Update scenario::

user1 --> logged from device 1 [user1 is admin type and has all kind of permission]
user2 --> logged from device 2 [normal user]
user3 --> logged from device 3 [normal user]

currently all user is logged simultaniously. user1 make some changes for user2 and want to re-login user2, there is no way to tell user2 please re-login except make him logout by user1. how can user1 make a user2(remote user) logout ??

Given solution analysis:

$userIdToLogout = 2; // it is user2

if (!is_null(Auth::user()) {
    if (Auth::user()->id == $userIdToLogout) {
        Auth::logout();
    }
}

- Auth::user() will return user1 information NOT user2
- So if (Auth::user()->id == $userIdToLogout) condition will never be true

is my analysis wrong ??

1条回答
Bombasti
2楼-- · 2019-02-20 01:52

The answer that Alexey Mezenin gave is correct. People are not looking at the other user perspective. Auth will return the current user information.

So for example, User1 wants to log user2 out, User1 clicks on a href link that will log the user2 out. When this link is clicked, $usersId will contain the id of user2. Now the function will be called when the link is being redirect to the route. Auth refers to the current session. But when user2 is login and his id belongs to the $usersID, his account will be logout.

if(Auth::check() && if (in_array(Auth()->id(), $usersId)) {
  Auth::logout()
}
查看更多
登录 后发表回答