Loheide-eu-mitarbeiterplatt.../app/Filament/Resources/Users/Schemas/UserForm.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

150 lines
8.7 KiB
PHP
Raw Permalink 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\Users\Schemas;
use App\Models\ProfileField;
use App\Models\Role;
use App\Models\User;
use App\Support\Permissions\RoleGuard;
use App\Support\ProfileFields;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Component;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Validation\Rules\Password;
class UserForm
{
public const PHONE_REGEX = '/^[0-9+()\/\-.\s]{3,50}$/';
/**
* Eingabefelder für die frei definierten Profilfelder.
*
* @return list<Component>
*/
private static function customFields(): array
{
return app(ProfileFields::class)->all()->map(function (ProfileField $field) {
$name = 'custom_fields.'.$field->key;
$hint = $field->visibility === 'restricted' ? 'Nur für die Person und die Personalverwaltung sichtbar.' : null;
$component = match ($field->type) {
'textarea' => Textarea::make($name)->rows(3)->maxLength(5000)->columnSpanFull(),
'date' => DatePicker::make($name)->native(false)->displayFormat('d.m.Y'),
'email' => TextInput::make($name)->email()->maxLength(255),
'url' => TextInput::make($name)->url()->maxLength(500),
'phone' => TextInput::make($name)->tel()->telRegex(self::PHONE_REGEX)->maxLength(50),
default => TextInput::make($name)->maxLength(255),
};
return $component->label($field->label)->helperText($hint);
})->values()->all();
}
public static function configure(Schema $schema): Schema
{
return $schema
->columns(3)
->components([
Section::make('Person')
->columnSpan(2)
->columns(2)
->schema([
TextInput::make('first_name')->label('Vorname')->required()->maxLength(100),
TextInput::make('last_name')->label('Nachname')->required()->maxLength(100),
TextInput::make('email')
->label('E-Mail-Adresse')
->email()
->required()
->maxLength(255)
->unique(ignoreRecord: true)
->columnSpanFull(),
TextInput::make('job_title')->label('Funktion')->maxLength(150),
TextInput::make('department')->label('Abteilung')->maxLength(150),
TextInput::make('location')->label('Standort')->maxLength(150),
// Eigene Regel: Filaments Standard lehnt übliche Schreibweisen wie „+49 (0) 30 123“ ab.
TextInput::make('phone')->label('Telefon')->tel()->telRegex(self::PHONE_REGEX)->maxLength(50),
TextInput::make('mobile')->label('Mobil')->tel()->telRegex(self::PHONE_REGEX)->maxLength(50),
Select::make('manager_id')
->label('Vorgesetzte*r')
->helperText('Für Team-Anzeige und Organigramm. Wird bei Anmeldung über Entra ID von dort übernommen, wenn eingerichtet.')
->searchable()
->getSearchResultsUsing(fn (string $search, ?User $record) => User::query()->active()
->when($record, fn ($q) => $q->whereKeyNot($record->id))
->where(fn ($q) => $q->where('name', 'like', "%{$search}%")->orWhere('email', 'like', "%{$search}%"))
->orderBy('name')->limit(30)->pluck('name', 'id')->all())
->getOptionLabelUsing(fn ($value) => User::query()->find($value)?->name)
->rule(fn (?User $record) => function (string $attribute, mixed $value, \Closure $fail) use ($record) {
// Keine Kreise im Organigramm: niemand ist (indirekt) sich selbst vorgesetzt.
for ($id = (int) $value, $i = 0; $id && $i < 50; $i++) {
if ($record && $id === $record->id) {
$fail('Diese Person kann nicht vorgesetzt sein – sie ist dieser Person bereits unterstellt.');
return;
}
$id = (int) User::query()->whereKey($id)->value('manager_id');
}
}),
Textarea::make('about')->label('Über mich')->rows(3)->maxLength(2000)->columnSpanFull(),
]),
Section::make('Konto')
->columnSpan(1)
->schema([
Toggle::make('is_active')
->label('Aktiv')
->helperText('Deaktivierte Konten können sich nicht anmelden und erscheinen nicht im Portal.')
->default(true),
Select::make('manual_roles')
->label('Rollen')
->helperText('Rollen sind additiv. Rollen aus Identity-Provider-Gruppen werden automatisch vergeben.')
->multiple()
->preload()
->options(fn () => Role::query()->orderByDesc('is_system')->orderBy('name')->pluck('name', 'id')->all())
// Nur Rollen, deren Rechte man selbst besitzt (nie „Administration“ ohne Vollzugriff)
->disableOptionWhen(fn (string $value) => ! RoleGuard::canAssign(auth()->user(), Role::query()->findOrFail((int) $value)))
->afterStateHydrated(function (Select $component, ?User $record) {
$component->state($record?->roles()->wherePivot('source', 'manual')->pluck('roles.id')->all() ?? []);
})
->rule(function (?User $record) {
return function (string $attribute, mixed $value, \Closure $fail) use ($record) {
$before = $record?->roles()->wherePivot('source', 'manual')->pluck('roles.id')->all() ?? [];
$changed = array_merge(array_diff(array_map('intval', (array) $value), $before), array_diff($before, array_map('intval', (array) $value)));
foreach (Role::query()->whereIn('id', $changed)->get() as $role) {
if (! RoleGuard::canAssign(auth()->user(), $role)) {
$fail("Die Rolle „{$role->name}“ enthält Rechte, die Sie selbst nicht haben.");
}
}
// Die eigene Administrationsrolle kann man sich nicht selbst entziehen.
if ($record?->is(auth()->user()) && $record->hasRole(Role::ADMINISTRATION)
&& ! in_array(Role::administration()->id, array_map('intval', (array) $value), true)) {
$fail('Die eigene Rolle „Administration“ kann nicht entfernt werden.');
}
};
})
->saveRelationshipsUsing(fn (User $record, ?array $state) => $record->syncManualRoles(array_map('intval', $state ?? [])))
->dehydrated(false),
TextInput::make('password')
->label(fn (string $operation) => $operation === 'create' ? 'Passwort' : 'Neues Passwort')
->helperText(fn (string $operation) => $operation === 'create'
? 'Leer lassen, wenn sich die Person nur über den Identity Provider anmelden soll.'
: 'Leer lassen, um das Passwort nicht zu ändern.')
->password()
->revealable()
->rule(Password::default())
->dehydrated(fn (?string $state) => filled($state)),
]),
Section::make('Weitere Angaben')
->columnSpan(2)
->columns(2)
->schema(fn () => self::customFields())
->visible(fn () => app(ProfileFields::class)->all()->isNotEmpty()),
]);
}
}