diff --git a/src/Service/KeycloakAdminService.php b/src/Service/KeycloakAdminService.php index f4ea1b9..568072e 100644 --- a/src/Service/KeycloakAdminService.php +++ b/src/Service/KeycloakAdminService.php @@ -8,6 +8,7 @@ use Symfony\Contracts\HttpClient\HttpClientInterface; class KeycloakAdminService { private const PATH_USERS = '/users'; + private const PATH_GROUPS = '/groups'; private const AUTH_BEARER = 'Bearer '; /** Groupes requis pour le CRM SITECONSEIL */ @@ -53,10 +54,8 @@ class KeycloakAdminService $created = []; foreach (self::REQUIRED_GROUPS as $groupName) { - if (!\in_array($groupName, $existingNames, true)) { - if ($this->createGroup($groupName)) { - $created[] = $groupName; - } + if (!\in_array($groupName, $existingNames, true) && $this->createGroup($groupName)) { + $created[] = $groupName; } } @@ -69,7 +68,7 @@ class KeycloakAdminService public function createGroup(string $groupName): bool { $token = $this->getToken(); - $response = $this->httpClient->request('POST', $this->getAdminUrl('/groups'), [ + $response = $this->httpClient->request('POST', $this->getAdminUrl(self::PATH_GROUPS), [ 'headers' => ['Authorization' => self::AUTH_BEARER.$token], 'json' => ['name' => $groupName], ]); @@ -267,7 +266,7 @@ class KeycloakAdminService public function listGroups(): array { $token = $this->getToken(); - $response = $this->httpClient->request('GET', $this->getAdminUrl('/groups'), [ + $response = $this->httpClient->request('GET', $this->getAdminUrl(self::PATH_GROUPS), [ 'headers' => ['Authorization' => self::AUTH_BEARER.$token], ]); @@ -285,7 +284,7 @@ class KeycloakAdminService private function getGroupIdByName(string $groupName): ?string { $token = $this->getToken(); - $response = $this->httpClient->request('GET', $this->getAdminUrl('/groups'), [ + $response = $this->httpClient->request('GET', $this->getAdminUrl(self::PATH_GROUPS), [ 'headers' => ['Authorization' => self::AUTH_BEARER.$token], 'query' => ['search' => $groupName, 'exact' => 'true'], ]);