Loheide-eu-mitarbeiterplatt.../app/Models/IdentityProvider.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

141 lines
4.3 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\Models;
use App\Identity\Entra\EntraAttributes;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
/**
* Ein angebundener Identity Provider (zunächst Microsoft Entra ID).
*
* config (verschlüsselt): tenant_id, client_id, client_secret, button_label,
* auto_provision, link_by_email, allowed_domains, default_role_id,
* attribute_map (Liste aus target + attribute), sync_manager, sync_photo,
* extension_properties, directory_sync, directory_create_users
* (älter: synced_fields – wird noch gelesen, siehe attributeMap())
*/
#[Fillable(['type', 'name', 'slug', 'enabled', 'config'])]
class IdentityProvider extends Model
{
public const ENTRA = 'entra';
/** Profilfelder, die aus dem Identity Provider übernommen werden können. */
/** Kernfelder des Profils, die aus einem Attribut befüllt werden können. */
public const PROFILE_TARGETS = [
'first_name' => 'Vorname',
'last_name' => 'Nachname',
'email' => 'E-Mail-Adresse',
'job_title' => 'Funktion',
'department' => 'Abteilung',
'location' => 'Standort',
'phone' => 'Telefon',
'mobile' => 'Mobil',
];
protected static function booted(): void
{
static::saving(function (IdentityProvider $provider) {
$provider->slug = $provider->slug ?: Str::slug($provider->name) ?: $provider->type;
});
}
protected function casts(): array
{
return [
'enabled' => 'boolean',
'config' => 'encrypted:array',
];
}
public function setting(string $key, mixed $default = null): mixed
{
return data_get($this->config, $key, $default);
}
/**
* Zuordnung Profilfeld => Graph-Attribut. Ziele sind Kernfelder („job_title“)
* oder freie Profilfelder („custom.personalnummer“). Ohne gespeicherte Zuordnung
* gilt die Standardzuordnung für die früher gewählten Felder.
*
* @return array<string, string>
*/
public function attributeMap(): array
{
$rows = $this->setting('attribute_map');
if (! is_array($rows)) {
$legacy = (array) $this->setting('synced_fields', array_keys(EntraAttributes::DEFAULT_MAP));
return array_intersect_key(EntraAttributes::DEFAULT_MAP, array_flip($legacy));
}
$map = [];
foreach ($rows as $row) {
$target = (string) ($row['target'] ?? '');
$attribute = trim((string) ($row['attribute'] ?? ''));
if ($attribute !== '' && preg_match(EntraAttributes::PATH_PATTERN, $attribute)
&& (array_key_exists($target, self::PROFILE_TARGETS) || str_starts_with($target, 'custom.'))) {
$map[$target] = $attribute;
}
}
return $map;
}
public function syncsManager(): bool
{
return (bool) $this->setting('sync_manager', in_array('manager', (array) $this->setting('synced_fields', ['manager']), true));
}
/**
* Felder, die dieser Anbieter pflegt (im Portal schreibgeschützt).
*
* @return list<string>
*/
public function syncedFields(): array
{
return [...array_keys($this->attributeMap()), ...($this->syncsManager() ? ['manager'] : [])];
}
public function buttonLabel(): string
{
return $this->setting('button_label') ?: 'Mit Microsoft anmelden';
}
public function isEmailDomainAllowed(?string $email): bool
{
$domains = array_filter(array_map(fn ($d) => strtolower(trim((string) $d)), (array) $this->setting('allowed_domains', [])));
if ($domains === []) {
return true;
}
return $email !== null && in_array(strtolower(substr(strrchr($email, '@') ?: '', 1)), $domains, true);
}
public function sourcePrefix(): string
{
return $this->id.':';
}
/**
* @return HasMany<IdpGroupMapping, $this>
*/
public function mappings(): HasMany
{
return $this->hasMany(IdpGroupMapping::class);
}
/**
* @return HasMany<UserIdentity, $this>
*/
public function identities(): HasMany
{
return $this->hasMany(UserIdentity::class);
}
}