octopyid / watchdog
管理记录或模型访问
v1.0.1
2022-06-03 19:07 UTC
Requires
- php: ^8.0|^8.1
Requires (Dev)
- nunomaduro/collision: ^6.2
- octopyid/phpunit-extra: ^1.0
- orchestra/testbench: ^7.5
README
Laravel Watch Dog
Watch Dog 是一个用于角色管理和控制 Laravel 应用程序能力的包。
功能
- 角色、权限和能力。
- 模型或记录的角色能力。
- 实体对模型或记录的能力。
它还包括中间件和可配置的缓存。
安装
要安装此包,请按照以下步骤操作。
使用 Composer 安装包
- 使用 Composer 安装 WatchDog。
composer require octopyid/watchdog:dev-main
- 发布包。
php artisan vendor:publish --provider="Octopy\WatchDog\WatchDogServiceProvider"
- 将 WatchDog 特性添加到您的模型中。
<?php use Octopy\WatchDog\Concerns\HasAbility; use Octopy\WatchDog\Concerns\HasRole; class User extends Authenticatable { use HasRole, HasAbility; }
- 最后,运行迁移
php artisan migrate
用法
分配角色给用户
$role = Role::create([ 'name' => 'foo', ]); # Assign $user->role->assign('foo'); $user->role->assign($role); # Check $user->role->has('foo'); $user->role->has($role); # Remove $user->role->retract('foo'); $user->role->retract($role);
将能力分配给角色
无模型的权限
$ability = Ability::create([ 'name' => 'delete', ]); # Assign $role->ability->assign('delete'); $role->ability->assign($ability); # Check By User $user->ability->able('delete'); # Check By Role $role->ability->able('delete'); # Remove $user->ability->retract('foo'); $user->ability->retract($ability);
有模型的权限
# You want to give abilities only to certain records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => 1, 'entity_type' => \App\Models\Post::class, ]); # Or do you want to give abilities to all records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => null, // null means all records 'entity_type' => \App\Models\Post::class, ]); # Check By User $user->ability->able('delete', $post); $user->ability->able('delete', Post::class);
将权限分配给实体
无模型的权限
$ability = Ability::create([ 'name' => 'delete', ]); # Assign $user->ability->assign('delete'); $user->ability->assign($ability); # Check $user->ability->able('delete'); # Remove $user->ability->retract('foo'); $user->ability->retract($ability);
有模型的权限
# You want to give abilities only to certain records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => 1, 'entity_type' => \App\Models\Post::class, ]); # Or do you want to give abilities to all records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => null, // null means all records 'entity_type' => \App\Models\Post::class, ]); # To check $user->ability->able('delete', $post); $user->ability->able('delete', Post::class);
免责声明
所有维护者、贡献者和该包本身对使用此包可能造成的任何直接或间接损害不承担责任。
安全性
如果您发现任何安全相关的问题,请通过电子邮件 bug@octopy.dev 联系,而不是使用问题跟踪器。
许可证
此包受 MIT 许可证的许可。