PHP code example of shiwuhao / laravel-rbac
1. Go to this page and download the library: Download shiwuhao/laravel-rbac library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shiwuhao / laravel-rbac example snippets
// 路由直接绑定 Action
Route::post('/roles', CreateRole::class);
Route::put('/roles/{id}', UpdateRole::class);
'models' => [
'user' => \App\Models\User::class,
],
use Rbac\Traits\HasRolesAndPermissions;
class User extends Authenticatable
{
use HasRolesAndPermissions;
}
use Rbac\Actions\Role\CreateRole;
// 创建角色
$role = CreateRole::handle([
'name' => '管理员',
'slug' => 'admin',
]);
use Rbac\Actions\Role\CreateRole;
$role = CreateRole::handle([
'name' => '管理员',
'slug' => 'admin',
]);
use Rbac\Actions\Permission\CreatePermission;
$permission = CreatePermission::handle([
'name' => '创建用户',
'slug' => 'user:create',
]);
// 代码中
if (auth()->user()->hasPermission('user:create')) {
// 有权限
}
// Blade 模板中
@permission('user:create')
<button>创建用户</button>
@endpermission
// 路由中间件
Route::get('/users', [UserController::class, 'index'])
->middleware('permission:user:view');
use Rbac\Actions\DataScope\CreateDataScope;
// 创建简单数据范围
$scope = CreateDataScope::handle([
'name' => '部门数据',
'type' => 'department',
]);
// 创建带动态表达式的自定义数据范围
$scope = CreateDataScope::handle([
'name' => '最近30天数据',
'slug' => 'recent-30-days',
'type' => 'custom',
'config' => [
'rules' => [
['field' => 'created_at', 'operator' => '>=', 'value' => '@now-30d:date'],
['field' => 'status', 'operator' => 'IN', 'value' => ['active', 'pending']],
],
'logic' => 'AND',
],
'description' => '只能访问最近30天的活跃数据(运行时动态计算)',
]);
'@now' // 当前时间
'@now-30d' // 30天前
'@now-3M' // 3个月前
'@now-1y' // 1年前
'@now+7d' // 7天后
'@now-30d:date' // 30天前(仅日期)
'@now-30d:timestamp' // 30天前(时间戳)
'@user.id' // 当前用户ID
'@user.department_id' // 用户部门ID
'@user.organization_id' // 用户组织ID
// 限制只能访问自己部门最近一周的数据
CreateDataScope::handle([
'name' => '本部门本周数据',
'type' => 'custom',
'config' => [
'rules' => [
['field' => 'department_id', 'operator' => '=', 'value' => '@user.department_id'],
['field' => 'created_at', 'operator' => '>=', 'value' => '@now-7d:datetime'],
],
'logic' => 'AND',
],
]);
use Rbac\Actions\Permission\CreateInstancePermission;
// 为特定文章创建权限
$permission = CreateInstancePermission::handle([
'resource' => 'article',
'resource_id' => 123,
'action' => 'update',
]);
use Rbac\Actions\BaseAction;
class CustomAction extends BaseAction
{
protected function rules(): array
{
return ['name' => '
return [
// 自定义用户模型
'models' => [
'user' => \App\Models\User::class,
],
// API 路由配置
'api' => [
'enabled' => true,
'prefix' => 'api/rbac',
'middleware' => ['api', 'auth:sanctum'],
],
];
bash
# 发布所有文件
php artisan vendor:publish --provider="Rbac\RbacServiceProvider"
# 或者分别发布
php artisan vendor:publish --tag=rbac-config
php artisan vendor:publish --tag=rbac-migrations
php artisan vendor:publish --tag=rbac-routes
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Rbac\RbacServiceProvider"
php artisan migrate
bash
# 扫描并生成权限节点
php artisan rbac:scan-permissions
# 清除权限缓存
php artisan rbac:clear-cache
# 查看权限统计
php artisan rbac:permission-stats