Skip to content

Commit

Permalink
add/remove/get group users
Browse files Browse the repository at this point in the history
  • Loading branch information
techBeck03 committed Jan 5, 2021
1 parent f0420a0 commit 52d8527
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
24 changes: 24 additions & 0 deletions examples/user_groups/user_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 31 additions & 0 deletions user_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
61 changes: 61 additions & 0 deletions user_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
Protocol: "ssh",
}
testUserGroupPermissionItems = []types.GuacPermissionItem{}
testUserGroupUser = types.GuacUser{Username: "testingUserGroups"}
)

func TestListUserGroups(t *testing.T) {
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 52d8527

Please sign in to comment.