Repository: Nerzal/gocloak
Pull request #126 (2019-12-23 23:35:17)

Group/Role methods
depends-on https://github.com/Nerzal/gocloak/pull/125


Patch:
diff --git a/README.md b/README.md
index 17925f7..e1aa0e0 100644
--- a/README.md
+++ b/README.md
@@ -173,6 +173,12 @@ type GoCloak interface {
 	AddRealmRoleComposite(token string, realm string, roleName string, roles []Role) error
 	DeleteRealmRoleComposite(token string, realm string, roleName string, roles []Role) error
 
+
+	// *** Client Roles ***
+
+	AddClientRoleToGroup(token string, realm string, clientID string, groupID string, roles []Role) error
+	DeleteClientRoleFromGroup(token string, realm string, clientID string, groupID string, roles []Role) error
+
 	// *** Realm ***
 
 	GetRealm(token string, realm string) (*RealmRepresentation, error)
diff --git a/client.go b/client.go
index 0619dc3..bbd6c1b 100644
--- a/client.go
+++ b/client.go
@@ -1270,6 +1270,15 @@ func (client *gocloak) AddClientRoleToUser(token string, realm string, clientID
 	return checkForError(resp, err)
 }
 
+// AddClientRoleToGroup adds a client role to the group
+func (client *gocloak) AddClientRoleToGroup(token string, realm string, clientID string, groupID string, roles []Role) error {
+	resp, err := client.getRequestWithBearerAuth(token).
+		SetBody(roles).
+		Post(client.getAdminRealmURL(realm, "groups", groupID, "role-mappings", "clients", clientID))
+
+	return checkForError(resp, err)
+}
+
 // DeleteClientRoleFromUser adds client-level role mappings
 func (client *gocloak) DeleteClientRoleFromUser(token string, realm string, clientID string, userID string, roles []Role) error {
 	resp, err := client.getRequestWithBearerAuth(token).
@@ -1279,6 +1288,15 @@ func (client *gocloak) DeleteClientRoleFromUser(token string, realm string, clie
 	return checkForError(resp, err)
 }
 
+// DeleteClientRoleFromGroup removes a client role from from the group
+func (client *gocloak) DeleteClientRoleFromGroup(token string, realm string, clientID string, groupID string, roles []Role) error {
+	resp, err := client.getRequestWithBearerAuth(token).
+		SetBody(roles).
+		Delete(client.getAdminRealmURL(realm, "groups", groupID, "role-mappings", "clients", clientID))
+
+	return checkForError(resp, err)
+}
+
 // AddClientRoleComposite adds roles as composite
 func (client *gocloak) AddClientRoleComposite(token string, realm string, roleID string, roles []Role) error {
 	resp, err := client.getRequestWithBearerAuth(token).
diff --git a/gocloak.go b/gocloak.go
index 398f222..98cd698 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -160,12 +160,16 @@ type GoCloak interface {
 
 	// AddClientRoleToUser adds a client role to the user
 	AddClientRoleToUser(token string, realm string, clientID string, userID string, roles []Role) error
+	// AddClientRoleToGroup adds a client role to the group
+	AddClientRoleToGroup(token string, realm string, clientID string, groupID string, roles []Role) error
 	// CreateClientRole creates a new role for a client
 	CreateClientRole(accessToken, realm, clientID string, role Role) (string, error)
 	// DeleteClientRole deletes the given role
 	DeleteClientRole(accessToken, realm, clientID, roleName string) error
 	// DeleteClientRoleFromUser removes a client role from from the user
 	DeleteClientRoleFromUser(token string, realm string, clientID string, userID string, roles []Role) error
+	// DeleteClientRoleFromGroup removes a client role from from the group
+	DeleteClientRoleFromGroup(token string, realm string, clientID string, groupID string, roles []Role) error
 	// GetClientRoles gets roles for the given client
 	GetClientRoles(accessToken string, realm string, clientID string) ([]*Role, error)
 	// GetClientRole get a role for the given client in a realm by role name


Test patch:
diff --git a/client_test.go b/client_test.go
index 4ffd5d4..2846262 100644
--- a/client_test.go
+++ b/client_test.go
@@ -2345,6 +2345,54 @@ func TestGocloak_AddClientRoleToUser_DeleteClientRoleFromUser(t *testing.T) {
 	assert.NoError(t, err, "DeleteClientRoleFromUser failed")
 }
 
+func TestGocloak_AddClientRoleToGroup_DeleteClientRoleFromGroup(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	SetUpTestUser(t, client)
+	tearDown1, roleName1 := CreateClientRole(t, client)
+	defer tearDown1()
+	token := GetAdminToken(t, client)
+	role1, err := client.GetClientRole(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		gocloakClientID,
+		roleName1,
+	)
+	assert.NoError(t, err, "GetClientRole failed")
+	tearDown2, roleName2 := CreateClientRole(t, client)
+	defer tearDown2()
+	role2, err := client.GetClientRole(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		gocloakClientID,
+		roleName2,
+	)
+	assert.NoError(t, err, "GetClientRole failed")
+
+	tearDownGroup, groupID := CreateGroup(t, client)
+	defer tearDownGroup()
+
+	roles := []Role{*role1, *role2}
+	err = client.AddClientRoleToGroup(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		gocloakClientID,
+		groupID,
+		roles,
+	)
+	assert.NoError(t, err, "AddClientRoleToGroup failed")
+
+	err = client.DeleteClientRoleFromGroup(
+		token.AccessToken,
+		cfg.GoCloak.Realm,
+		gocloakClientID,
+		groupID,
+		roles,
+	)
+	assert.NoError(t, err, "DeleteClientRoleFromGroup failed")
+}
+
 func TestGocloak_AddDeleteClientRoleComposite(t *testing.T) {
 	t.Parallel()
 	cfg := GetConfig(t)