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, groups: list}|null */ public static function lastSeen(IdentityProvider $provider): ?array { $value = Cache::get('identity.last_seen.'.$provider->id); return is_array($value) ? $value : null; } }