Laravel以其他用户身份登录 [英] Laravel login as another user

查看:104
本文介绍了Laravel以其他用户身份登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发laravel应用,其中有3个user_roles

I am currently developing a laravel app where there are 3 user_roles

  1. 超级管理员
  2. 管理员
  3. 普通

因此每个角色都可以访问他下面的角色.

So each role can access the roles below him.

例如

超级管理员可以访问管理员和普通用户帐户.

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               *******

推荐答案

阅读评论,我认为您想执行以下操作:

Reading the comments I think you want to do the following:

  • 编辑其他人的个人资料(或其他任何东西)
  • 您的权限必须高于其他帐户的权限
  • 所有更改均应由更改条目的用户而不是所有者记录

以下解决方案是内置的,也许有一些laravel软件包可以解决此类问题.

The following solutions are build in ones, maybe there are some packages for laravel to solve this kind of problem.

Auth :: loginById($ otherUserId)可能是一种解决方案:

Auth::loginById($otherUserId) could be one solution:

  • 您必须检查是否允许用户登录此个人资料
  • 您必须记住自己的用户ID(在会话中)才能将其添加到日志中
  • 您只能访问用户可以看到的页面(而不是管理页面)

另一种方法是使用政策

例如您是用户1,并且想要在更新功能user/3/profile中编辑用户3的配置文件.您调用一个策略函数,在其中检查您的user_role_id是否小于其他用户.然后,记录将被保存,记录器将使用您的用户ID将其注销.

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.

两种方法都有其优点和缺点. 使用ID登录将为您提供其他用户的确切视图.但是您必须修改记录器(而不是Auth :: 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.

不知道您的项目的规模和复杂性,我建议第一个解决方案.我自己在一个项目中实现了它,但是没有记录器功能.

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.

这篇关于Laravel以其他用户身份登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆