Repository: Nerzal/gocloak
Pull request #176 (2020-06-15 18:52:56)

Adding search parameters when counting users and groups
Adding params to user and group count methods to support paging while searching users and groups.



Patch:
diff --git a/README.md b/README.md
index 8f7fc09..cd59c87 100644
--- a/README.md
+++ b/README.md
@@ -151,12 +151,12 @@ type GoCloak interface {
 	RegenerateClientSecret(token string, realm string, clientID string) (*CredentialRepresentation, error)
 	GetKeyStoreConfig(accessToken string, realm string) (*KeyStoreConfig, error)
 	GetUserByID(accessToken string, realm string, userID string) (*User, error)
-	GetUserCount(accessToken string, realm string) (int, error)
+	GetUserCount(accessToken string, realm string, params GetUsersParams) (int, error)
 	GetUsers(accessToken string, realm string, params GetUsersParams) ([]*User, error)
 	GetUserGroups(accessToken string, realm string, userID string) ([]*UserGroup, error)
 	GetComponents(accessToken string, realm string) ([]*Component, error)
 	GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
-	GetGroupsCount(token string, realm string) (int, error)
+	GetGroupsCount(token string, realm string, params GetGroupsParams) (int, error)
 	GetGroup(accessToken string, realm, groupID string) (*Group, error)
 	GetDefaultGroups(accessToken string, realm string) ([]*Group, error)
 	AddDefaultGroup(accessToken string, realm string, groupID string) error
diff --git a/client.go b/client.go
index 72bd02e..fdce0f7 100644
--- a/client.go
+++ b/client.go
@@ -1018,12 +1018,17 @@ func (client *gocloak) GetGroups(token string, realm string, params GetGroupsPar
 }
 
 // GetGroupsCount gets the groups count in the realm
-func (client *gocloak) GetGroupsCount(token string, realm string) (int, error) {
+func (client *gocloak) GetGroupsCount(token string, realm string, params GetGroupsParams) (int, error) {
 	const errMessage = "could not get groups count"
 
 	var result GroupsCount
+	queryParams, err := GetQueryParams(params)
+	if err != nil {
+		return 0, errors.Wrap(err, errMessage)
+	}
 	resp, err := client.getRequestWithBearerAuth(token).
 		SetResult(&result).
+		SetQueryParams(queryParams).
 		Get(client.getAdminRealmURL(realm, "groups", "count"))
 
 	if err := checkForError(resp, err, errMessage); err != nil {
@@ -1535,12 +1540,18 @@ func (client *gocloak) GetUserByID(accessToken string, realm string, userID stri
 }
 
 // GetUserCount gets the user count in the realm
-func (client *gocloak) GetUserCount(token string, realm string) (int, error) {
+func (client *gocloak) GetUserCount(token string, realm string, params GetUsersParams) (int, error) {
 	const errMessage = "could not get user count"
 
 	var result int
+	queryParams, err := GetQueryParams(params)
+	if err != nil {
+		return 0, errors.Wrap(err, errMessage)
+	}
+
 	resp, err := client.getRequestWithBearerAuth(token).
 		SetResult(&result).
+		SetQueryParams(queryParams).
 		Get(client.getAdminRealmURL(realm, "users", "count"))
 
 	if err := checkForError(resp, err, errMessage); err != nil {
diff --git a/gocloak.go b/gocloak.go
index 986980c..c41dbf7 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -116,7 +116,7 @@ type GoCloak interface {
 	// GetGroups gets all groups of the given realm
 	GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
 	// GetGroupsCount gets groups count of the given realm
-	GetGroupsCount(token string, realm string) (int, error)
+	GetGroupsCount(token string, realm string, params GetGroupsParams) (int, 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
@@ -233,7 +233,7 @@ type GoCloak interface {
 	// GetUserByID gets the user with the given id
 	GetUserByID(accessToken string, realm string, userID string) (*User, error)
 	// GetUser count returns the userCount of the given realm
-	GetUserCount(accessToken string, realm string) (int, error)
+	GetUserCount(accessToken string, realm string, params GetUsersParams) (int, error)
 	// GetUsers gets all users of the given realm
 	GetUsers(accessToken string, realm string, params GetUsersParams) ([]*User, error)
 	// GetUserGroups gets the groups of the given user


Test patch:
diff --git a/client_test.go b/client_test.go
index 27198e6..35e77d7 100644
--- a/client_test.go
+++ b/client_test.go
@@ -1927,7 +1927,9 @@ func TestGocloak_GetUserCount(t *testing.T) {
 
 	count, err := client.GetUserCount(
 		token.AccessToken,
-		cfg.GoCloak.Realm)
+		cfg.GoCloak.Realm,
+		GetUsersParams{})
+
 	t.Logf("Users in Realm: %d", count)
 	require.NoError(t, err, "GetUserCount failed")
 }
@@ -1940,7 +1942,8 @@ func TestGocloak_GetGroupsCount(t *testing.T) {
 
 	count, err := client.GetGroupsCount(
 		token.AccessToken,
-		cfg.GoCloak.Realm)
+		cfg.GoCloak.Realm,
+		GetGroupsParams{})
 	t.Logf("Groups in Realm: %d", count)
 	require.NoError(t, err, "GetGroupsCount failed")
 }