简体   繁体   English

Laravel 5.5中的用户角色

[英]User Roles in Laravel 5.5

What is the most effective method to add User roles in Laravel 5.5? 在Laravel 5.5中添加用户角色的最有效方法是什么?

I came across a couple of methods ie 我遇到了几种方法,即

  • Admin Middleware 管理员中间件
  • Pivot Table for user_roles user_roles的数据透视表

I've tried and implemented both methods in my application but asking this question by keeping in mind my application's scalability on how they would be effective. 我已经在我的应用程序中尝试并实现了这两种方法,但是通过记住我的应用程序在如何有效方面的可伸缩性来提出这个问题。

If you want you can add permissions on role table create a new permission table to define permissions to user. 如果需要,可以在角色表上添加权限,请创建一个新的权限表以定义对用户的权限。

Eg: 例如:

Without permission table: Based on cartalyst sentinel
User Table -> name, email, password etc
Role Table -> name, slug, permissions
Users_role -> user_id, role_id

With permission table: Based in Zizaco entrust
User Table -> name, email, password etc
Role Table -> name, slug, permissions
Users_role -> user_id, role_id
Permission table -> name, slug, description
role_permission -> permission_id, role_id

Why would you try to reinvent the wheel? 您为什么要重新发明轮子? Mayb you can use a package for that. 也许您可以使用一个软件包。 The spatie/laravel-permission package is one I particulary like. 我非常喜欢spatie / laravel许可包。

After a quick search I also found this discussion on laracasts with a few other options. 快速搜索之后,我还发现了有关laracast的讨论以及其他一些选择。

I'd say you need to have both: the middleware to check for access on each request & the permissions table (& pivot) for storing your permissions. 我想您需要同时拥有:用于检查对每个请求的访问权限的中间件以及用于存储权限的权限表(&pivot)。

For a working example, you could: 对于一个可行的示例,您可以:

Disclaimer: I am a contributor to these packages. 免责声明:我是这些软件包的贡献者。

In the above examples, in brief, permissions are tied to a role, which can be assigned to an owner, and the owner has users. 简而言之,在以上示例中,权限与角色相关联,可以将角色分配给所有者,并且所有者具有用户。

To me it looks that you need both of things mentioned in your question. 对我来说,您似乎需要问题中提到的两件事。 You should have a Roles table that defines all possible roles a user can have in your application. 您应该有一个Roles表,该表定义用户可以在您的应用程序中拥有的所有可能角色。 A user_roles pivot table that you would use to associate users to particular roles And a Middleware which you will use to check for roles before authorizing user to access the functionality. 用来将用户与特定角色相关联的user_roles数据透视表,以及在授权用户访问功能之前将用于检查角色的中间件。

Detained demo is given in this link for roles based authentication in Laravel 5.5 https://www.5balloons.info/user-role-based-authentication-and-access-control-in-laravel/ 在此链接中提供了有关Laravel 5.5中基于角色的身份验证的示例演示https://www.5balloons.info/user-role-based-authentication-and-access-control-in-laravel/

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

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