dmox/h-rbac
Composer 安装命令:
composer require dmox/h-rbac
包简介
Based on native Laravel's abilities. Hierarchical RBAC with callbacks.
README 文档
README
Based on native Laravel's 5 abilities. Hierarchical RBAC with callbacks.
In the process of creating my own projects I have formed an opinion about the minimum required ability of RBAC. It should allow:
- roles and permissions
- callbacks for permissions (for passing parameters in permission checking)
- permission's inheritance
- the way RBAC is served
Install
Keep in mind it's only for Laravel 5.1 and later.
Via Composer
$ composer require dlnsk/h-rbac
Add the service provider to config/app.php
Dlnsk\HierarchicalRBAC\HRBACServiceProvider::class,
Publish some cool stuff:
- config file (config/h-rbac.php)
- migration (add field
roletouserstable) - role/permission/callbacks configuration class (app/Classes/Authorization/AuthorizationClass.php)
with
php artisan vendor:publish --provider="Dlnsk\HierarchicalRBAC\HRBACServiceProvider"
Add roles, permissions which you need and callbacks where it needs and have fun!
Overview
This module is wrapper for authorization logic and control access to resources of Laravel 5.1 and later. Except you shouldn't define abilities, they will define automatically.
Let's describe the minimum required ability of RBAC (in my opinion).
Roles and permissions
It's clear.
Callbacks for permissions
Very common situation is to allow user to change only his own posts. With this package it's simple:
public function editOwnPost($user, $post) { return $user->id === $post->user_id; }
and use as
if (\Gate::can('editOwnPost', $post)) { }
You can pass any number of parameters in callback as array.
Permission's inheritance
As you see callbacks is very useful. But what about site manager who may edit any posts? Create separate permission? But which of it we should check?
Answer is use chained (inherited) permissions. Example:
editPost -> editPostInCategory -> editOwnPost
Each of this permission put in appropriate role but we always check the first (except in very rare cases):
if (\Gate::can('editPost', $post)) { }
These permissions will be checked one by one until one of it will pass. In other case ability will be rejected for this user. So, we have many permissions with different buisnes logic but checking in code only one.
The way RBAC is served
Very popular is to use database for store roles and permissions. It flexible but hard to support. Managing of roles and permissions required backend (but stil available to change directly in DB). When we start to use inheritance for permissions it becomes too difficult for direct changing.
In other case most projects aren't large. It need only few roles and permissions, so backend becomes economically inexpedient. Thus, I believe that file driven RBAC is enough for many projects. It's visual and simple for support.
Storage of roles and permissions is on another level of logic, so DB support may be added later.
Usage
As I said h-rbac is wrapper for authorization logic of Laravel 5.1 and later. So, you can use any features of it.
if (\Gate::allows('editPost', $post)) { // do something } ... if (\Gate::denies('editPost', $post)) { abort(403); } ... if (\Gate::forUser($user)->allows('editPost', $post)) { // do something }
From User model:
if ($request->user()->can('editPost', $post)) { // do something } ... if ($request->user()->cannot('editPost', $post)) { abort(403); }
In controller:
$this->authorize('editPost', $post);
Within Blade
@can('editPost', $post)
<!-- The Current User Can Update The Post -->
@else
<!-- The Current User Can't Update The Post -->
@endcan
Also in h-rbac we add directive @role which you can combine with @else
@role('user|manager')
<!-- The current user has any role -->
@endrole
Configuration
When you publish configuration with artisan you'll have configuration class app/Classes/Authorization/AuthorizationClass.php where you should define permissions, roles and callbacks. You are free to move this file anywhere you want. Don't forget update config/h-rbac.php in this case.
Structure of configuration class:
<?php namespace App\Classes\Authorization; use Dlnsk\HierarchicalRBAC\Authorization; class AuthorizationClass extends Authorization { public function getPermissions() { return [ 'editPost' => [ 'description' => 'Edit any posts', // optional property 'next' => 'editOwnPost', // used for making chain (hierarchy) of permissions ], 'editOwnPost' => [ 'description' => 'Edit own post', ], 'deletePost' => [ 'description' => 'Delete any posts', ], ]; } public function getRoles() { return [ 'manager' => [ 'editPost', 'deletePost', ], 'user' => [ 'editOwnPost', ], ]; } ////////////// Callbacks /////////////// public function editOwnPost($user, $post) { $post = $this->getModel(\App\Post::class, $post); // helper method for geting model return $user->id === $post->user_id; } }
You should add callback only if you need additional check for this permission. The name of callback should be camelcased name of permission.
We use next logic for checking permission: starting from the current permission, we check all of the following in chain one by one and:
- allow if role has a permission with no callback
- allow if role has a permission and callback return true
- deny if role hasn't any permission in chain
- deny if role has a permission but callback return false (in this case we don't check any remaining permission in chain)
Change log
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
dmox/h-rbac 适用场景与选型建议
dmox/h-rbac 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「permission」 「hierarchical」 「rbac」 「role」 「ability」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dmox/h-rbac 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dmox/h-rbac 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dmox/h-rbac 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library to extend Object capabilities.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
This package provides a flexible way to add Role-based Permissions to Laravel
An enhanced tree layout plugin for Filament Admin with policy authorization and dedicated tree resources
Alfabank REST API integration
Symfony bundle to handle authorization, i.e. check permission to perform action on a resource.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-01