feat(权限): 实现用户权限管理功能
- 更新 UserResource 添加角色和权限管理 - 添加角色选择字段(多选) - 添加直接权限配置(按模块分组的复选框列表) - 在用户列表中显示角色和权限数量 - 添加角色筛选器 - 防止删除超级管理员 - 创建 ViewUser 页面显示用户详细权限信息 - 显示所有权限(角色权限 + 直接权限) - 按模块分组展示权限 - 区分显示直接权限 - 创建 UserPolicy 控制用户管理权限 - 基于 user.* 权限控制访问 - 保护超级管理员不被编辑和删除 - 防止用户删除自己 - 在 AppServiceProvider 中注册 UserPolicy
This commit is contained in:
74
app/Policies/UserPolicy.php
Normal file
74
app/Policies/UserPolicy.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class UserPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
/**
|
||||
* 查看用户列表
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return $user->can('user.view');
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看单个用户
|
||||
*/
|
||||
public function view(User $user, User $model): bool
|
||||
{
|
||||
return $user->can('user.view');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return $user->can('user.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
*/
|
||||
public function update(User $user, User $model): bool
|
||||
{
|
||||
// 超级管理员只能由超级管理员编辑
|
||||
if ($model->isSuperAdmin() && !$user->isSuperAdmin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user->can('user.update');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*/
|
||||
public function delete(User $user, User $model): bool
|
||||
{
|
||||
// 不能删除超级管理员
|
||||
if ($model->isSuperAdmin()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 不能删除自己
|
||||
if ($user->id === $model->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user->can('user.delete');
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除用户
|
||||
*/
|
||||
public function deleteAny(User $user): bool
|
||||
{
|
||||
return $user->can('user.delete');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user