Laravel 5.5 中的用户角色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46213616/
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
User Roles in Laravel 5.5
提问by usama muneer
What is the most effective method to add User roles in Laravel 5.5?
在 Laravel 5.5 中添加用户角色的最有效方法是什么?
I came across a couple of methods i.e.
我遇到了几种方法,即
- 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.
我已经在我的应用程序中尝试并实现了这两种方法,但是通过牢记我的应用程序的可扩展性来提出这个问题。
回答by Saurav
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
回答by Mark Walet
Why would you try to reinvent the wheel? Mayb you can use a package for that. The spatie/laravel-permissionpackage is one I particulary like.
你为什么要尝试重新发明轮子?也许你可以为此使用一个包。该spatie / laravel-许可包是一个我格外喜欢。
After a quick search I also found this discussionon laracasts with a few other options.
快速搜索后,我还发现了有关 laracasts 的讨论以及其他一些选项。
回答by Mihai Ocneanu
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:
对于一个工作示例,您可以:
- take a look here at a packagethat is part of
- a bigger app, and try out
- a working demo
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.
在上面的例子中,简而言之,权限绑定到一个角色,可以分配给一个所有者,所有者拥有用户。
回答by Tushar
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. 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.
在我看来,您需要问题中提到的两件事。您应该有一个 Roles 表,该表定义了用户在您的应用程序中可以拥有的所有可能的角色。您将使用一个 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/