From 66ee6dff2670990b6999e27a235ecbe1aa329122 Mon Sep 17 00:00:00 2001 From: Tri Duong Date: Wed, 24 Jan 2024 18:27:16 -0500 Subject: [PATCH] Implement method to get group's child groups using Keycloak 23 API --- README.md | 1 + client.go | 17 +++++++++++++++++ client_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/README.md b/README.md index 6f4d7c09..6f993729 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,7 @@ type GoCloak interface { GetGroups(ctx context.Context, accessToken, realm string, params GetGroupsParams) ([]*Group, error) GetGroupsCount(ctx context.Context, token, realm string, params GetGroupsParams) (int, error) GetGroup(ctx context.Context, accessToken, realm, groupID string) (*Group, error) + GetChildGroups(ctx context.Context, token, realm, groupID string) ([]*Group, error) GetDefaultGroups(ctx context.Context, accessToken, realm string) ([]*Group, error) AddDefaultGroup(ctx context.Context, accessToken, realm, groupID string) error RemoveDefaultGroup(ctx context.Context, accessToken, realm, groupID string) error diff --git a/client.go b/client.go index 96790969..80f40d50 100644 --- a/client.go +++ b/client.go @@ -1740,6 +1740,23 @@ func (g *GoCloak) GetGroups(ctx context.Context, token, realm string, params Get return result, nil } +// GetChildGroups get child groups of group with id in realm +func (g *GoCloak) GetChildGroups(ctx context.Context, token, realm, groupID string) ([]*Group, error) { + const errMessage = "could not get child groups" + + var result []*Group + + resp, err := g.GetRequestWithBearerAuth(ctx, token). + SetResult(&result). + Get(g.getAdminRealmURL(realm, "groups", groupID, "children")) + + if err := checkForError(resp, err, errMessage); err != nil { + return nil, err + } + + return result, nil +} + // GetGroupManagementPermissions returns whether group Authorization permissions have been initialized or not and a reference // to the managed permissions func (g *GoCloak) GetGroupManagementPermissions(ctx context.Context, token, realm string, idOfGroup string) (*ManagementPermissionRepresentation, error) { diff --git a/client_test.go b/client_test.go index 0cda97c3..473d8add 100644 --- a/client_test.go +++ b/client_test.go @@ -2458,6 +2458,39 @@ func Test_GetGroupMembers(t *testing.T) { require.Len(t, users, 1) } +func Test_GetChildGroups(t *testing.T) { + t.Parallel() + cfg := GetConfig(t) + client := NewClientWithDebug(t) + token := GetAdminToken(t, client) + + tearDown, parentGroupID := CreateGroup(t, client) + defer tearDown() + + childGroupIDs := make([]string, 0) + for i := 0; i < 2; i++ { + id, err := client.CreateChildGroup( + context.Background(), + token.AccessToken, + cfg.GoCloak.Realm, + parentGroupID, + gocloak.Group{ + Name: GetRandomNameP("GroupName"), + }, + ) + require.NoError(t, err, "CreateChildGroup failed") + childGroupIDs = append(childGroupIDs, id) + } + + res, err := client.GetChildGroups( + context.Background(), + token.AccessToken, + cfg.GoCloak.Realm, + parentGroupID) + require.NoError(t, err, "GetChildGroups failed") + require.Len(t, res, len(childGroupIDs)) +} + func Test_ListAddRemoveDefaultGroups(t *testing.T) { t.Parallel() cfg := GetConfig(t)