diff --git a/examples/user_groups/user_groups.go b/examples/user_groups/user_groups.go index b0b68d0..255258d 100644 --- a/examples/user_groups/user_groups.go +++ b/examples/user_groups/user_groups.go @@ -118,6 +118,30 @@ func main() { fmt.Printf("System permissions = %s\n", strings.Join(permissions.SystemPermissions[:], ", ")) + // Create user and add as member + user := types.GuacUser{Username: "testUser"} + err = client.CreateUser(&user) + + if err != nil { + log.Fatal(err) + } + + // Create user permission + permissionItems = []types.GuacPermissionItem{ + client.NewAddGroupMemberPermission(user.Username), + } + + err = client.SetUserGroupUsers(newGroup.Identifier, &permissionItems) + + // Get member users + users, err := client.GetUserGroupUsers(newGroup.Identifier) + + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Member users = %s\n", strings.Join(users[:], ", ")) + // Delete user group err = client.DeleteUserGroup(newGroup.Identifier) diff --git a/user_groups.go b/user_groups.go index fa8a918..41f1577 100644 --- a/user_groups.go +++ b/user_groups.go @@ -206,3 +206,34 @@ func (c *Client) SetUserGroupPermissions(group string, permissionItems *[]types. } return nil } + +// GetUserGroupUsers retrieves member groups of a given group +func (c *Client) GetUserGroupUsers(group string) ([]string, error) { + var ret []string + request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s/%s/memberUsers", c.baseURL, userGroupsBasePath, group), nil) + + if err != nil { + return ret, err + } + + err = c.Call(request, &ret) + if err != nil { + return ret, err + } + return ret, nil +} + +// SetUserGroupUsers defines users of a given group +func (c *Client) SetUserGroupUsers(group string, permissionItems *[]types.GuacPermissionItem) error { + request, err := c.CreateJSONRequest(http.MethodPatch, fmt.Sprintf("%s/%s/%s/memberUsers", c.baseURL, userGroupsBasePath, group), permissionItems) + + if err != nil { + return err + } + + err = c.Call(request, nil) + if err != nil { + return err + } + return nil +} diff --git a/user_groups_test.go b/user_groups_test.go index 87056a8..b4d3db6 100644 --- a/user_groups_test.go +++ b/user_groups_test.go @@ -31,6 +31,7 @@ var ( Protocol: "ssh", } testUserGroupPermissionItems = []types.GuacPermissionItem{} + testUserGroupUser = types.GuacUser{Username: "testingUserGroups"} ) func TestListUserGroups(t *testing.T) { @@ -489,6 +490,66 @@ func TestSetUserGroupPermissions(t *testing.T) { } } +func TestSetUserGroupUsers(t *testing.T) { + client := New(userGroupsConfig) + + err := client.Connect() + if err != nil { + t.Errorf("Error %s connecting to guacamole with config %+v", err, userGroupsConfig) + } + + // Create test user + err = client.CreateUser(&testUserGroupUser) + if err != nil { + t.Errorf("Error creating guac user %s with error: %s", testUserGroupUser.Username, err) + } + + // Add user to group + permissionItems := []types.GuacPermissionItem{ + client.NewAddGroupMemberPermission(testUserGroupUser.Username), + } + + err = client.SetUserGroupUsers(testGroup.Identifier, &permissionItems) + + if err != nil { + t.Errorf("Error adding user: %s to group: %s with error: %s\n", testUserGroupUser.Username, testGroup.Identifier, err) + } + + err = client.Disconnect() + + if err != nil { + t.Errorf("Disconnect errors: %s\n", err) + } +} + +func TestGettUserGroupUsers(t *testing.T) { + client := New(userGroupsConfig) + + err := client.Connect() + if err != nil { + t.Errorf("Error %s connecting to guacamole with config %+v", err, userGroupsConfig) + } + + // Read member users + users, err := client.GetUserGroupUsers(testGroup.Identifier) + + if users[0] != testUserGroupUser.Username { + t.Errorf("Expected member user: %s but found none\n", testUserGroupUser.Username) + } + + // Remove user + err = client.DeleteUser(testUserGroupUser.Username) + if err != nil { + t.Errorf("Error deleting test user: %s with error: %s\n", testUserGroupUser.Username, err) + } + + err = client.Disconnect() + + if err != nil { + t.Errorf("Disconnect errors: %s\n", err) + } +} + func TestDeleteUserGroup(t *testing.T) { client := New(userGroupsConfig) diff --git a/users_test.go b/users_test.go index 564efe4..764ec3d 100644 --- a/users_test.go +++ b/users_test.go @@ -18,7 +18,7 @@ var ( Password: os.Getenv("GUACAMOLE_PASSWORD"), DisableTLSVerification: true, } - testUser = types.GuacUser{Username: "testing"} + testUser = types.GuacUser{Username: "testingUsers"} testUserMemberGroup = types.GuacUserGroup{Identifier: "Test User Group"} testUserConnectionGroup = types.GuacConnectionGroup{ Name: "Testing Users Group",