Repository: Nerzal/gocloak
Pull request #109 (2019-12-07 21:52:11)

Add full group representation
I'd like to be able to get all of the fields of a group. The naming of `UserAttributeContains` seems a bit off now because both user and groups attributes are the same. However, changing it would cause an API change.



Patch:
diff --git a/models.go b/models.go
index 00c5a67..b4aff9d 100644
--- a/models.go
+++ b/models.go
@@ -218,10 +218,14 @@ type ExecuteActionsEmail struct {
 
 // Group is a Group
 type Group struct {
-	ID        string        `json:"id,omitempty"`
-	Name      string        `json:"name,omitempty"`
-	Path      string        `json:"path,omitempty"`
-	SubGroups []interface{} `json:"subGroups,omitempty"`
+	ID          string              `json:"id,omitempty"`
+	Name        string              `json:"name,omitempty"`
+	Path        string              `json:"path,omitempty"`
+	SubGroups   []*Group            `json:"subGroups,omitempty"`
+	Attributes  map[string][]string `json:"attributes,emitempty"`
+	Access      map[string]bool     `json:"access,omitempty"`
+	ClientRoles map[string][]string `json:"clientRoles,omitempty"`
+	RealmRoles  []string            `json:"realmRoles,omitempty"`
 }
 
 // GetGroupsParams represents the optional parameters for getting groups
@@ -229,6 +233,7 @@ type GetGroupsParams struct {
 	First  int    `json:"first,string,omitempty"`
 	Max    int    `json:"max,string,omitempty"`
 	Search string `json:"search,omitempty"`
+	Full   bool   `json:"full,string,omitempty"`
 }
 
 // Role is a role


Test patch:
diff --git a/client_test.go b/client_test.go
index 3b95e8f..b036df2 100644
--- a/client_test.go
+++ b/client_test.go
@@ -208,6 +208,10 @@ func CreateGroup(t *testing.T, client GoCloak) (func(), string) {
 	token := GetAdminToken(t, client)
 	group := Group{
 		Name: GetRandomName("GroupName"),
+		Attributes: map[string][]string{
+			"foo": {"bar", "alice", "bob", "roflcopter"},
+			"bar": {"baz"},
+		},
 	}
 	err := client.CreateGroup(
 		token.AccessToken,
@@ -1115,6 +1119,54 @@ func TestGocloak_GetGroups(t *testing.T) {
 	FailIfErr(t, err, "GetGroups failed")
 }
 
+func TestGocloak_GetGroupsFull(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+
+	tearDown, groupID := CreateGroup(t, client)
+	defer tearDown()
+
+	groups, err := client.GetGroups(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		GetGroupsParams{
+			Full: true,
+		})
+	assert.NoError(t, err, "GetGroups failed")
+
+	for _, group := range groups {
+		if group.ID == groupID {
+			ok := client.UserAttributeContains(group.Attributes, "foo", "alice")
+			assert.True(t, ok, "UserAttributeContains")
+			return
+		}
+	}
+
+	assert.Fail(t, "GetGroupsFull failed")
+}
+
+func TestGocloak_GetGroupFull(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+
+	tearDown, groupID := CreateGroup(t, client)
+	defer tearDown()
+
+	createdGroup, err := client.GetGroup(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		groupID,
+	)
+	assert.NoError(t, err, "GetGroup failed")
+
+	ok := client.UserAttributeContains(createdGroup.Attributes, "foo", "alice")
+	assert.True(t, ok, "UserAttributeContains")
+}
+
 func TestGocloak_GetGroupMembers(t *testing.T) {
 	t.Parallel()
 	cfg := GetConfig(t)