简体   繁体   English

Laravel 4多用户身份验证

[英]Laravel 4 Multi User Authentication

I'm building a Laravel 4 app that requires login authentication for 3 entity types: Coach, Student & Admin, all with separate user interfaces. 我正在构建一个Laravel 4应用程序,该应用程序需要3种实体类型的登录验证:Coach,Student和Admin,所有这些都具有单独的用户界面。 While I could use a package like Sentry 2 and a single DB user table with user types to achieve this, something about the potential polymorphic DB design patterns and headaches that can occur down the track, don't sit well with me. 虽然我可以使用像Sentry 2这样的软件包和一个带有用户类型的单个数据库用户表来实现这一点,但是关于可能出现在轨道上的潜在多态数据库设计模式和令人头疼的事情,并不适合我。 Having dealt with polymorphic issues in the past with previous apps, and the grief it can create when you want to normalise your DB structure, etc. having separate DB tables for each entity type seems a better way to go. 处理过去与以前的应用程序的多态问题,以及当你想要规范化你的数据库结构时可以创建的悲痛等等,为每个实体类型分别设置数据库表似乎是一种更好的方法。

How would you solve this design problem? 你会如何解决这个设计问题?

Laravel 4 auth uses basically the following files: Laravel 4 auth基本上使用以下文件:

  • Auth.php (facade) Auth.php(门面)
  • AuthManager.php AuthManager.php
  • AuthServiceProvider.php AuthServiceProvider.php
  • Guard.php Guard.php
  • auth.php (config) auth.php(config)
  • User.php (eloquent model) User.php(雄辩的模型)

I've played around with duplicating these files to come up with mostly an independent auth for the coach entity that works, registering the facade and service provider in the app.php file, as well as making the necessary changes to config to use the Coach eloquent model for authentication: 我已经玩过复制这些文件,主要是为工作的教练实体提供一个独立的auth,在app.php文件中注册facade和服务提供者,以及对config进行必要的更改以使用Coach用于身份验证的雄辩模型:

  • AuthCoach.php (facade) AuthCoach.php(门面)
  • AuthCoachManager.php AuthCoachManager.php
  • AuthCoachServiceProvider.php AuthCoachServiceProvider.php
  • Guard.php Guard.php
  • authcoach.php (config) authcoach.php(config)
  • Coach.php (eloquent model) Coach.php(雄辩模特)

I am still using Guard.php from the standard Laravel 4 auth, but Guard can easily be extended if the need arises to customise Guard methods for coach authentication by creating a GuardCoach.php file. 我仍在使用标准Laravel 4 auth中的Guard.php,但是如果需要通过创建GuardCoach.php文件来定制Guard方法的Guard方法,可以轻松扩展Guard。

If I'm going to have separate auth for each entity type, do you think this is a good way to achieve it? 如果我要为每种实体类型分别使用auth,你认为这是实现它的好方法吗?

Can you see any potential problems or know a better way of doing this? 你能看到任何潜在的问题或知道更好的方法吗?

Maybe I do not understand your context well, but why dont you just use the basic concept of role based access control and render different stuff for different roles ? 也许我不太了解您的上下文,但为什么不使用基于角色的访问控制的基本概念并为不同的角色呈现不同的东西? If that is not tight enough you can use attribute based auth policies to granulate permissions. 如果这不够紧,您可以使用基于属性的身份验证策略来细化权限。 What are the reasons you want to duplicate (triple that is) auth-logic? 您想要复制(三倍)auth-logic的原因是什么? Not mentioning the fact of redundant db data (separate user table for separate user type? yuk!) ? 没有提到冗余数据库数据的事实(单独的用户表用于单独的用户类型?yuk!)?

If you are not satisifed with Sentry (personally, I dont use that library) I can recommend Zizaco/Confide + Zizaco/Entrust as a clean and elegant solution for user/role/permission management. 如果您对Sentry不满意(个人而言,我不使用该库)我可以推荐Zizaco / Confide + Zizaco / Entrust作为用户/角色/权限管理的干净而优雅的解决方案。 Check it out here Zizaco GitHub . 在这里查看Zizaco GitHub

A quick general idea: 一个快速的总体思路:

  • use a single clean authentication mechanism for whole app 对整个应用程序使用单一的干净认证机制
  • granulate access with Roles or Roles+Permissions 使用角色或角色+权限进行粒度访问
  • separate your admin logic into separate controllers (AdminUserController, AdminCoachController, whatever..) 将您的管理逻辑分成单独的控制器(AdminUserController,AdminCoachController,等等..)
  • I see no difficulties in composing appropriate blade templating structure to have it all nicely done and well organised 我认为组成合适的刀片模板结构没有任何困难,使它完美地完成并且组织良好

What are your polymorphic concerns? 你的多态问题是什么?

If you are worrying that your user table will get cluttered, leave it be as a place to store auth details and put all other necessary (non-auth) user details in another table. 如果您担心您的用户表会变得混乱,请将其留作存储身份验证详细信息的位置,并将所有其他必要(非身份验证)用户详细信息放在另一个表中。

Hope this helps you out, if only I understood your problem well. 希望这可以帮助你,只要我理解你的问题。

I think you are trying to swat a mosquito using a mallet. 我想你正试图用木槌敲打蚊子。

Here is how I tackled the same problem: 以下是我解决同样问题的方法:

  • I wanted to make sure that each user's authentication (username, password) is stored in the same table. 我想确保每个用户的身份验证(用户名,密码)都存储在同一个表中。 Basically, I had three types of users. 基本上,我有三种类型的用户。

  • I used Sentry 2 which makes it a breeze to manage the authentication stuff. 我使用了Sentry 2,它可以轻松管理身份验证。

  • Using the default migrations provided by Sentry 2, I added a column 'role' to the 'users' table - which differentiates the 3 types of users. 使用Sentry 2提供的默认迁移,我在“用户”表中添加了一个“角色”列 - 它区分了3种类型的用户。

  • For each user type, I created a table with specific fields. 对于每种用户类型,我创建了一个包含特定字段的表。

  • When the user authenticated, I would grab their 'role' from the 'users' table, run a few if statements and know which view to serve them. 当用户进行身份验证时,我会从'users'表中获取他们的'角色',运行一些if语句并知道为他们提供哪个视图。

And the mosquito was completely dead. 蚊子完全死了。

Onto yours: 在你的上面:

  • Basically, both our approaches are the same - since each user type has a separate table for fields they don't all share (except first name, last name, email, password, last login etc). 基本上,我们的两种方法都是相同的 - 因为每个用户类型都有一个单独的表,它们不是全部共享的字段(除了名字,姓氏,电子邮件,密码,最后登录等)。

  • Your approach will allow a user to belong to the three entities - which is logically not correct. 您的方法将允许用户属于三个实体 - 这在逻辑上是不正确的。 Mine won't - which is logically... 我不会 - 这在逻辑上......

  • You are afraid of 'polymorphic issues' but I don't think we have a lot to deal with here. 你害怕'多态问题',但我认为我们在这里没有太多需要处理的问题。 All we would perhaps do is define in our models that a coach, for example, belongsTo a user. 我们所做的一切就是在我们的模型中定义一个教练,例如, belongsTo一个用户。 And a user hasOne coach. 并且用户有hasOne教练。

  • But in reality, we don't even need to define the relationships. 但实际上,我们甚至不需要定义关系。 Because at authentication, we need to run if statements anyway. 因为在身份验证时,我们无论如何都需要运行if语句。 So, using the user object returned from authentication, we will know two things: which table to then go to for user-type-sepecific information and which view to serve to the authenticated user. 因此,使用从身份验证返回的用户对象,我们将知道两件事:然后转到哪个表以获取用户类型特定信息以及向经过身份验证的用户提供哪个视图。

Don't be afraid, son 儿子,别害怕

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

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