diff --git a/client.go b/client.go index 4b10f260..1a4ff8ea 100644 --- a/client.go +++ b/client.go @@ -1390,6 +1390,22 @@ func (client *gocloak) GetGroups(ctx context.Context, token, realm string, param return result, nil } +// GetGroupsByRole gets groups assigned with a specific role of a realm +func (client *gocloak) GetGroupsByRole(ctx context.Context, token, realm string, roleName string) ([]*Group, error) { + const errMessage = "could not get groups" + + var result []*Group + resp, err := client.getRequestWithBearerAuth(ctx, token). + SetResult(&result). + Get(fmt.Sprintf("%s/%s/%s", client.getAdminRealmURL(realm, "roles"), roleName, "groups")) + + if err := checkForError(resp, err, errMessage); err != nil { + return nil, err + } + + return result, nil +} + // GetGroupsCount gets the groups count in the realm func (client *gocloak) GetGroupsCount(ctx context.Context, token, realm string, params GetGroupsParams) (int, error) { const errMessage = "could not get groups count" diff --git a/gocloak.go b/gocloak.go index 2ad9f207..ddc3b25a 100644 --- a/gocloak.go +++ b/gocloak.go @@ -163,6 +163,8 @@ type GoCloak interface { RemoveDefaultGroup(ctx context.Context, accessToken, realm, groupID string) error // GetGroups gets all groups of the given realm GetGroups(ctx context.Context, accessToken, realm string, params GetGroupsParams) ([]*Group, error) + // GetGroupsByRole gets groups with specified roles assigned of given realm + GetGroupsByRole(ctx context.Context, accessToken, realm string, roleName string) ([]*Group, error) // GetGroupsCount gets groups count of the given realm GetGroupsCount(ctx context.Context, token, realm string, params GetGroupsParams) (int, error) // GetGroup gets the given group