shah-newaz/permissible-ng
最新稳定版本:v3.0.0
Composer 安装命令:
composer require shah-newaz/permissible-ng
包简介
Role Permission system for Laravel 11+
README 文档
README
A flexible and powerful Laravel package for managing roles and permissions with hierarchical support.
Features
- Role-based access control (RBAC)
- Hierarchical roles with weight-based inheritance
- Permission management
- Route middleware for roles and permissions
- Caching support for optimal performance
- Soft delete support
Installation
composer require shah-newaz/permissible-ng
php artisan vendor:publish --provider="Shahnewaz\PermissibleNg\Providers\PermissibleServiceProvider"
Run the migrations:
php artisan migrate
Usage
Setup User Model
Add the Permissible trait to your User model:
use Shahnewaz\PermissibleNg\Traits\Permissible; class User extends Authenticatable { use Permissible; // ... rest of your User model }
Note: This can be done using the permissible:setup command automatically.
Managing Roles and Permissions
// Create a role $role = Role::create([ 'name' => 'Admin', 'code' => 'admin', 'weight' => 1 ]); // Create a permission $permission = Permission::createPermission('users.create'); // Assign permission to role $role->permissions()->attach($permission); // Assign role to user $user->roles()->attach($role);
Checking Permissions
// Check single permission $user->hasPermission('users.create'); // Check multiple permissions (requires all) $user->hasPermissions(['users.create', 'users.delete']); // Check permission type $user->hasPermissionType('users');
Route Protection
Use the elegant route middleware syntax:
// Protect routes with roles Route::get('/admin/dashboard', [DashboardController::class, 'index']) ->roles(['admin', 'super-admin']); // Protect routes with permissions Route::get('/users', [UserController::class, 'index']) ->permissions(['users.view']); // Combine both Route::get('/users/create', [UserController::class, 'create']) ->roles(['admin']) ->permissions(['users.create']);
Route Group Protection
Route::group(['middleware' => ['roles:su,admin']], function () { Route::get('/admin/dashboard', [DashboardController::class, 'index']); });
Permission Wildcards
Use wildcards for broader permission control:
// Grant all user permissions Permission::createPermission('users.*'); // Check if user has any user permission $user->hasPermissionType('users');
Configuration
The package configuration can be modified in config/permissible.php:
return [ 'enable_routes' => true, 'enable_user_management_routes' => true, 'first_last_name_migration' => true, 'default_fallback_route' => 'backend.dashboard', ];
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 140
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-11