Repository: Nerzal/gocloak
Pull request #143 (2020-02-10 19:47:34)

Adding GetDefaultGroups, AddDefaultGroup, RemoveDefaultGroup methods
These methods will allow to manage default realm groups



Patch:
diff --git a/README.md b/README.md
index a1ae959..79d4280 100644
--- a/README.md
+++ b/README.md
@@ -149,6 +149,9 @@ 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)
+	GetDefaultGroups(accessToken string, realm string) ([]*Group, error)
+	AddDefaultGroup(accessToken string, realm string, groupID string) error
+	RemoveDefaultGroup(accessToken string, realm string, groupID string) 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)
diff --git a/client.go b/client.go
index 4447586..d703588 100644
--- a/client.go
+++ b/client.go
@@ -789,6 +789,37 @@ func (client *gocloak) GetComponents(token string, realm string) ([]*Component,
 	return result, nil
 }
 
+// GetDefaultGroups returns a list of default groups
+func (client *gocloak) GetDefaultGroups(token string, realm string) ([]*Group, error) {
+	var result []*Group
+
+	resp, err := client.getRequestWithBearerAuth(token).
+		SetResult(&result).
+		Get(client.getAdminRealmURL(realm, "default-groups"))
+
+	if err := checkForError(resp, err); err != nil {
+		return nil, err
+	}
+
+	return result, nil
+}
+
+// AddDefaultGroup adds group to the list of default groups
+func (client *gocloak) AddDefaultGroup(token string, realm string, groupID string) error {
+	resp, err := client.getRequestWithBearerAuth(token).
+		Put(client.getAdminRealmURL(realm, "default-groups", groupID))
+
+	return checkForError(resp, err)
+}
+
+// RemoveDefaultGroup removes group from the list of default groups
+func (client *gocloak) RemoveDefaultGroup(token string, realm string, groupID string) error {
+	resp, err := client.getRequestWithBearerAuth(token).
+		Delete(client.getAdminRealmURL(realm, "default-groups", groupID))
+
+	return checkForError(resp, err)
+}
+
 func (client *gocloak) getRoleMappings(token string, realm string, path string, objectID string) (*MappingsRepresentation, error) {
 	var result MappingsRepresentation
 	resp, err := client.getRequestWithBearerAuth(token).
diff --git a/gocloak.go b/gocloak.go
index fade9e7..f7c72ed 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -107,6 +107,12 @@ type GoCloak interface {
 	GetKeyStoreConfig(accessToken string, realm string) (*KeyStoreConfig, error)
 	// GetComponents gets components of the given realm
 	GetComponents(accessToken string, realm string) ([]*Component, error)
+	// GetDefaultGroups returns a list of default groups
+	GetDefaultGroups(accessToken string, realm string) ([]*Group, error)
+	// AddDefaultGroup adds group to the list of default groups
+	AddDefaultGroup(accessToken string, realm string, groupID string) error
+	// RemoveDefaultGroup removes group from the list of default groups
+	RemoveDefaultGroup(accessToken string, realm string, groupID string) error
 	// GetGroups gets all groups of the given realm
 	GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
 	// GetGroup gets the given group


Test patch:
diff --git a/client_test.go b/client_test.go
index 406ee07..5c64b15 100644
--- a/client_test.go
+++ b/client_test.go
@@ -1308,6 +1308,52 @@ func TestGocloak_GetGroupMembers(t *testing.T) {
 	)
 }
 
+func TestGocloak_ListAddRemoveDefaultGroups(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+
+	tearDown, groupID := CreateGroup(t, client)
+	defer tearDown()
+
+	groupsBeforeAdding, err := client.GetDefaultGroups(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+	)
+	assert.NoError(t, err, "GetDefaultGroups failed")
+
+	err = client.AddDefaultGroup(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		groupID,
+	)
+	assert.NoError(t, err, "AddDefaultGroup failed")
+
+	groupsAfterAdding, err := client.GetDefaultGroups(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+	)
+	assert.NoError(t, err, "GetDefaultGroups failed")
+
+	assert.NotEqual(t, len(groupsBeforeAdding), len(groupsAfterAdding), "group should have been added")
+
+	err = client.RemoveDefaultGroup(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		groupID,
+	)
+	assert.NoError(t, err, "RemoveDefaultGroup failed")
+
+	groupsAfterRemoving, err := client.GetDefaultGroups(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+	)
+	assert.NoError(t, err, "GetDefaultGroups failed")
+
+	assert.Equal(t, len(groupsAfterRemoving), len(groupsBeforeAdding), "group should have been removed")
+}
+
 func TestGocloak_GetClientRoles(t *testing.T) {
 	t.Parallel()
 	cfg := GetConfig(t)