简体   繁体   English

Laravel 4.2用户角色安全性

[英]Laravel 4.2 User Role Security

I've been tinkering with my own user authorisation and providing user roles based on a database entry for my users . 我一直在摆弄自己的用户授权和基于我的数据库条目提供用户角色users

I made two roles; 我扮演两个角色; a user role which is just a general, signed up user, and an author role, which is obviously for an author. user角色(仅是一般的注册用户)和author角色(显然适用于作者)。

In my users table migration, I'm using $table->string('role')->default('user'); 在我的用户表迁移中,我正在使用$table->string('role')->default('user'); as the default user role. 作为默认用户角色。 I can then decide from a form somewhere if the user should be an author. 然后,我可以从某处的表单中确定用户是否应为作者。

In my User.php , I set up a public function that calls the role database entry in my users table, like so: 在我的User.php ,我设置了一个public function ,该public function调用用户表中的角色数据库条目,如下所示:

public function isAuthor()
{
    if (Auth::guest()) return false;
    return Auth::user()->role = $this->role == 'author';
}

Which I can now call in my templates, like so: 我现在可以在模板中调用它,如下所示:

@if($user->isAuthor())
    <p>Only the author can this!</p>
@endif

This gives me the control I expect, but I'm not entirely sold on the fact 'it's that easy'. 这给了我期望的控制权,但是我并没有完全被“那么简单”这一事实所吸引。 I understand that yes, I can create a relationship with the User and bind the relationship between two models, but I found this pretty neat. 我知道是的,我可以与User建立关系并绑定两个模型之间的关系,但是我发现这很简洁。 My only worry on this is security. 我对此唯一担心的是安全性。

This may not be best practise by a long shot, but I'm fairly new to Laravel and was wondering what security implications doing it the way I have done vs. other methods available. 从长远来看,这可能不是最佳实践,但是我对Laravel还是陌生的,我想知道这样做与其他可用方法相比有什么安全隐患。

I totally understand that there are other methods on SO/Laravel Docs and User roles have been the topic of many discussions, though, I've never seen it the way I've implemented it (which worries me). 我完全理解SO / Laravel Docs上还有其他方法,并且用户角色一直是许多讨论的主题,但是,我从来没有以实现它的方式看过它(这让我感到担忧)。 I just noticed that I could check the current user is signed in using the above method and thought that querying the DB for matched string would work the same -- and it does. 我只是注意到我可以使用上述方法检查当前用户是否已登录,并认为查询数据库中匹配的字符串将起到相同的作用,并且确实如此。

My question is, is this safe enough to use in the real world? 我的问题是,这样在现实世界中使用是否足够安全?

It looks like you're just using a varchar field in your database and hard coding your permissions around it. 看起来您只是在数据库中使用varchar字段,并对其周围的权限进行硬编码。 I propose a better structure. 我提出了一个更好的结构。

table: users
columns: id:int, role_id:int username:varchar64 password:varchar64 etc

table: roles
columns id:int, name:varchar64, description:varchar64 etc etc...

table: permissions
columns: id:int, name:varchar64

table:roles_permissions
columns: role_id:int, permission_id:int

Here we have four tables, one for users, user roles, permissions and a join pivot table for a many to many relationship between roles and permissions. 这里我们有四个表,一个表用于用户,用户角色,权限,还有一个连接数据透视表,用于角色和权限之间的多对多关系。 This is the basis for any RBPS (role based permission system). 这是任何RBPS(基于角色的权限系统)的基础。

Each user can have one role 每个user可以有一个 role

A role can have 0 or many permissions 一个role可以具有0个多个 permissions

By this concept we can reuse roles over and over or even make a user have many roles with an extra join table is applicable. 通过这种概念,我们可以反复使用角色,甚至可以使用户拥有多个角色,并且适用额外的联接表。

All permissions would be stored like so: id:1, name:can_login 所有权限的存储方式如下: id:1, name:can_login

If the can_login relation is present on a user 's role is present, he can login. 如果can_login关系存在于user角色上,则他可以登录。 Some mock code. 一些模拟代码。

if(Auth::can('can_login'))
{
    //Log me in etc etc
}

The advantage of using the database is that we don't repeat ourselves with permission logic which is a design principle in of its self. 使用数据库的优点是我们不会重复使用许可逻辑,而许可逻辑本身就是一种设计原则。

This type of functionality has been provided again and again and I suggest you look at Entrust and Confide for Laravel as they provide all of this out of the box for you. 已经一再提供这种类型的功能,我建议您查看Entrust和Confide for Laravel,因为它们为您提供了所有这些开箱即用的功能。

On the other hand if you're doing this to learn then keep going! 另一方面,如果您这样做是为了学习,那就继续吧!

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

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