add servers relationship to users
This commit is contained in:
		
							parent
							
								
									287c657e60
								
							
						
					
					
						commit
						32a3b8dd9b
					
				| @ -348,7 +348,8 @@ class ServerResource extends Resource | ||||
|                     ->columnSpanFull(), | ||||
| 
 | ||||
|                 Forms\Components\KeyValue::make('environment') | ||||
|                     ->default([]), | ||||
|                     ->default([]) | ||||
|                     ->columnSpanFull(), | ||||
| 
 | ||||
|                 Forms\Components\Section::make('Egg Variables') | ||||
|                     ->icon('tabler-eggs') | ||||
|  | ||||
| @ -3,8 +3,10 @@ | ||||
| namespace App\Filament\Resources; | ||||
| 
 | ||||
| use App\Filament\Resources\UserResource\Pages; | ||||
| use App\Filament\Resources\UserResource\RelationManagers; | ||||
| use App\Models\User; | ||||
| use Filament\Forms; | ||||
| use Filament\Forms\Components\Section; | ||||
| use Filament\Forms\Form; | ||||
| use Filament\Resources\Resource; | ||||
| use Filament\Tables; | ||||
| @ -23,54 +25,56 @@ class UserResource extends Resource | ||||
|     { | ||||
|         return $form | ||||
|             ->schema([ | ||||
|                 Forms\Components\TextInput::make('username')->required()->maxLength(191), | ||||
|                 Forms\Components\TextInput::make('email')->email()->required()->maxLength(191), | ||||
|                 Section::make()->schema([ | ||||
|                     Forms\Components\TextInput::make('username')->required()->maxLength(191), | ||||
|                     Forms\Components\TextInput::make('email')->email()->required()->maxLength(191), | ||||
| 
 | ||||
|                 Forms\Components\TextInput::make('name_first') | ||||
|                     ->maxLength(191) | ||||
|                     ->hidden(fn (string $operation): bool => $operation === 'create') | ||||
|                     ->label('First Name'), | ||||
|                 Forms\Components\TextInput::make('name_last') | ||||
|                     ->maxLength(191) | ||||
|                     ->hidden(fn (string $operation): bool => $operation === 'create') | ||||
|                     ->label('Last Name'), | ||||
|                     Forms\Components\TextInput::make('name_first') | ||||
|                         ->maxLength(191) | ||||
|                         ->hidden(fn (string $operation): bool => $operation === 'create') | ||||
|                         ->label('First Name'), | ||||
|                     Forms\Components\TextInput::make('name_last') | ||||
|                         ->maxLength(191) | ||||
|                         ->hidden(fn (string $operation): bool => $operation === 'create') | ||||
|                         ->label('Last Name'), | ||||
| 
 | ||||
|                 Forms\Components\TextInput::make('password') | ||||
|                     ->dehydrateStateUsing(fn (string $state): string => Hash::make($state)) | ||||
|                     ->dehydrated(fn (?string $state): bool => filled($state)) | ||||
|                     ->required(fn (string $operation): bool => $operation === 'create') | ||||
|                     ->password(), | ||||
|                     Forms\Components\TextInput::make('password') | ||||
|                         ->dehydrateStateUsing(fn (string $state): string => Hash::make($state)) | ||||
|                         ->dehydrated(fn (?string $state): bool => filled($state)) | ||||
|                         ->required(fn (string $operation): bool => $operation === 'create') | ||||
|                         ->password(), | ||||
| 
 | ||||
|                 Forms\Components\ToggleButtons::make('root_admin') | ||||
|                     ->label('Administrator (Root)') | ||||
|                     ->options([ | ||||
|                         false => 'No', | ||||
|                         true => 'Admin', | ||||
|                     ]) | ||||
|                     ->colors([ | ||||
|                         false => 'primary', | ||||
|                         true => 'danger', | ||||
|                     ]) | ||||
|                     ->disableOptionWhen(function (string $operation, $value, User $user) { | ||||
|                         if ($operation !== 'edit' || $value) { | ||||
|                             return false; | ||||
|                         } | ||||
|                     Forms\Components\ToggleButtons::make('root_admin') | ||||
|                         ->label('Administrator (Root)') | ||||
|                         ->options([ | ||||
|                             false => 'No', | ||||
|                             true => 'Admin', | ||||
|                         ]) | ||||
|                         ->colors([ | ||||
|                             false => 'primary', | ||||
|                             true => 'danger', | ||||
|                         ]) | ||||
|                         ->disableOptionWhen(function (string $operation, $value, User $user) { | ||||
|                             if ($operation !== 'edit' || $value) { | ||||
|                                 return false; | ||||
|                             } | ||||
| 
 | ||||
|                         return $user->isLastRootAdmin(); | ||||
|                     }) | ||||
|                     ->hint(fn (User $user) => $user->isLastRootAdmin() ? 'This is the last root administrator!' : '') | ||||
|                     ->helperText(fn (User $user) => $user->isLastRootAdmin() ? 'You must have at least one root administrator in your system.' : '') | ||||
|                     ->hintColor('warning') | ||||
|                     ->inline() | ||||
|                     ->required() | ||||
|                     ->default(false), | ||||
|                             return $user->isLastRootAdmin(); | ||||
|                         }) | ||||
|                         ->hint(fn (User $user) => $user->isLastRootAdmin() ? 'This is the last root administrator!' : '') | ||||
|                         ->helperText(fn (User $user) => $user->isLastRootAdmin() ? 'You must have at least one root administrator in your system.' : '') | ||||
|                         ->hintColor('warning') | ||||
|                         ->inline() | ||||
|                         ->required() | ||||
|                         ->default(false), | ||||
| 
 | ||||
|                 Forms\Components\Hidden::make('skipValidation')->default(true), | ||||
|                 Forms\Components\Select::make('language') | ||||
|                     ->required() | ||||
|                     ->hidden() | ||||
|                     ->default('en') | ||||
|                     ->options(fn (User $user) => $user->getAvailableLanguages()), | ||||
|                     Forms\Components\Hidden::make('skipValidation')->default(true), | ||||
|                     Forms\Components\Select::make('language') | ||||
|                         ->required() | ||||
|                         ->hidden() | ||||
|                         ->default('en') | ||||
|                         ->options(fn (User $user) => $user->getAvailableLanguages()), | ||||
|                 ])->columns(2), | ||||
|             ]); | ||||
|     } | ||||
| 
 | ||||
| @ -130,7 +134,7 @@ class UserResource extends Resource | ||||
|     public static function getRelations(): array | ||||
|     { | ||||
|         return [ | ||||
|             //
 | ||||
|             RelationManagers\ServersRelationManager::class, | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -0,0 +1,55 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace App\Filament\Resources\UserResource\RelationManagers; | ||||
| 
 | ||||
| use App\Models\Server; | ||||
| use Filament\Tables; | ||||
| use Filament\Tables\Table; | ||||
| use Filament\Resources\RelationManagers\RelationManager; | ||||
| 
 | ||||
| class ServersRelationManager extends RelationManager | ||||
| { | ||||
|     protected static string $relationship = 'servers'; | ||||
| 
 | ||||
|     public function table(Table $table): Table | ||||
|     { | ||||
|         return $table | ||||
|             ->searchable(false) | ||||
|             ->columns([ | ||||
|                 Tables\Columns\TextColumn::make('uuid') | ||||
|                     ->hidden() | ||||
|                     ->label('UUID') | ||||
|                     ->searchable(), | ||||
|                 Tables\Columns\TextColumn::make('name') | ||||
|                     ->icon('tabler-brand-docker') | ||||
|                     ->searchable() | ||||
|                     ->sortable(), | ||||
|                 Tables\Columns\TextColumn::make('node.name') | ||||
|                     ->icon('tabler-server-2') | ||||
|                     ->url(fn (Server $server): string => route('filament.admin.resources.nodes.edit', ['record' => $server->node])) | ||||
|                     ->sortable(), | ||||
|                 Tables\Columns\TextColumn::make('egg.name') | ||||
|                     ->icon('tabler-egg') | ||||
|                     ->url(fn (Server $server): string => route('filament.admin.resources.eggs.edit', ['record' => $server->egg])) | ||||
|                     ->sortable(), | ||||
|                 Tables\Columns\SelectColumn::make('allocation.id') | ||||
|                     ->label('Primary Allocation') | ||||
|                     ->options(fn ($state, Server $server) => [$server->allocation->id => $server->allocation->address]) | ||||
|                     ->selectablePlaceholder(false) | ||||
|                     ->sortable(), | ||||
|                 Tables\Columns\TextColumn::make('image')->hidden(), | ||||
|                 Tables\Columns\TextColumn::make('databases_count') | ||||
|                     ->counts('databases') | ||||
|                     ->label('Databases') | ||||
|                     ->icon('tabler-database') | ||||
|                     ->numeric() | ||||
|                     ->sortable(), | ||||
|                 Tables\Columns\TextColumn::make('backups_count') | ||||
|                     ->counts('backups') | ||||
|                     ->label('Backups') | ||||
|                     ->icon('tabler-file-download') | ||||
|                     ->numeric() | ||||
|                     ->sortable(), | ||||
|             ]); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Scai
						Scai