GitHub - huang-yi/rbac-laravel: 基于Laravel的RBAC拓展包

[](#%E5%85%B3%E4%BA%8Erbac-laravel)关于Rbac-Laravel

Rbac-Laravel是一个基于Laravel框架的RBAC拓展包,RBAC(Role-Based Access Control)是指基于角色的访问控制。该拓展包为Laravel框架提供了RBAC模型的实现,并且提供了诸多操作RBAC的便捷方法。

[](#%E7%89%88%E6%9C%AC%E4%BF%A1%E6%81%AF)版本信息

Rbac

Laravel

PHP

1.0.x

5.3.*

=5.6.4

[](#%E5%AE%89%E8%A3%85%E6%96%B9%E6%B3%95)安装方法

使用composer来快速安装拓展包:

$ composer require huang-yi/rbac-laravel:1.0.*

或者编辑项目根目录的composer.json文件,在require属性里面添加一项:

{
"require": {
"huang-yi/rbac-laravel": "1.0.*"
}
}

然后执行composer update

[](#%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF)配置信息

首先往Laravel应用中注册ServiceProvider,打开文件config/app.php,在providers中添加一项:

[
'providers' => [
HuangYi\RbacRbacServiceProvider::class,
]
]

然后发布拓展包的配置文件,使用如下命令:

$ php artisan vendor:publish

这时候config/目录下会出现rbac.php文件,该配置文件中有两个配置选项:

第一个为connection属性,用于配置RBAC模块使用的数据库连接名。

第二个为user属性,用于配置Laravel应用中使用的用户模型。默认为App\User

上述配置文件中所配置的user模型类必须use HuangYi\Rbac\RbacTrait

namespace App;
use HuangYi\Rbac\RbacTrait;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use RbacTrait;

}

[](#%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB)数据迁移

执行以下命令创建拓展包必须依赖的数据库表:

$ php artisan migrate

[](#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95)使用方法

[](#permission)Permission

1、权限有三个属性:nameslugdescription

2、创建一个权限:HuangYi\Rbac\Managers\PermissionManager::create(array $attributes)

$permissionManager = new \HuangYi\Rbac\ManagersPermissionManager();
$permission = $permissionManager->create([
'name' => 'Create product',
'slug' => 'product.create',
'description' => 'Create a new product.',
]);

3、删除一个权限:HuangYi\Rbac\Managers\PermissionManager::delete($ID)

$permissionManager = new \HuangYi\Rbac\ManagersPermissionManager();
$deleted = $permissionManager->delete(1);

删除权限时会自动解绑已绑定的角色。

4、更新一个权限:HuangYi\Rbac\Managers\PermissionManager::update($ID, array $attributes)

$permissionManager = new \HuangYi\Rbac\ManagersPermissionManager();
$updated = $permissionManager->update(1, [
'description' => 'Blabla...',
]);

5、查询一个权限:HuangYi\Rbac\Managers\PermissionManager::find($ID)

$permissionManager = new \HuangYi\Rbac\ManagersPermissionManager();
$permission = $permissionManager->find(1);

[](#role)Role

1、角色有三个属性:nameslugdescription

2、创建一个角色:HuangYi\Rbac\Managers\RoleManager::create(array $attributes)

$roleManager = new \HuangYi\Rbac\ManagersRoleManager();
$role = $roleManager->create([
'name' => 'Administrator',
'slug' => 'admin',
'description' => 'Can do anything.',
]);

3、删除一个角色:HuangYi\Rbac\Managers\RoleManager::delete($ID)

$roleManager = new \HuangYi\Rbac\ManagersRoleManager();
$deleted = $roleManager->delete(1);

删除角色时会自动解绑已绑定的用户和权限。

4、更新一个角色:HuangYi\Rbac\Managers\RoleManager::update($ID, array $attributes)

$roleManager = new \HuangYi\Rbac\ManagersRoleManager();
$updated = $roleManager->update(1, [
'description' => 'Blabla...',
]);

5、查询一个角色:HuangYi\Rbac\Managers\RoleManager::find($ID)

$roleManager = new \HuangYi\Rbac\ManagersRoleManager();
$role = $roleManager->find(1);

6、为角色绑定权限:HuangYi\Rbac\Managers\RoleManager::attachPermissions($permissionIDs)

$roleManager = new \HuangYi\Rbac\ManagersRoleManager();
// 绑定一个权限
$roleManager->attachPermissions(1);
// 同时绑定多个权限
$roleManager->attachPermissions([1, 2, 3]);

7、为角色解绑权限:HuangYi\Rbac\Managers\RoleManager::detachPermissions($permissionIDs)

$roleManager = new \HuangYi\Rbac\ManagersRoleManager();
// 解绑一个权限
$roleManager->detachPermissions(1);
// 同时绑定多个权限
$roleManager->detachPermissions([1, 2, 3]);

[](#user)User

1、为用户绑定角色:HuangYi\Rbac\RbacTrait::attachRoles($roleIDs)

$user = \AppUser::find(1);
// 绑定一个角色
$user->attachRoles(1);
// 同时绑定多个角色
$user->attachRoles([1, 2, 3]);

2、为用户解绑角色:HuangYi\Rbac\RbacTrait::detachRoles($roleIDs)

$user = \AppUser::find(1);
// 解绑一个角色
$user->detachRoles(1);
// 同时解绑多个角色
$user->detachRoles([1, 2, 3]);

3、判断用户是否为某些角色,若需要判断多个角色请使用|间隔:HuangYi\Rbac\RbacTrait::hasRole($roles)

$user = \AppUser::find(1);
// 判断一个角色
$user->hasRole('admin');
// 判断多个角色
$user->hasRole('seller|operator');

4、判断用户是否拥有某些权限,若需要判断多个权限请使用|间隔:HuangYi\Rbac\RbacTrait::hasPermission($permissions)

$user = \AppUser::find(1);
// 判断一个权限
$user->hasPermission('product.create');
// 判断多个权限
$user->hasPermission('product.create|product.update');

[](#middleware)Middleware

Rbac-Laravel为开发人员提供了便利的Middleware,如果需要使用请将HuangYi\Rbac\RbacMiddleware注入到app/Http/Kernel.php中的路由中间件里:

protected $routeMiddleware = [
'rbac' => \HuangYi\RbacRbacMiddleware::class,
];

配置好后即可在路由中使用:

Route::get('/do/something', [
'uses' => 'SomeController@action',
'middleware' => 'rbac:role,roleSlug1|roleSlug2',
]);
Route::get('/do/something', [
'uses' => 'SomeController@action',
'middleware' => 'rbac:permission,permissionSlug1|permissionSlug2',
]);

[](#%E5%9C%A8%E8%A7%86%E5%9B%BE%E4%B8%AD%E4%BD%BF%E7%94%A8)在视图中使用

@ifHasRole('roleSlug1|roleSlug2')
<p>You can see this.</p>
@endIfHasRole
@ifHasPermission('permission1|permission2')
<p>You can see this too.</p>
@endIfHasPermission

[](#%E6%94%AF%E6%8C%81)支持

Bugs和问题可提交至Github,或者请联系作者黄毅(coodeer@163.com

[](#license)License

The Rbac-Laravel is open-sourced software licensed under the MIT license


Original url: Access
Created at: 2018-11-06 13:20:42
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论