Laravel 以其他用户身份登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45705087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel login as another user
提问by Paul Ryan
I am currently developing a laravel app where there are 3 user_roles
我目前正在开发一个 Laravel 应用程序,其中有 3 user_roles
- Superadmin
- Admin
- Normal
- 超级管理员
- 行政
- 普通的
So each role can access the roles below him.
所以每个角色都可以访问他下面的角色。
e.g
例如
Superadmins can access admins and normal users account.
超级管理员可以访问管理员和普通用户帐户。
How do I allow a authenticated superadmin user to log in as an admin or normal user with a click of a button?
如何允许经过身份验证的超级管理员用户通过单击按钮以管理员或普通用户身份登录?
USER_ROLES TABLE
id name
1 superadmin
2 admin
3 normal
----------------------------
USERS TABLE
id first_name last_name user_role_id password
1 john doe 1 *******
2 jane doe 2 *******
3 cassie snow 3 *******
4 sansa stark 3 *******
回答by Vladimir verleg
You can use the following methods to log in any user
您可以使用以下方法登录任何用户
$userId = 1;
Auth::loginUsingId($userId, true);
or
或者
$user = User::find(1);;
Auth::login($user);
If you have set up roles in your user model you could use something like
如果您在用户模型中设置了角色,则可以使用类似
//check if the current user is superadmin
$userRoles = Auth::user()->getRoleNames()->toArray();
if (in_array('superadmin', $userRoles)) {
//login the user
Auth::login($user);
}
回答by cre8
Reading the comments I think you want to do the following:
阅读评论我认为您想要执行以下操作:
- Editing anothers profile (or anything else)
- your rights have to be higher than the ones of the other account
- everything should be logged by the user that changed the entries, not by the owner
- 编辑他人的个人资料(或其他任何内容)
- 您的权利必须高于其他帐户的权利
- 一切都应该由更改条目的用户记录,而不是由所有者记录
The following solutions are build in ones, maybe there are some packages for laravel to solve this kind of problem.
以下解决方案是内置的,也许laravel有一些包可以解决此类问题。
Auth::loginById($otherUserId)could be one solution:
Auth::loginById($otherUserId)可能是一种解决方案:
- you have to check if the user is allowed to log in in this profile
- you have to remember your own user id (in a session) to add it for the log
- you can access only the pages the user can see (not the admin pages)
- 您必须检查是否允许用户登录此配置文件
- 您必须记住自己的用户 ID(在会话中)才能将其添加到日志中
- 您只能访问用户可以看到的页面(而不是管理页面)
Another approach would be to use Policies
另一种方法是使用策略
e.g. you are user 1 and want to edit the profile of user 3. in the update function user/3/profile
. You call a policy function where you check if your user_role_id is smaller than the other ones. Then the record will be saved and the logger will log it away with your user id.
例如,您是用户 1,想要在更新功能中编辑用户 3 的个人资料user/3/profile
。您调用一个策略函数,检查您的 user_role_id 是否小于其他的。然后记录将被保存,记录器将使用您的用户 ID 将其记录下来。
Both ways have pros and cons. Login with the id will give you exact the view of the other user. But you have to modify your logger (instead of Auth::id() use something with a session). Then you can implement a little button with (jump back to own profile) to login back in your own account. Using polices will be easier for the logger, but at every part you have to implement the check with the policy.
两种方式各有利弊。用 id 登录会给你准确的其他用户的视图。但是你必须修改你的记录器(而不是 Auth::id() 在会话中使用一些东西)。然后,您可以使用(跳回自己的个人资料)来实现一个小按钮,以重新登录您自己的帐户。对于记录器来说,使用策略会更容易,但在每个部分,您都必须使用策略进行检查。
Not knowing the size and complexity of your project I would suggest the first solution. I implemented it by myself in one project but without the logger function.
不知道您的项目的规模和复杂性,我会建议第一个解决方案。我自己在一个项目中实现了它,但没有记录器功能。
回答by Rubén Ruíz
First you need add 2 columns to user table: type(integer 1=admin, 2=some other) and active (boolean 1 to true and 0 false)
首先,您需要向用户表中添加 2 列:type(integer 1=admin, 2=some other) 和 active (boolean 1 to true and 0 false)
php artisan make:migration add_cols_to_users_table --table=users
php artisan make:migration add_cols_to_users_table --table=users
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('type')->default(0);
$table->boolean('active')->default(0);
});
}
public function down()
{
Schema::table('users', function ($table) {
$table->dropColumn(['type', 'active']);
});
}
}
link on some page
某个页面上的链接
<a href="{{ url('impersonate') }}/{{ $user->id }}" class="btn btn-success">Enter as {{$user->name}}</a>
someUserController.php:
someUserController.php:
use Illuminate\Support\Facades\Auth;
class someUserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$id = Auth::id();
$user = User::find($id);
//echo '<pre>ID:'.$id.' - '.print_r($user,1); die();
if($user->type !== 1) //1 for type admin
{
echo ' error not admin (nice try!).';
die();
}
}
public function impersonate($id)
{
Auth::logout(); // for end current session
Auth::loginUsingId($id);
return redirect()->to('get-dashboard');
}
}
}
routes.php | web.php
路线.php | 网页.php
Route::get('/impersonate/{id}', 'someUserController@impersonate');
Route::get('get-dashboard', function () {
$id = \Illuminate\Support\Facades\Auth::id();
$user = \App\User::find($id);
//echo '<pre>'.print_r($user,1); die();
if(!$user->active) return redirect('404-page');
switch($user->type)
{
case 1: return redirect('x-url-dashboard-1'); break;
case 2: return redirect('x-url-dashboard-2'); break;
case 3: return redirect('x-url-dashboard-3'); break;
}
});