Repository: Nerzal/gocloak
Pull request #108 (2019-12-05 19:25:46)

adding GetGroupMembers to goclock client
Thanks for making this great package it really helps me.

I'm hoping you'll add these method to list a group's users.

Thanks!



Patch:
diff --git a/README.md b/README.md
index 5c83398..a8ceda2 100644
--- a/README.md
+++ b/README.md
@@ -159,6 +159,7 @@ type GoCloak interface {
 	GetComponents(accessToken string, realm string) ([]*Component, error)
 	GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
 	GetGroup(accessToken string, realm, groupID string) (*Group, error)
+	GetGroupMembers(accessToken string, realm, groupID string, params GetGroupsParams) ([]*User, error)
 	GetRoleMappingByGroupID(accessToken string, realm string, groupID string) (*MappingsRepresentation, error)
 	GetRoleMappingByUserID(accessToken string, realm string, userID string) (*MappingsRepresentation, error)
 	GetClientRoles(accessToken string, realm string, clientID string) ([]*Role, error)
diff --git a/client.go b/client.go
index d1a0598..a7f31c7 100644
--- a/client.go
+++ b/client.go
@@ -7,10 +7,9 @@ import (
 	"strings"
 	"time"
 
+	"github.com/Nerzal/gocloak/v3/pkg/jwx"
 	"github.com/dgrijalva/jwt-go"
 	"github.com/go-resty/resty/v2"
-
-	"github.com/Nerzal/gocloak/v3/pkg/jwx"
 )
 
 type gocloak struct {
@@ -799,6 +798,26 @@ func (client *gocloak) GetGroups(token string, realm string, params GetGroupsPar
 	return result, nil
 }
 
+// GetGroupMembers get a list of users of group with id in realm
+func (client *gocloak) GetGroupMembers(token string, realm string, groupID string, params GetGroupsParams) ([]*User, error) {
+	var result []*User
+	queryParams, err := GetQueryParams(params)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := client.getRequestWithBearerAuth(token).
+		SetResult(&result).
+		SetQueryParams(queryParams).
+		Get(client.getAdminRealmURL(realm, "groups", groupID, "members"))
+
+	if err := checkForError(resp, err); err != nil {
+		return nil, err
+	}
+
+	return result, nil
+}
+
 // GetClientRoles get all roles for the given client in realm
 func (client *gocloak) GetClientRoles(token string, realm string, clientID string) ([]*Role, error) {
 	var result []*Role
diff --git a/gocloak.go b/gocloak.go
index b671754..6f166d5 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -109,6 +109,8 @@ type GoCloak interface {
 	GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
 	// GetGroup gets the given group
 	GetGroup(accessToken string, realm, groupID string) (*Group, error)
+	// GetGroupMembers get a list of users of group with id in realm
+	GetGroupMembers(accessToken string, realm, groupID string, params GetGroupsParams) ([]*User, error)
 	// GetRoleMappingByGroupID gets the rolemapping for the given group id
 	GetRoleMappingByGroupID(accessToken string, realm string, groupID string) (*MappingsRepresentation, error)
 	// GetRoleMappingByUserID gets the rolemapping for the given user id


Test patch:
diff --git a/client_test.go b/client_test.go
index aebcf90..3b95e8f 100644
--- a/client_test.go
+++ b/client_test.go
@@ -1115,6 +1115,40 @@ func TestGocloak_GetGroups(t *testing.T) {
 	FailIfErr(t, err, "GetGroups failed")
 }
 
+func TestGocloak_GetGroupMembers(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+	tearDownUser, userID := CreateUser(t, client)
+	defer tearDownUser()
+
+	tearDownGroup, groupID := CreateGroup(t, client)
+	defer tearDownGroup()
+
+	err := client.AddUserToGroup(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		userID,
+		groupID,
+	)
+	assert.NoError(t, err, "AddUserToGroup failed")
+
+	users, err := client.GetGroupMembers(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		groupID,
+		GetGroupsParams{},
+	)
+	assert.NoError(t, err, "AddUserToGroup failed")
+
+	assert.Equal(
+		t,
+		1,
+		len(users),
+	)
+}
+
 func TestGocloak_GetClientRoles(t *testing.T) {
 	t.Parallel()
 	cfg := GetConfig(t)