且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel以其他用户身份登录

更新时间:2023-02-25 19:06:39

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

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.