obrainwave / access-tree
此包允许您在 Laravel 项目数据库中管理用户权限和角色
v1.1.0
2024-08-25 19:42 UTC
Requires
- php: ^8.1
- illuminate/contracts: *
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.6
- spatie/laravel-ray: ^1.26
README
此包允许您在 Laravel 项目数据库中管理用户权限和角色。
安装
您可以通过 composer 安装此包
composer require obrainwave/access-tree
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="accesstree-migrations"
php artisan migrate
创建根用户
运行迁移后,将在 users
表中添加一个新列 is_root_user
。要创建一个可以覆盖应用程序中所有权限和角色的用户,请将该列设置为特定用户的 true
。
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="accesstree-config"
使用方法
创建权限
$data = [ 'name' => 'Add User', 'status' => 1 or 0 ]; $create = createAccess($data, 'permission'); echo $create;
更新权限
$data = [ 'data_id' => id from created permissions table when installed package // 3, 'name' => 'Add User', 'status' => 1 or 0 ]; $update = updateAccess($data, 'permission'); echo $update;
创建角色
$data = [ 'name' => 'Admin', 'status' => 1 or 0 ]; $permission_ids = array of ids from created permissions table when installed package // array(1, 5, 4); $create = createAccess($data, 'role', $permission_ids); echo $create;
更新角色
$data = [ 'data_id' => role id // 5, 'name' => 'Admin Staff', 'status' => 1 or 0 ]; $permission_ids = array of ids from created permissions table when installed package // array(10, 6, 3); $update = updateAccess($data, 'role', $permission_ids); echo $update;
创建用户角色
$roles = array of ids from created roles table when installed package // array(2, 5); $user_id = id of a user from App\Models\User // 1; $user_role = createUserRole($roles, $user_id); echo $user_role;
更新用户角色
$roles = array of ids from created roles table when installed package // array(2, 5); $user_id = id of a user from App\Models\User // 5; $user_role = updateUserRole($roles, $user_id); echo $user_role;
检查用户访问或权限
checkPermission(string slug_of_permission) // returns true or false or checkPermissions(array slug_of_permissions) // returns true or false
您可以在应用程序的任何地方调用上述函数。使用示例
@if(checkPermission('add_user') // Do some stuff @else throw new \Exception("Access Forbidden", 1); @endif @if(checkPermissions(['add_user', 'view_user']) // Do some stuff @else throw new \Exception("Access Forbidden", 1); @endif
检查用户是否为根用户
isRootUser(int user_id) // returns true or false
获取权限
fetchPermissions(int $status) // active = 1 or inactive = 0 // $status is optional if is empty all permissions will be fetched
获取角色
fetchRoles(int $status) // active = 1 or inactive = 0 // $status is optional if is empty all roles will be fetched
获取用户角色
fetchUserRoles(int $user_id) // $user_id is id of the user from App\Models\User
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。