doc88 / flux-entity-permission
Laravel应用程序中实现实体访问控制的库。
1.1.0
2020-07-06 19:21 UTC
Requires
- php: >= 7.2.0
This package is auto-updated.
Last update: 2024-09-07 22:03:41 UTC
README
Laravel应用程序中实现实体访问控制的库。
需求
- Laravel >= 6.0
安装
- 在项目根目录下运行以下命令,将包添加到Laravel应用程序中
composer require doc88/flux-entity-permission
- 在config/app.php文件的providers列表中添加
'providers' => [ ... Doc88\FluxEntityPermission\FluxEntityPermissionServiceProvider::class, ]
- 在项目根目录下运行以下命令发布新的提供者
php artisan vendor:publish
- 运行迁移
php artisan migrate
- 在您的用户模型中添加以下行
use Doc88\FluxEntityPermission\Traits\HasEntityPermissions; class User { use HasEntityPermissions; }
用法
Doc88\FluxEntityPermission\EntityPermission 类
用于列出、注册、验证和撤销对实体的权限的类。
- 从用户的权限中列出实体
// Entities which the user has access to EntityPermission::list($user); // Specifying which entity you want to list EntityPermission::list($user, 'App\Company'); /** * Return: array */
- 从用户的权限中列出实体ID
// Ids of entities which the user has access to EntityPermission::idList($user, 'App\Company'); /** * Return: array */
- 检查用户对实体的权限
// The entity you want to access $company = Company::find(1); // Checking if the user has access to the entity EntityPermission::has($user, $company); /** * Return: true or false */
- 记录实体的权限
// The entity you want to access $company = Company::find(1); // Grants permission to the entity for the User EntityPermission::register($user, $company); /** * Return: true or false */
- 撤销实体的权限
// The entity you want to access $company = Company::find(1); // Revokes permission to the Entity EntityPermission::revoke($user, $company); /** * Return: true or false */
使用用户模型
可以使用用户类列出、注册、验证和撤销实体的权限。
- 列出用户权限
$user = User::find(1); // Entities which the user has access to $user->listEntityAccess(); // Specifying which entity you want to list $user->listEntityAccess('App\Company'); /** * Return: array */
- 检查用户对实体的权限
$user = User::find(1); // The entity you want to access $company = Company::find(1); // Checking if the user has access to the entity $user->hasEntityAccess($company); /** * Return: true or false */
- 记录实体的权限
$user = User::find(1); // The entity you want to access $company = Company::find(1); // Grants permission to the entity for the User $user->registerEntityAccess($company); /** * Return: true or false */
- 撤销实体的权限
$user = User::find(1); // The entity you want to access $company = Company::find(1); // Revokes permission to the entity $user->revokeEntityAccess($company); /** * Return: true or false */