Loheide-eu-mitarbeiterplatt.../app/Identity/Entra/EntraProfile.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

79 lines
2.8 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\Identity\Entra;
use App\Models\IdentityProvider;
/**
* Normalisiert Entra-/Graph-Benutzerdaten auf die Profilfelder des Portals –
* gemäß der Attribut-Zuordnung des Anbieters.
*
* Ergebnis: display_name, Kernfelder (first_name, email, …), freie Felder unter
* „custom“ und – falls abgefragt – manager_subject.
*/
final class EntraProfile
{
/**
* @param array<string, mixed> $graphUser Antwort von /me, /users/{id} oder /users/delta
* @param bool $partial Delta-Antwort: nur Felder liefern, deren Attribut enthalten ist
* @return array<string, mixed>
*/
public static function fromGraph(IdentityProvider $provider, array $graphUser, ?string $managerSubject = null, bool $withManager = false, bool $partial = false): array
{
$present = fn (string $path) => ! $partial || array_key_exists(EntraAttributes::topLevel($path), $graphUser);
$profile = [];
if ($present('displayName')) {
$profile['display_name'] = EntraAttributes::value($graphUser, 'displayName');
}
// Grundwerte für Kontoanlage und Verknüpfung; die Zuordnung überschreibt sie.
foreach (['first_name' => 'givenName', 'last_name' => 'surname', 'email' => 'mail'] as $field => $path) {
if ($present($path)) {
$profile[$field] = EntraAttributes::value($graphUser, $path);
}
}
foreach ($provider->attributeMap() as $target => $path) {
if (! $present($path)) {
continue;
}
$value = EntraAttributes::value($graphUser, $path);
if (str_starts_with($target, 'custom.')) {
$profile['custom'][substr($target, 7)] = $value;
} else {
$profile[$target] = $value;
}
}
// Gastkonten haben oft keine „mail“; der UPN ist dann eine #EXT#-Adresse.
if (array_key_exists('email', $profile) && $profile['email'] === null && ! $partial) {
$upn = (string) ($graphUser['userPrincipalName'] ?? '');
$profile['email'] = $upn !== '' && ! str_contains($upn, '#EXT#') ? $upn : null;
}
if ($withManager) {
$profile['manager_subject'] = $managerSubject;
}
return $profile;
}
/**
* Rückfall, wenn Graph nicht erreichbar ist: Angaben aus dem ID-Token.
*
* @param array<string, mixed> $claims
* @return array<string, mixed>
*/
public static function fromClaims(array $claims): array
{
return [
'display_name' => $claims['name'] ?? null,
'first_name' => $claims['given_name'] ?? null,
'last_name' => $claims['family_name'] ?? null,
'email' => $claims['email'] ?? $claims['preferred_username'] ?? null,
];
}
}