fortix / shieldify
为Laravel提供全面的角色和权限管理包。
1.0
2024-07-19 20:02 UTC
Requires
- php: ^7.3|^8.0
- illuminate/support: ^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-09-19 20:33:39 UTC
README
Shieldify是一个为简化应用中的角色、权限和模块管理而设计的Laravel包。它提供了一个流畅、易于使用的API,用于将权限附加到角色、将角色分配给用户,以及执行用户授权的权限检查。
目录
安装
要在项目中安装Shieldify,请运行以下命令
composer require fortix/shieldify
手动服务提供者注册
如果Laravel的包自动发现没有自动注册Shieldify服务提供者,请手动将其添加到config/app.php文件中的providers数组
'providers' => [
...
Fortix\Shieldify\ShieldifyServiceProvider::class,
],
-
打开config/app.php
-
查找aliases数组
-
添加一个新的条目为您的Shieldify外观,如下所示
'aliases' => [ ... 'Shieldify' => \Fortix\Shieldify\Facades\ShieldifyFacade::class, ],
安装后,您可以使用以下命令发布包的资产
php artisan vendor:publish --provider="Fortix\Shieldify\ShieldifyServiceProvider"
此命令发布配置、迁移以及Shieldify正常运行所需的任何其他资产。运行迁移以设置所需的数据库表
php artisan migrate
配置
发布后,shieldify.php
配置文件将位于您的config目录中。此文件允许您自定义Shieldify的各种方面,包括权限的缓存行为以提高性能。
<?php
return [
// Whether to use cache for storing permissions and roles
'use_cache' => env('SHIELDFY_USE_CACHE', true),
// Default context for the package usage. Possible values: 'web', 'api', 'both'
'context' => env('SHIELDFY_CONTEXT', 'web'),
// Cache duration in minutes. Determines how long permissions and roles are stored in cache
'cache_duration' => env('SHIELDFY_CACHE_DURATION', 60),
// Other configuration values can be added here as needed
];
基本用法
设置关系
要集成Shieldify与您的User模型,请在您的User模型中使用ShieldifyUserTrait
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Fortix\Shieldify\Traits\ShieldifyUserTrait;
class User extends Authenticatable
{
use ShieldifyUserTrait;
// Your model's content
}
定义角色、权限和模块
直接使用Eloquent模型或通过Shieldify外观创建角色、权限和模块。
use Fortix\Shieldify\Models\{Role, Permission, Module};
// Create a role
$role = Role::create(['name' => 'Editor']);
// Create a module
$module = Module::create(['name' => 'Articles']);
您可以使用Shieldify外观
来完成此操作
use Shieldify;
// To create a role named 'Employee'
Shieldify::createRole('Employee');
// To update a role named 'Employee' to 'Super Employee'
Shieldify::updateRole('Employee', 'Super Employee');
// To assign the role 'Employee' to a user with a specific ID
Shieldify::assignRoleToUser($userId, 'Employee');
// To create a module named 'Posts'
Shieldify::createModule('Posts');
// To delete a role named 'Employee'
Shieldify::deleteRole('Employee');
// To delete a module named 'Posts'
Shieldify::deleteModule('Posts');
//To Get all roles and modules
Shieldify::getAllRoles();
Shieldify::getAllModules();
将角色分配给用户
轻松地将角色分配给用户以授予他们相关权限。
$user = User::find(1); // Assuming an existing user
Shieldify::assignRoleToUser($user->id, 'Editor');
授予和撤销权限
use Shieldify;
// To grant permissions
$granted = Shieldify::grantPermissionsToRole('Editor', 'Posts', ['create', 'edit']);
if ($granted) {
echo "Permissions granted successfully.";
} else {
echo "Failed to grant permissions.";
}
// To revoke permissions
$revoked = Shieldify::revokePermissionsFromRole('Editor', 'Posts', ['delete']);
if ($revoked) {
echo "Permissions revoked successfully.";
} else {
echo "Failed to revoke permissions.";
}
检查权限
执行权限检查以控制对应用功能的访问。
if (Shieldify::module('Articles')->hasPermission('edit')) {
// The user has permission to edit articles
}
中间件
Shieldify提供了基于角色和权限的路由保护中间件。
在您的app/Http/Kernel.php中
protected $routeMiddleware = [
'role' => \Fortix\Shieldify\Http\Middleware\EnsureRole::class,
'permission' => \Fortix\Shieldify\Http\Middleware\EnsurePermission::class,
];
在您的routes/web.php或routes/api.php中
Route::middleware(['role:Editor'])->group(function () {
// Routes accessible only by users with the 'Editor' role
});
Route::middleware(['permission:edit Articles'])->group(function () {
// Routes requiring 'edit' permission on 'Articles' module
});
场景1:已登录用户
$roles = Shieldify::allRoles(); // Fetches all roles for the logged-in user
$rolesWithPermissions = Shieldify::allRolesWithPermissions(); // Fetches all roles and their permissions for the logged-in user
场景2:通过ID指定的特定用户
$user = User::find($userId); // Assuming you have the user's ID
Shieldify::setUser($user); // Set the user context
$roles = Shieldify::allRoles(); // Fetches all roles for the specified user
$rolesWithPermissions = Shieldify::allRolesWithPermissions(); // Fetches all roles and their permissions for the specified user
高级用法
完整文档将很快提供
支持
如需支持,请在GitHub存储库中创建一个问题。