Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement method to get group's child groups using Keycloak 23 API #459

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading