简体   繁体   English

在 Laravel 5.8 中以用户身份管理员登录

[英]Admin login as user in Laravel 5.8

I am working on a Laravel 5.8 project.我正在从事 Laravel 5.8 项目。 I want to make an authentication system where the admin can log in as a user.我想制作一个身份验证系统,管理员可以在其中以用户身份登录。 I have separate tables for authentication;我有单独的身份验证表; one is for admin, and the second is for normal users.一个是给管理员的,第二个是给普通用户的。

User login works on the default Laravel authentication, and for admin, there is a custom code for login.用户登录使用默认的 Laravel 身份验证,对于管理员,有一个自定义登录代码。

Auth::guard('admin')->attempt([
    'name' => $request->name,
    'password' => $request->password
], $request->remember);

For example:例如:

I have 3 users: user1我有 3 个用户:user1
user2 user3用户 2 用户 3

And one admin user with a username of "admin."以及一个用户名为“admin”的管理员用户。

The admin can see a list of users on site.管理员可以看到站点上的用户列表。 And on that page, there is a button which says "login as...".在那个页面上,有一个按钮,上面写着“以...登录”。 So when the admin clicks on that button, the admin can log in as that user.因此,当管理员单击该按钮时,管理员可以以该用户身份登录。 This means the admin can do what that user can do.这意味着管理员可以做该用户可以做的事情。 But all work will be done as admin.但所有工作都将以管理员身份完成。

In short: If the admin wants to log in as any of the users on front and perform his stuff, the admin will log in as that user.简而言之:如果管理员想以前面的任何用户身份登录并执行他的工作,管理员将以该用户身份登录。 Meanwhile, the admin wants to log in to the front end as User1, but the admin side admin will log in as admin.同时,admin想以User1的身份登录前端,但是admin端的admin会以admin的身份登录。

Try this instead.试试这个。 You can add 2 table columns, maybe one called "role" with enum values, eg, ADMIN, CUSTOMER, and another called "is_normal_user" with boolean type values, which you can set to true or false.您可以添加 2 个表列,一个称为“role”,带有枚举值,例如 ADMIN、CUSTOMER,另一个称为“is_normal_user”,带有布尔类型值,您可以将其设置为 true 或 false。 So when you try to login as admin and perform admin functions, use the role by checking if their role is ADMIN or CUSTOMER, then when you try to log in and do customer functions as the admin, use the "is_normal_user" field.因此,当您尝试以管理员身份登录并执行管理员功能时,请通过检查其角色是 ADMIN 还是 CUSTOMER 来使用该角色,然后当您尝试以管理员身份登录并执行客户功能时,请使用“is_normal_user”字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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