Loheide-eu-mitarbeiterplatt.../app/Support/ProfileFields.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
4.9 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
{
/** Dürfen Mitarbeitende Abteilung und Standort selbst ändern? (Standard: nein – steuern Sichtbarkeit) */
public const SELF_EDIT_ORG = 'profile.self_edit_org';
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;
}
}