在laravel 5.2中由另一个用户(admin)注销用户 [英] logout a user by another user(admin) in laravel 5.2
问题描述
让我们说三个用户当前是从三个设备登录的.其中一个用户(管理员)想要强制注销user2,如何在laravel 5.2中实现?
数据库表结构:: users -> id|name|email|password|remember_token
更新 场景::
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]
当前所有用户都被同时登录. user1对user2进行了一些更改并想要重新登录user2,除了让user1注销外,无法告诉user2请重新登录. user1如何使user2(远程用户)注销??
给出解决方案分析:
$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
我的分析错了吗?
Alexey Mezenin给出的答案是正确的.人们不是在看其他用户的观点. Auth将返回当前的用户信息.
例如,User1要注销user2,User1单击href链接将注销user2.单击此链接后,$ usersId将包含user2的ID.现在,当链接被重定向到路由时,将调用该函数.身份验证是指当前会话.但是,当user2登录后,其ID属于$ usersID时,其帐户将被注销.
if(Auth::check() && if (in_array(Auth()->id(), $usersId)) {
Auth::logout()
}
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 ??
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()
}
这篇关于在laravel 5.2中由另一个用户(admin)注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!