Files
KnowledgeBase/app/Filament/Resources/SystemSettingResource.php
lizhuoran a100b2dce7 feat(权限): 为所有 Filament 资源添加导航菜单权限控制
- DocumentResource: 添加 document.view 权限检查
- SystemSettingResource: 添加 system-setting.view 权限检查
- ActivityLogResource: 添加 activity-log.view 权限检查
- TerminalResource: 添加 terminal.view 权限检查
- SopTemplateResource: 添加 sop-template.view 权限检查
- GroupResource: 添加 group.view 权限检查
- UserResource: 添加 user.view 权限检查
- RoleResource: 添加 role.viewAny 权限检查

所有资源都实现了 shouldRegisterNavigation() 方法
根据用户权限动态显示/隐藏导航菜单项
2026-03-11 10:14:16 +08:00

202 lines
8.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\SystemSettingResource\Pages;
use App\Models\SystemSetting;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
class SystemSettingResource extends Resource
{
protected static ?string $model = SystemSetting::class;
protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';
protected static ?string $navigationLabel = '系统设置';
protected static ?string $modelLabel = '系统设置';
protected static ?string $pluralModelLabel = '系统设置';
protected static ?int $navigationSort = 1;
/**
* 控制导航菜单是否显示
*/
public static function shouldRegisterNavigation(): bool
{
return auth()->user()?->can('system-setting.view') ?? false;
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Tabs::make('配置表单')
->tabs([
// 基本信息标签页
Forms\Components\Tabs\Tab::make('基本信息')
->icon('heroicon-o-information-circle')
->schema([
Forms\Components\TextInput::make('key')
->label('配置键')
->required()
->unique(ignoreRecord: true)
->maxLength(255)
->minLength(3)
->regex('/^[a-z0-9_\.]+$/')
->helperText('配置的唯一标识符,只能包含小写字母、数字、下划线和点,例如: embedding.model_name')
->placeholder('例如: system.name')
->validationMessages([
'regex' => '配置键只能包含小写字母、数字、下划线和点',
]),
Forms\Components\Select::make('group')
->label('配置分组')
->required()
->options([
'embedding' => '嵌入模型',
'chunking' => '分块参数',
'system' => '系统配置',
'search' => '搜索配置',
])
->native(false)
->helperText('选择配置所属的分组'),
Forms\Components\Textarea::make('description')
->label('配置说明')
->rows(3)
->maxLength(65535)
->minLength(5)
->helperText('描述此配置项的用途至少5个字符')
->columnSpanFull(),
Forms\Components\Toggle::make('is_public')
->label('公开配置')
->helperText('公开配置可以被前端访问')
->default(false)
->inline(false),
]),
// 配置值标签页
Forms\Components\Tabs\Tab::make('配置值')
->icon('heroicon-o-cog-6-tooth')
->schema([
Forms\Components\KeyValue::make('value')
->label('配置值')
->required()
->helperText('以键值对形式输入配置内容。键名应与配置键的最后一部分匹配。')
->addActionLabel('添加配置项')
->keyLabel('配置项名称')
->valueLabel('配置项值')
->reorderable(false)
->columnSpanFull(),
]),
])
->columnSpanFull()
->contained(false),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('key')
->label('配置键')
->searchable()
->sortable()
->copyable()
->tooltip('点击复制'),
Tables\Columns\TextColumn::make('group')
->label('配置分组')
->badge()
->color(fn (string $state): string => match ($state) {
'embedding' => 'info',
'chunking' => 'success',
'system' => 'warning',
'search' => 'primary',
default => 'gray',
})
->formatStateUsing(fn (string $state): string => match ($state) {
'embedding' => '嵌入模型',
'chunking' => '分块参数',
'system' => '系统配置',
'search' => '搜索配置',
default => $state,
})
->sortable(),
Tables\Columns\TextColumn::make('description')
->label('说明')
->limit(50)
->tooltip(function (Tables\Columns\TextColumn $column): ?string {
$state = $column->getState();
if (strlen($state) > 50) {
return $state;
}
return null;
}),
Tables\Columns\IconColumn::make('is_public')
->label('公开')
->boolean()
->trueIcon('heroicon-o-check-circle')
->falseIcon('heroicon-o-x-circle')
->trueColor('success')
->falseColor('danger')
->tooltip(fn (bool $state): string => $state ? '公开配置' : '私有配置'),
Tables\Columns\TextColumn::make('updated_at')
->label('更新时间')
->dateTime('Y-m-d H:i:s')
->sortable()
->toggleable(),
])
->filters([
Tables\Filters\SelectFilter::make('group')
->label('配置分组')
->options([
'embedding' => '嵌入模型',
'chunking' => '分块参数',
'system' => '系统配置',
'search' => '搜索配置',
]),
Tables\Filters\TernaryFilter::make('is_public')
->label('公开状态')
->placeholder('全部')
->trueLabel('公开')
->falseLabel('私有'),
])
->actions([
Tables\Actions\ViewAction::make()
->label('查看'),
Tables\Actions\EditAction::make()
->label('编辑'),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make()
->label('批量删除'),
]),
])
->defaultSort('group', 'asc');
}
public static function getPages(): array
{
return [
'index' => Pages\ListSystemSettings::route('/'),
'create' => Pages\CreateSystemSetting::route('/create'),
'edit' => Pages\EditSystemSetting::route('/{record}/edit'),
'view' => Pages\ViewSystemSetting::route('/{record}'),
];
}
}