不同的用户类型 laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30411585/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:36:44  来源:igfitidea点击:

Different user types laravel

phpauthenticationlaravellaravel-5

提问by ruuux93

I'm fairly new to laravel-5 and I love it so far, but I've come out with this doubt while I was working on my project.

我对 laravel-5 还很陌生,到目前为止我很喜欢它,但是在我从事我的项目时,我提出了这个疑问。

In my app I want different user types, let's say Employee, Admin and Freelance. Every type will have ofcourse, different permissions and acces to pages. Since every user type has different data, I created 3 additional tables to my schema, admins, employees and freelancers, and added a field user_type on the users default table, to link it to the new tables, which have a user_type aswell, keeping my default users table small as possible, just login info.

在我的应用程序中,我想要不同的用户类型,比如说员工、管理员和自由职业者。当然,每种类型都有不同的权限和对页面的访问。由于每个用户类型都有不同的数据,我为我的架构、管理员、员工和自由职业者创建了 3 个额外的表,并在用户默认表上添加了一个字段 user_type,将其链接到新表,这些表也有一个 user_type,保持我的默认用户表尽可能小,只是登录信息。

So I started reading around and I came up with polymorphic relationship on eloquent, which apparently is exactly what I wanted. After implementing those models relationships and migrations, I don't know how to keep moving, and how to check if a user is either freelancer, admin or employee and how to give acces to certain page regarding the usertype.

所以我开始四处阅读,我想出了关于 eloquent 的多态关系,这显然正是我想要的。实现这些模型关系和迁移后,我不知道如何继续前进,如何检查用户是自由职业者、管理员还是员工,以及如何访问有关用户类型的某些页面。

This is how my User Model looks like:

这是我的用户模型的样子:

public function userable()
{
    return $this->morphTo();
}

And this is how one the childs look like:

这就是孩子们的样子:

public function user()
{
    return $this->morphOne('User', 'userable');
}

回答by Bogdan

Since your Usermodel is userable, that means the userstable should have the two columns necessary for polymorphic relations: userable_idand userable_type. The userable_typewill contain the class name of the owning model (which in your case will be either Admin, Freelanceror Employee). So this will return the user type class:

由于您的User模型是userable,这意味着该users表应该具有多态关系所需的两列:userable_iduserable_type。在userable_type将包含所属模型的类名(在你的情况将是要么AdminFreelancerEmployee)。所以这将返回用户类型类:

User::find($id)->userable_type; // Use this to identify the user type

While normalizing the database structure by separating information into different tables and using a polymorphic relation to handle them is very good, I strongly suggest you handle roles in a separate structure. The Entrustpackage is a very good way of doing that, because it allows for a very robust way of handling user roles and permissions.

虽然通过将信息分离到不同的表中并使用多态关系来处理它们来规范化数据库结构是非常好的,但我强烈建议您在单独的结构中处理角色。该委托包是这样做的一个很好的方法,因为它允许处理用户角色和权限一个非常可靠的方法。

回答by tommy

You can use the zizaco/entrustpackage for this. It introduces roles and permissions per role for your users.

您可以为此使用zizaco/entrust包。它为您的用户引入了角色和每个角色的权限。

In this case, roles are not defined by new models or polymorphic relationships, but simply stored in a rolestable. Users and roles are then linked by a pivot table role_user(by default).

在这种情况下,角色不是由新模型或多态关系定义的,而是简单地存储在角色表中。用户和角色然后通过数据透视表role_user(默认情况下)链接。

Giving access to certain pages becomes really easy:

访问某些页面变得非常容易:

// Checking for a role
$user->hasRole('admin');

// Checking for a single permission
$user->can('edit-users');

The big advantage of this approach is that you don't have to add new tables, models, migrations et cetera to add new roles and permissions.

这种方法的一大优点是您不必添加新表、模型、迁移等来添加新角色和权限。

In case of "every user type has different data", it really depends on what you mean. If you mean the permissions per role, then this package solves this for you. But if you mean data such as a second address or data that only belongs to certain types of users, I'd consider if it really makes sense to outsource this data to different tables (e.g. if you have a lot of unique data per user type), or if I'd just ignore those extra columns in my users table in cases where they are not needed (maybe make them nullable).

在“每个用户类型都有不同的数据”的情况下,这实际上取决于您的意思。如果您指的是每个角色的权限,那么这个包可以为您解决这个问题。但是,如果您的意思是诸如第二个地址或仅属于某些类型用户的数据之类的数据,我会考虑将这些数据外包给不同的表是否真的有意义(例如,如果每个用户类型都有很多独特的数据),或者如果我只是在不需要的情况下忽略我的用户表中的那些额外列(也许使它们可以为)。

Edit:

编辑:

No information given on which database is used, but these are cases where i personally like to switch from something like MySQL to a schemaless database like MongoDB. Maybe this is an option for you.

没有提供关于使用哪个数据库的信息,但在这些情况下,我个人喜欢从 MySQL 之类的东西切换到MongoDB 之类的无模式数据库。也许这对你来说是一个选择。