components([ TextInput::make('name')->label('Name')->required()->maxLength(120), TextInput::make('email')->label('E-Mail')->email()->required()->maxLength(255), TextInput::make('password') ->label('Passwort') ->password() ->revealable() ->required(fn (string $operation): bool => $operation === 'create') ->dehydrated(fn (?string $state): bool => filled($state)) ->minLength(8), Select::make('status') ->label('Status') ->options(collect(AccountStatus::cases())->mapWithKeys( fn (AccountStatus $status): array => [$status->value => $status->label()], )->all()) ->required() ->default(AccountStatus::Aktiv->value), CheckboxList::make('roles') ->label('Personalrollen') ->options(StaffRole::options()) ->columns(2), ]); } public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('name')->label('Name')->searchable(), TextColumn::make('email')->label('E-Mail')->searchable(), TextColumn::make('status')->label('Status')->formatStateUsing( fn (AccountStatus|string|null $state): string => $state instanceof AccountStatus ? $state->label() : (string) $state, ), TextColumn::make('staffRoles.role') ->label('Rollen') ->badge() ->formatStateUsing(fn (StaffRole|string $state): string => $state instanceof StaffRole ? $state->label() : StaffRole::from($state)->label()), ]); } public static function getPages(): array { return [ 'index' => ListUsers::route('/'), 'create' => CreateUser::route('/create'), 'edit' => EditUser::route('/{record}/edit'), ]; } /** * @param array $data */ public static function persist(User $user, array $data, bool $creating): User { $roles = $data['roles'] ?? []; unset($data['roles']); if ($creating) { $user = app(RegisterAccount::class)->register( $data['name'], $data['email'], $data['password'], true, ); $user->forceFill([ 'status' => $data['status'], 'email_verified_at' => now(), ])->save(); } else { if (blank($data['password'] ?? null)) { unset($data['password']); } $user->update($data); $actor = Filament::auth()->user(); if ($actor instanceof User && $actor->is($user) && array_key_exists('password', $data)) { $actor->refresh(); session()->put([ 'password_hash_staff' => $actor->getAuthPassword(), ]); } } $actor = Filament::auth()->user() ?? auth()->user(); app(AssignStaffRoles::class)->sync($user, $roles, $actor); return $user; } }