Laravel 5 基于角色的访问控制

13

我正在尝试为我的应用程序设计一个高效且灵活的RBAC解决方案。我进行了一些研究,并认为我已经创建了以下内容。

在我的用户模型中,我有:

...
public function role() {
        return $this->belongsToMany('App\Models\Role', 'user_roles');
    }

    public function hasRole($role) {
        if($this->role->where('name', $role)->first())
            return true;
    }
...

以下是用法示例:

Route::group(['middleware' => 'auth'], function () {

    Route::get('/dashboard', function () {
        if (Auth::user()->hasRole('Sales')) {
            return view('dashboards/sales');
        } else {
            return 'Don\'t know where to send you :(';
        }
    });

});

权限分配给角色,但在上面的示例中没有检查权限。然后将角色分配给用户,一个用户可以拥有多个角色。

我这样做的方式是否可扩展且是有效的RBAC解决方案?


3
5.1.11引入了使用能力和策略进行授权的功能。它们是否符合您的要求?请参阅http://laravel.com/docs/5.1/authorization。不过,您也可以尝试使用ENTRUST https://github.com/Zizaco/entrust。 - e4rthdog
你可以通过用户模型来检查权限能力,具体请参考:http://laravel.com/docs/5.1/authorization#defining-abilities - e4rthdog
@e4rthdog 那么,例如如何确定已登录的用户是否可以查看某个屏幕呢?并且它似乎不支持组或角色。 - imperium2335
1
只是为了提供一个L5开箱即用解决方案的替代方案:另一个很好的包是Entrust包 - 它有非常好的文档,并且可以完全满足您想要做的事情。https://github.com/Zizaco/entrust - Gummibeer
谢谢,看起来正是我所需要的,给earthdog和gummi加1。 - imperium2335
显示剩余3条评论
3个回答

3
我制作了一些基于RBAC的应用程序,这取决于你面临的挑战类型,例如:用户拥有一个角色,但你希望特定用户可以访问某些区域,比如“Posts”,现在用户可以像“Moderator”一样编辑帖子。在这种情况下,权限方法比仅使用角色方法更适合。
通过slug定义访问权限,其他字段可用作对“超级管理员”的引用,或者反讽地用于“编辑角色”。从现在开始,编辑角色加上对新“区域”的许可。
public function up()
{
    Schema::create('permissions', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->string('name');
        $table->string('slug')->unique();
        $table->string('description')->nullable();
        $table->string('model')->nullable();
    });
}

作为内容数据的示例,
$createUsersPermission = Permission::create([
    'name' => 'Create permissions',
    'slug' => 'create.permissions',
    ...
]); 

还有一个使用示例:

if ($user->can('create.permissions') { // you can pass an id or slug
    //
}

个人偏好,从未像其他人建议的那样使用Zizaco Entrust,但它的工作方式相同。此外,您还可以采用级别方法。


3

我做了一些不同的事情,我在 UserRole 中创建了 hasRole,而不是在 User 中(这不会影响太多,但根据代码应该是这样)。所以,这是我的路由:

Route::group(['middleware' => 'auth'], function () {
Route::get('/myProfile', function () {
    if (App\UserRole::hasRole('ROLE_CUSTOMER',Auth::user())) {
        return view('views/customer');
    } else {
        return 'Don\'t know where to send you :(';
    }
}); });

下一步是我的UserRole中的方法。我尽量简单明了:
public static function hasRole($authority,$user) {
        $role = Role::where('authority',$authority)->first();
        $userRole = UserRole::where('role_id',$role->id)
                    ->where('user_id',$user->id)->first();
        if($userRole){
            return true;
        }
    }

我们需要查找权限(ROLE_USER,ROLE_CUSTOMER等),$user是从数据库中检索到的用户对象。 其他所有内容都按照您的问题运行。 希望能帮到您! 干杯!

0
由于Laravel没有现成的解决方案来实现基于角色的身份验证,因此您可以创建一个自定义角色表,定义应用程序可能具有的所有可能角色,并创建一个role_user表,其中包含用户和角色的关联。
您可以在User模型下创建方法来检查用户是否属于特定角色。利用该方法注册新的中间件。中间件可以附加到路由或控制器。
详细演示请参见此链接 https://www.5balloons.info/user-role-based-authentication-and-access-control-in-laravel/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接