116 lines
3.9 KiB
PHP
116 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Users;
|
|
|
|
use App\Domain\Identity\AccountStatus;
|
|
use App\Domain\Identity\AssignStaffRoles;
|
|
use App\Domain\Identity\RegisterAccount;
|
|
use App\Domain\Identity\StaffRole;
|
|
use App\Filament\Resources\Users\Pages\CreateUser;
|
|
use App\Filament\Resources\Users\Pages\EditUser;
|
|
use App\Filament\Resources\Users\Pages\ListUsers;
|
|
use App\Models\User;
|
|
use Filament\Forms\Components\CheckboxList;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
|
|
class UserResource extends Resource
|
|
{
|
|
protected static ?string $model = User::class;
|
|
|
|
protected static ?string $navigationLabel = 'Benutzer';
|
|
|
|
protected static ?string $modelLabel = 'Benutzer';
|
|
|
|
protected static ?string $pluralModelLabel = 'Benutzer';
|
|
|
|
protected static ?string $recordTitleAttribute = 'email';
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema->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<string, mixed> $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);
|
|
}
|
|
|
|
app(AssignStaffRoles::class)->sync($user, $roles, auth()->user());
|
|
|
|
return $user;
|
|
}
|
|
}
|