Loheide-eu-mitarbeiterplatt.../app/Support/ProfileFields.php
Friederich Loheide 6269d3302c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m43s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m52s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m49s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m49s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m12s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 1m59s
CI / Build-Artefakte aktuell (pull_request) Successful in 57s
Beliebige Entra-Attribute ins Profil übernehmen; eigene Profilfelder
- Profilfelder frei definierbar (Typ, Sichtbarkeit, selbst pflegbar), Werte als JSON am Benutzer
- Attribut-Zuordnung je Anbieter statt fester Feldliste: jedes Graph-Attribut inkl.
  verschachtelter Pfade, onPremisesExtensionAttributes und Verzeichniserweiterungen
- Erweiterungsattribute per Knopf aus Entra laden
- $select dynamisch aus der Zuordnung; Delta-Abgleich startet bei geänderter Zuordnung neu
- Rückfall auf Grundattribute, wenn einzelne Attribute nicht lesbar sind
- Ältere Konfiguration (synced_fields) wird weiter gelesen

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 08:09:57 +00:00

147 lines
4.7 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\Support;
use App\Models\ProfileField;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Throwable;
/**
* Frei definierbare Profilfelder: Definitionen (gecacht), Sichtbarkeit und Validierung.
*/
class ProfileFields
{
private const CACHE_KEY = 'profile-fields';
/** @var Collection<int, ProfileField>|null */
private ?Collection $fields = null;
/**
* @return Collection<int, ProfileField>
*/
public function all(): Collection
{
try {
// Im Cache nur Arrays – Laravel deserialisiert dort keine Objekte.
return $this->fields ??= ProfileField::hydrate(Cache::rememberForever(
self::CACHE_KEY,
fn () => ProfileField::query()->orderBy('sort')->orderBy('label')->get()->map->getAttributes()->all(),
));
} catch (Throwable $e) {
report($e);
return collect();
}
}
public function flush(): void
{
$this->fields = null;
Cache::forget(self::CACHE_KEY);
}
/**
* @return array<string, string> Schlüssel => Bezeichnung
*/
public function options(): array
{
return $this->all()->pluck('label', 'key')->all();
}
/**
* Felder mit Werten, die $viewer bei $person sehen darf.
*
* @return list<array{key: string, label: string, type: string, value: string}>
*/
public function visibleValues(User $person, ?User $viewer): array
{
$seesRestricted = $viewer && ($viewer->is($person) || $viewer->can('users.manage'));
$values = (array) ($person->custom_fields ?? []);
return $this->all()
->filter(fn (ProfileField $field) => $field->visibility === 'public' || $seesRestricted)
->filter(fn (ProfileField $field) => filled($values[$field->key] ?? null))
->map(fn (ProfileField $field) => [
'key' => $field->key,
'label' => $field->label,
'type' => $field->type,
'value' => $this->display($field, (string) $values[$field->key]),
])
->values()
->all();
}
/**
* Definitionen und Werte für das Bearbeitungsformular der Person selbst.
*
* @param list<string> $managed vom Identity Provider gepflegte Felder („custom.<key>“)
* @return list<array<string, mixed>>
*/
public function formFor(User $user, array $managed): array
{
$values = (array) ($user->custom_fields ?? []);
return $this->all()->map(fn (ProfileField $field) => [
'key' => $field->key,
'label' => $field->label,
'type' => $field->type,
'help' => $field->help,
'restricted' => $field->visibility === 'restricted',
'value' => $values[$field->key] ?? '',
'readonly' => ! $field->editable || in_array('custom.'.$field->key, $managed, true),
'managed' => in_array('custom.'.$field->key, $managed, true),
])->values()->all();
}
/**
* @return array<string, list<mixed>>
*/
public function rules(string $prefix = 'custom_fields'): array
{
return $this->all()->mapWithKeys(fn (ProfileField $field) => [
"{$prefix}.{$field->key}" => array_merge(['nullable'], match ($field->type) {
'email' => ['email', 'max:255'],
'url' => ['url', 'max:500'],
'date' => ['date'],
'phone' => ['string', 'max:50'],
'textarea' => ['string', 'max:5000'],
default => ['string', 'max:255'],
}),
])->all();
}
/**
* Nur die Felder übernehmen, die die Person selbst pflegen darf.
*
* @param array<string, mixed> $input
* @param list<string> $managed
*/
public function mergeUserInput(User $user, array $input, array $managed): void
{
$values = (array) ($user->custom_fields ?? []);
foreach ($this->all() as $field) {
if ($field->editable && ! in_array('custom.'.$field->key, $managed, true) && array_key_exists($field->key, $input)) {
$values[$field->key] = filled($input[$field->key]) ? (string) $input[$field->key] : null;
}
}
$user->forceFill(['custom_fields' => array_filter($values, fn ($v) => $v !== null)])->save();
}
private function display(ProfileField $field, string $value): string
{
if ($field->type === 'date') {
try {
return Carbon::parse($value)->format('d.m.Y');
} catch (Throwable) {
return $value;
}
}
return $value;
}
}