mr-luke / privileges
Laravel 5 包,为您的应用程序添加角色和权限。
Requires
- php: >=7.1
- laravel/framework: ^8.0|^7.0|^6.0|^5.8
- mr-luke/configuration: ^1.0
Requires (Dev)
- fzaninotto/faker: ^1.9
- mockery/mockery: ^1.0
- orchestra/testbench: ^6.0|^5.0|^4.0|^3.8.0
- phpunit/phpunit: ^8.0|^7.0
This package is auto-updated.
Last update: 2024-09-08 19:01:24 UTC
README
此包提供了支持多角色、权限和限制的权限管理器。
入门指南
Privileges Manager 使用 Laravel 5.5
开发。建议在使用之前进行测试。需要 PHP >= 7.1.3。
注意!此包仍处于 Beta 版本。
安装
要通过 composer 安装,只需将以下内容放入您的 composer.json 文件中,并运行 composer update
{ "require": { "mr-luke/privileges": "~1.0.0" } }
或者使用以下命令
composer require "mr-luke/privileges"
接下来,将服务提供者添加到 app/config/app.php
Mrluke\Privileges\PrivilegesServiceProvider::class,
注意:包是自动可发现的!
配置
要使用 Privileges
,您需要设置您的 Authorizable
模型以及 配置文件 中允许的 scopes
。
/* |-------------------------------------------------------------------------- | Authorizable model class |-------------------------------------------------------------------------- | | This config specify which model class is authorizable. | */ 'authorizable' => \App\User::class, /* |-------------------------------------------------------------------------- | Available scopes |-------------------------------------------------------------------------- | | This config is a list of all available in application scopes. | */ 'scopes' => [ 'users', 'settings', ],
您还可以设置一个映射规则,将给定的 Eloquent 模型引用转换为作用域
/* |-------------------------------------------------------------------------- | Models mapping |-------------------------------------------------------------------------- | | This config allows you to map all application models to specific scopes. | | Example: \App\Model::class => 'scope' | */ 'mapping' => [ \App\Users::class => 'users' ],
默认情况下,Detector
在允许或拒绝访问时返回 bool
值,但您可以设置一个自定义的
'allowed_value' => true, 'denied_value' => false,
您还可以通过命令发布配置文件
php artisan vendor:publish
使用
外观
您可以使用 Mrluke\Privileges\Facades
命名空间访问 Manager
和 Detector
。
合同
Privileges
是一个使用合同构建的包。首先需要将 Mrluke\Privileges\Contracts\Authorizable
实现到您的 User 模型中。建议使用默认的 Mrluke\Privilges\Extentions\Authorizable
特性,并结合合同使用。
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Mrluke\Privileges\Contracts\Authorizable as Contract; use Mrluke\Privileges\Extentions\Authorizable; class User extends Authenticatable implements Contract { use Authorizable, Notifiable; }
管理器 - Mrluke\Privileges\Manager
Manager
是一个主要工具,提供了一个简单界面来为 Authorizable
分配 Role
和 Privileges
。
assignRole(Authorizable $auth, $role): void
允许您将新的 Mrluke\Privileges\Models\Role
分配给 Authorizable
。 $role
可以是三个值之一:如果知道 Role
id,则是 int
;如果是 Role
实例;如果是想一次分配多个角色的 array<int>
。
considerPermission(Authorizable $auth, string $scope): int
根据作用域中的角色和个人的权限返回给定 Authorizable
的权限级别。 注意!个人权限始终在顶部。
considerRestriction(Authorizable $auth, string $scope): array
返回给定 $scope
中限制的数组,例如:给定 Authorizable
的 IP 或时间限制。
detectScope(string $model)
使用此方法,您可以获取通过映射连接到模型的作用域,例如:\App\User::class => 'users'
。
getPermission(Permitable $subject, string $scope): mixed
返回给定 Authorizable
和 scope
的 Mrluke\Privileges\Models\Permission
实例。
grantPermission(Permitable $subject, string $scope, int $level): void
在给定的 scope
和 level
中创建新的 Permission
,并将其分配给给定的 Mrluke\Privileges\Contracts\Permitable
。 注意!Authorizable
和 Role
实现了 Permitable
合同。
hasPermission(Permitable $subject, string $scope): bool
确定给定的 Permitable
是否分配了 scope
权限。
regainPermission(Permitable $subject, string $scope): void
从 Permitable
中恢复 Permission
。
removeRole(Authorizable $auth, $role): void
从 Authorizable
中删除 Role
。
级别
可以将5种不同的权限
级别应用于可许可
。所有这些组合了多角色、个人权限、限制和角色级别,为您提供了广泛的可能性,但让我们来看看这5种。
- 0 - 无访问权限。
- 1 -
可授权
只能查看。 - 2 -
可授权
可以管理 & 查看,但仅限于现有的。 - 3 -
可授权
可以创建 & 管理自己的。 - 4 -
可授权
可以管理所有。
探测器 - Mrluke\Privileges\Detector
探测器
是一个主要工具,为检测可授权
权限提供了一个接口。有6种方法可以为您执行各种检查。
has(Model $model, string $relation = null): bool
此方法负责在两个模型之间存在多对多关系时belongsToMany
场景。默认情况下,函数(关系)的名称(关系)是从$model
基类名称(复数)检测到的。如果您有自己的命名约定,请使用参数$relation
提供函数名称。
hasOrLevel(Model $model, int $min, string $relation = null): bool
此方法是has
和level
的行组合。
level(int $min): bool
此方法检测可授权
是否已分配满足条件的权限
。
owner(Model $model, string $foreign = null): bool
此方法负责当可授权
通过扁平关系是模型的所有者时的hasOne
和hasMany
场景。默认情况下,外键是从可授权
基类名称(带有_id
)检测到的。如果您有不同的键名,请使用参数$foreign
提供外键列的名称。
ownerOrLevel(Model $model, int $min, string $foreign = null): bool
此方法是owner
和level
的行组合。
share(Model $model, string $modelRelation, string $relation): bool
此方法负责更复杂的关系场景,当可授权
与$model
的父级共享某些模型时。想象一下,您有一个User
,它有许多Thread
,这些Thread
可以有多个Reply
。现在您需要考虑用户是否可以评价Reply
,而只有所有者才能这样做。这就是此方法的作用。
Detector::subject($auth)->share($reply, 'thread', 'threads');
主题 & 范围
探测器有两个需要预定义的方法。在进行任何检查之前,您需要设置一个主题
$detector->subject($authorizable);
如果您想检测级别,您还需要通过指定范围
$detector->scope($scope);
示例
- 让我们检查用户是否可以更新所有帖子(级别4)
/** * Determine whether the user can update the post. * * @param \App\Models\User $user * @param \App\Models\Post $post * @return mixed */ public function update(User $user, Post $post) { $scope = Manager::detectScope(Post::class); return Detector::subject($user)->scope($scope)->level(4); }
- 让我们检查用户是否是所有者或可以更新所有帖子(自定义键)
/** * Determine whether the user can update the post. * * @param \App\Models\User $user * @param \App\Models\Post $post * @return mixed */ public function update(User $user, Post $post) { $scope = Manager::detectScope(Post::class); return Detector::subject($user)->scope($scope)->ownerOrLevel($post, 4, 'author_id'); }
注意!Manager
和Detector
是单例实例。
计划
- 测试
- 策略的自动检测扩展
- 双因素确认(经理/总监批准)