Skip to content

Commit

Permalink
change name and return value for realm roles scope
Browse files Browse the repository at this point in the history
close #18 #28
  • Loading branch information
mainick committed Nov 23, 2024
1 parent 2e3ff66 commit c20bfff
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/Token/KeycloakResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,35 @@ public function getLastName(): ?string
*
* @return array<string>
*/
private function getRealRoles(): ?array
private function getRealmRoles(): array
{
return $this->response['realm_access']['roles'] ?? null;
return $this->response['realm_access']['roles'] ?? [];
}

/**
* Get client roles.
*
* @return array<string>|null
* @param string|null $client_id Optional client ID to filter roles
* @return array<string>
*/
private function getClientRoles(?string $client_id = null): ?array
private function getClientRoles(?string $client_id = null): array
{
$roles = [];

if (isset($this->response['resource_access'])) {
foreach ($this->response['resource_access'] as $client_rif) {
if (isset($client_rif['roles'])) {
$roles = [...$roles, ...$client_rif['roles']];
}
}
}
$resource_access = $this->response['resource_access'] ?? [];

if ($client_id && isset($this->response['resource_access'][$client_id]['roles'])) {
$roles = $this->response['resource_access'][$client_id]['roles'] ?? [];
// If client_id is provided, return only roles for that client
if ($client_id !== null) {
return $resource_access[$client_id]['roles'] ?? [];
}

return $roles;
// Otherwise, collect all roles from all clients
return array_reduce(
$resource_access,
static fn(array $carry, array $client): array => [
...$carry,
...($client['roles'] ?? [])
],
[]
);
}

/**
Expand All @@ -120,9 +122,7 @@ private function getClientRoles(?string $client_id = null): ?array
*/
public function getRoles(?string $client_id = null): array
{
$roles = $this->getRealRoles();

return [...$roles, ...$this->getClientRoles($client_id)];
return [...$this->getRealmRoles(), ...$this->getClientRoles($client_id)];
}

/**
Expand Down

0 comments on commit c20bfff

Please sign in to comment.