, groups: list}>, app_roles: list}>, roles: list} */ public function test(IdentityProvider $provider, string $userPrincipal): array { $client = new EntraClient($provider); $token = $client->appToken(); $user = $client->graph($token)->get('/users/'.rawurlencode($userPrincipal), ['$select' => 'id,displayName,mail,userPrincipalName,accountEnabled']); if ($user->status() === 404) { throw new IdentityException('Diese Person gibt es in Entra ID nicht.'); } $client->assertGraph($user); $id = (string) $user->json('id'); $groups = $client->graphAll($token, '/users/'.$id.'/transitiveMemberOf/microsoft.graph.group?$select=id,displayName&$top=999'); $servicePrincipal = $client->graph($token)->get("/servicePrincipals(appId='".$client->clientId()."')", ['$select' => 'id,appRoles']); $appRoles = []; if ($servicePrincipal->successful()) { $values = collect((array) $servicePrincipal->json('appRoles', []))->pluck('value', 'id'); $assignments = $client->graphAll($token, '/users/'.$id.'/appRoleAssignments?$top=999'); $groupIds = array_column($groups, 'id'); foreach ($assignments as $assignment) { if (($assignment['resourceId'] ?? null) === $servicePrincipal->json('id') && $values->has($assignment['appRoleId'] ?? '')) { $appRoles[] = $values[$assignment['appRoleId']]; } } // Über Gruppen zugewiesene App-Rollen foreach ($client->graphAll($token, '/servicePrincipals/'.$servicePrincipal->json('id').'/appRoleAssignedTo?$top=999') as $assignment) { if (($assignment['principalType'] ?? '') === 'Group' && in_array($assignment['principalId'], $groupIds, true) && $values->has($assignment['appRoleId'] ?? '')) { $appRoles[] = $values[$assignment['appRoleId']]; } } } $mappings = $provider->mappings()->with('role')->get(); $roleNames = fn (string $kind, string $external) => $mappings ->filter(fn (IdpGroupMapping $m) => $m->kind === $kind && strtolower($m->external_id) === strtolower($external)) ->map(fn (IdpGroupMapping $m) => $m->role->name)->values()->all(); $groupRows = array_map(fn (array $g) => ['id' => $g['id'], 'name' => $g['displayName'] ?? $g['id'], 'roles' => $roleNames(IdpGroupMapping::GROUP, $g['id'])], $groups); usort($groupRows, fn ($a, $b) => [empty($a['roles']), $a['name']] <=> [empty($b['roles']), $b['name']]); $appRoleRows = array_map(fn (string $v) => ['value' => $v, 'roles' => $roleNames(IdpGroupMapping::APP_ROLE, $v)], array_values(array_unique($appRoles))); $roles = collect($groupRows)->pluck('roles')->flatten()->merge(collect($appRoleRows)->pluck('roles')->flatten()); if ($default = $provider->setting('default_role_id')) { $roles->push(Role::query()->find($default)?->name.' (Standardrolle)'); } return [ 'user' => [ 'name' => $user->json('displayName'), 'email' => $user->json('mail') ?: $user->json('userPrincipalName'), 'enabled' => (bool) $user->json('accountEnabled', true), ], 'groups' => $groupRows, 'app_roles' => $appRoleRows, 'roles' => $roles->filter()->unique()->values()->all(), ]; } /** * Gruppensuche für das Mapping-Formular. * * @return array Objekt-ID => Anzeigename */ public function searchGroups(IdentityProvider $provider, string $search): array { $client = new EntraClient($provider); $response = $client->graph($client->appToken()) ->withHeaders(['ConsistencyLevel' => 'eventual']) ->get('/groups', ['$search' => '"displayName:'.str_replace('"', '', $search).'"', '$select' => 'id,displayName', '$top' => 20]); $client->assertGraph($response); return collect((array) $response->json('value', []))->mapWithKeys(fn ($g) => [$g['id'] => $g['displayName'].' ('.substr($g['id'], 0, 8).'…)'])->all(); } /** * Verzeichniserweiterungen des Mandanten (extension__) für Benutzer – * etwa aus Entra Connect synchronisierte AD-Attribute. Wird in der Konfiguration * gespeichert und im Formular als Vorschlag angeboten. * * @return list */ public function loadExtensionProperties(IdentityProvider $provider): array { $client = new EntraClient($provider); $response = $client->graph($client->appToken())->post('/directoryObjects/getAvailableExtensionProperties', (object) []); $client->assertGraph($response); $names = collect((array) $response->json('value', [])) ->filter(fn ($property) => in_array('User', (array) ($property['targetObjects'] ?? []), true)) ->pluck('name') ->filter(fn ($name) => is_string($name) && preg_match(EntraAttributes::PATH_PATTERN, $name)) ->unique()->sort()->values()->all(); $provider->update(['config' => array_merge((array) $provider->config, ['extension_properties' => $names])]); return $names; } }