feat(权限): 创建角色管理资源(RoleResource)

- 创建 RoleResource 及其所有页面类
- 实现角色列表、创建、编辑、查看功能
- 权限选择器按模块分组显示,支持批量选择
- 实现 super-admin 角色保护(不可编辑和删除)
- 实现角色删除前检查(有关联用户时不可删除)
- 创建 RolePolicy 控制角色管理权限
- 在 AppServiceProvider 中注册 RolePolicy
- 角色列表显示权限数量和用户数量
- 完整的中文界面和提示信息
This commit is contained in:
2026-03-11 10:00:29 +08:00
parent 7d13a560f3
commit a17fe167b0
7 changed files with 526 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
<?php
namespace App\Filament\Resources\RoleResource\Pages;
use App\Filament\Resources\RoleResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
use Filament\Infolists;
use Filament\Infolists\Infolist;
class ViewRole extends ViewRecord
{
protected static string $resource = RoleResource::class;
protected function getHeaderActions(): array
{
return [
Actions\EditAction::make()
->label('编辑')
->visible(fn (): bool => $this->record->name !== 'super-admin'),
];
}
public function infolist(Infolist $infolist): Infolist
{
return $infolist
->schema([
Infolists\Components\Section::make('角色信息')
->schema([
Infolists\Components\TextEntry::make('name')
->label('角色标识')
->badge()
->color(fn (string $state): string => match ($state) {
'super-admin' => 'danger',
'admin' => 'warning',
'user' => 'success',
default => 'gray',
}),
Infolists\Components\TextEntry::make('guard_name')
->label('守卫')
->badge(),
Infolists\Components\TextEntry::make('permissions_count')
->label('权限数量')
->getStateUsing(fn ($record) => $record->permissions()->count())
->badge()
->color('info'),
Infolists\Components\TextEntry::make('users_count')
->label('用户数量')
->getStateUsing(fn ($record) => $record->users()->count())
->badge()
->color('success'),
Infolists\Components\TextEntry::make('created_at')
->label('创建时间')
->dateTime('Y-m-d H:i:s'),
Infolists\Components\TextEntry::make('updated_at')
->label('更新时间')
->dateTime('Y-m-d H:i:s'),
])
->columns(2),
Infolists\Components\Section::make('权限列表')
->schema([
Infolists\Components\RepeatableEntry::make('permissions')
->label('')
->schema([
Infolists\Components\TextEntry::make('name')
->label('权限')
->badge()
->formatStateUsing(function (string $state): string {
// 格式化权限名称
$parts = explode('.', $state);
$module = $parts[0] ?? '';
$action = $parts[1] ?? '';
$moduleNames = [
'document' => '文档管理',
'system-setting' => '系统设置',
'activity-log' => '操作日志',
'terminal' => '终端管理',
'sop-template' => 'SOP模板',
'group' => '分组管理',
'user' => '用户管理',
'role' => '角色管理',
];
$actionNames = [
'viewAny' => '查看列表',
'view' => '查看详情',
'create' => '创建',
'update' => '编辑',
'delete' => '删除',
'download' => '下载',
'export' => '导出',
'sync' => '同步',
'publish' => '发布',
'archive' => '归档',
];
$moduleName = $moduleNames[$module] ?? $module;
$actionName = $actionNames[$action] ?? $action;
return "{$moduleName} - {$actionName}";
}),
])
->columns(3)
->grid(3),
])
->collapsible(),
]);
}
}