Loheide-eu-mitarbeiterplatt.../app/Identity/IdentityLogin.php
Friederich Loheide 4fcc41ed30
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
Phase 6 (Teil 2): OpenID Connect, LDAP/AD und SAML 2
- 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>
2026-09-26 12:38:32 +00:00

86 lines
3.3 KiB
PHP

<?php
namespace App\Identity;
use App\Models\IdentityProvider;
use App\Models\User;
use App\Models\UserIdentity;
use Illuminate\Support\Facades\Cache;
/**
* Gemeinsamer Abschluss jeder Anmeldung über einen Anbieter: Konto finden oder
* anlegen, Profil, Rollen und Foto übernehmen.
*/
class IdentityLogin
{
public const LAST_SEEN_TTL = 60 * 60 * 24 * 14;
public function __construct(private readonly IdentitySync $sync) {}
public function finish(IdentityProvider $provider, ExternalUser $external): User
{
self::remember($provider, $external);
$user = $this->sync->resolveUser($provider, $external->subject, $external->profile, (bool) $provider->setting('auto_provision', true));
if (! $user) {
throw new IdentityException('Für Ihr Konto bei „'.$provider->name.'“ gibt es kein Konto in diesem Portal. Bitte wenden Sie sich an die Administration.');
}
if (! $user->is_active) {
throw new IdentityException('Ihr Konto ist deaktiviert. Bitte wenden Sie sich an die Administration.');
}
$identity = UserIdentity::query()->where('identity_provider_id', $provider->id)->where('subject', $external->subject)->firstOrFail();
$identity->update(['last_login_at' => now(), 'email' => $external->profile['email'] ?? $identity->email]);
$this->sync->syncProfile($provider, $user, $external->profile);
$this->sync->syncRoles($provider, $user, $external->groups, $external->appRoles);
if ($external->photo !== false && $provider->setting('sync_photo', false)) {
$this->sync->syncPhoto($identity->fresh('user'), $external->photo);
}
return $user->fresh();
}
/**
* Zuletzt empfangene Angaben für die Administration (Hilfe bei der Zuordnung).
*/
public static function remember(IdentityProvider $provider, ExternalUser $external): void
{
$attributes = [];
foreach ($external->attributes as $key => $value) {
if (count($attributes) >= 80) {
break;
}
$text = is_array($value) && ! array_is_list($value)
? (string) json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
: (is_array($value)
? implode(', ', array_slice(array_map(fn ($v) => is_scalar($v) ? (string) $v : json_encode($v), $value), 0, 20))
: (is_scalar($value) || $value === null ? (string) $value : json_encode($value)));
// Binärwerte (Fotos, GUIDs) nicht speichern.
$attributes[(string) $key] = mb_check_encoding($text, 'UTF-8') ? mb_strimwidth($text, 0, 300, '…') : '(Binärdaten)';
}
Cache::put('identity.last_seen.'.$provider->id, [
'at' => now()->timestamp,
'subject' => $external->subject,
'attributes' => $attributes,
'groups' => array_slice($external->groups, 0, 100),
], self::LAST_SEEN_TTL);
}
/**
* @return array{at: int, subject: string, attributes: array<string, string>, groups: list<string>}|null
*/
public static function lastSeen(IdentityProvider $provider): ?array
{
$value = Cache::get('identity.last_seen.'.$provider->id);
return is_array($value) ? $value : null;
}
}