Skip to content

Commit

Permalink
Grant API-mapped team members admin privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
melegiul committed Nov 15, 2024
1 parent 5aea0bf commit fca64b0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ GROUP_API_URI=http://localhost
GROUP_API_KEY=CHANGEME
GROUP_API_ROLES=CHANGEME,CHANGEME,...
GROUP_API_USER_ID=CHANGEME
GROUP_API_GRANT_ADMIN=false
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ parameters:
superAdminRole: '%env(superAdminRole)%'
group_api_user_id: '%env(default::GROUP_API_USER_ID)%'
group_api_roles: '%env(default::csv:GROUP_API_ROLES)%'
group_api_grant_admin: '%env(bool:default::GROUP_API_GRANT_ADMIN)%'
services:
# default configuration for services in *this* file
_defaults:
Expand Down Expand Up @@ -64,3 +65,4 @@ services:
arguments:
$groupApiUserId: '%group_api_user_id%'
$groupApiRoles: '%group_api_roles%'
$groupApiGrantAdmin: '%group_api_grant_admin%'
9 changes: 9 additions & 0 deletions src/Security/KeycloakAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class KeycloakAuthenticator extends OAuth2Authenticator implements Authenticatio
public function __construct(
private readonly ?string $groupApiUserId,
private readonly ?array $groupApiRoles,
private readonly bool $groupApiGrantAdmin,
private readonly LoggerInterface $logger,
private readonly ParameterBagInterface $parameterBag,
private readonly TokenStorageInterface $tokenStorage,
Expand Down Expand Up @@ -204,6 +205,10 @@ private function persistUser(User $user, ResourceOwnerInterface $keycloakUser):
$teams = $this->syncApiGroups($keycloakUser);
foreach ($teams as $team) {
$user->addTeam($team);
if ($this->groupApiGrantAdmin) {
$team->addAdmin($user);
$this->em->persist($team);
}
}
break;
}
Expand All @@ -214,6 +219,10 @@ private function persistUser(User $user, ResourceOwnerInterface $keycloakUser):
return $user;
}

/**
* @param ResourceOwnerInterface $keycloakUser
* @return Collection<Team>
*/
private function syncApiGroups(ResourceOwnerInterface $keycloakUser): Collection {
try {
$userId = $keycloakUser->toArray()[$this->groupApiUserId];
Expand Down

0 comments on commit fca64b0

Please sign in to comment.