All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m41s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m48s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m15s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m17s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m48s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m41s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m59s
- Gemeinsamer Treiber-Unterbau (App\Identity\IdentityDriver, IdentityDrivers, IdentityLogin, AttributeProfile); Entra ID nutzt ihn ebenfalls - Plugins oidc, ldap und saml mit Profil- und Gruppen-Zuordnung - Admin: Verfahren wählen, Formular je Verfahren, zuletzt empfangene Angaben - onelogin/php-saml als neue Abhängigkeit Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
181 lines
5.9 KiB
PHP
181 lines
5.9 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Identity\Entra\EntraAttributes;
|
||
use App\Identity\IdentityDriver;
|
||
use App\Identity\IdentityDrivers;
|
||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
use Illuminate\Database\Eloquent\Collection;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||
use Illuminate\Support\Str;
|
||
|
||
/**
|
||
* Ein angebundener Identity Provider. Das Verfahren (type) liefert ein Treiber aus
|
||
* App\Identity\IdentityDrivers – Entra ID im Kern, OpenID Connect, LDAP und SAML als Plugins.
|
||
*
|
||
* config (verschlüsselt), gemeinsam: button_label, auto_provision, link_by_email,
|
||
* allowed_domains, default_role_id, attribute_map, sync_manager, sync_photo;
|
||
* übrige Schlüssel je Verfahren. Entra: 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 driver(): ?IdentityDriver
|
||
{
|
||
return app(IdentityDrivers::class)->get((string) $this->type);
|
||
}
|
||
|
||
/**
|
||
* Aktive Anbieter, deren Verfahren verfügbar ist – für die Anmeldeseite.
|
||
*
|
||
* @return Collection<int, IdentityProvider>
|
||
*/
|
||
public static function forLogin(): Collection
|
||
{
|
||
return static::query()->where('enabled', true)->orderBy('name')->get()
|
||
->filter(fn (IdentityProvider $p) => $p->driver()?->unavailableReason() === null && $p->driver() !== null)
|
||
->values();
|
||
}
|
||
|
||
/**
|
||
* @return array{slug: string, label: string, type: string}
|
||
*/
|
||
public function toLoginButton(): array
|
||
{
|
||
return ['slug' => $this->slug, 'label' => $this->buttonLabel(), 'type' => $this->type];
|
||
}
|
||
|
||
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) && $this->type !== self::ENTRA) {
|
||
return $this->driver()?->defaultAttributeMap() ?? [];
|
||
}
|
||
|
||
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 = [];
|
||
$pattern = $this->driver()?->attributePattern() ?? EntraAttributes::PATH_PATTERN;
|
||
|
||
foreach ($rows as $row) {
|
||
$target = (string) ($row['target'] ?? '');
|
||
$attribute = trim((string) ($row['attribute'] ?? ''));
|
||
|
||
if ($attribute !== '' && preg_match($pattern, $attribute)
|
||
&& (array_key_exists($target, self::PROFILE_TARGETS) || str_starts_with($target, 'custom.'))) {
|
||
$map[$target] = $attribute;
|
||
}
|
||
}
|
||
|
||
return $map;
|
||
}
|
||
|
||
public function syncsManager(): bool
|
||
{
|
||
if ($this->type !== self::ENTRA) {
|
||
return (bool) $this->setting('sync_manager', false) && (bool) $this->driver()?->supportsManager();
|
||
}
|
||
|
||
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') ?: ($this->driver()?->defaultButtonLabel($this) ?? 'Mit '.$this->name.' 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);
|
||
}
|
||
}
|