-
Notifications
You must be signed in to change notification settings - Fork 6
/
users.go
176 lines (137 loc) · 4.01 KB
/
users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package guacamole
import (
"fmt"
"net/http"
"github.com/techBeck03/guacamole-api-client/types"
)
const (
usersBasePath = "users"
)
// CreateUser creates a guacamole user
func (c *Client) CreateUser(user *types.GuacUser) error {
request, err := c.CreateJSONRequest(http.MethodPost, fmt.Sprintf("%s/%s", c.baseURL, usersBasePath), user)
if err != nil {
return err
}
err = c.Call(request, &user)
if err != nil {
return err
}
return nil
}
// ReadUser gets a user by username
func (c *Client) ReadUser(username string) (types.GuacUser, error) {
var ret types.GuacUser
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s/%s", c.baseURL, usersBasePath, username), nil)
if err != nil {
return ret, err
}
err = c.Call(request, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
// UpdateUser updates a user by username
func (c *Client) UpdateUser(user *types.GuacUser) error {
request, err := c.CreateJSONRequest(http.MethodPut, fmt.Sprintf("%s/%s/%s", c.baseURL, usersBasePath, user.Username), user)
if err != nil {
return err
}
err = c.Call(request, nil)
if err != nil {
return err
}
return nil
}
// DeleteUser deletes a user by username
func (c *Client) DeleteUser(username string) error {
request, err := c.CreateJSONRequest(http.MethodDelete, fmt.Sprintf("%s/%s/%s", c.baseURL, usersBasePath, username), nil)
if err != nil {
return err
}
err = c.Call(request, nil)
if err != nil {
return err
}
return nil
}
// ListUsers lists all users
func (c *Client) ListUsers() ([]types.GuacUser, error) {
var ret map[string]types.GuacUser
var userList []types.GuacUser
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s", c.baseURL, usersBasePath), nil)
if err != nil {
return userList, err
}
err = c.Call(request, &ret)
if err != nil {
return userList, err
}
for i := range ret {
userList = append(userList, ret[i])
}
return userList, nil
}
// GetUserPermissions gets a user's permissions by username
func (c *Client) GetUserPermissions(username string) (types.GuacPermissionData, error) {
var ret types.GuacPermissionData
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s/%s/permissions", c.baseURL, usersBasePath, username), nil)
if err != nil {
return ret, err
}
err = c.Call(request, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
// SetUserConnectionPermissions adds connection permissions to a user
func (c *Client) SetUserConnectionPermissions(username string, permissionItems *[]types.GuacPermissionItem) error {
request, err := c.CreateJSONRequest(http.MethodPatch, fmt.Sprintf("%s/%s/%s/permissions", c.baseURL, usersBasePath, username), permissionItems)
if err != nil {
return err
}
err = c.Call(request, nil)
if err != nil {
return err
}
return nil
}
// SetUserGroupMembership defines parent groups of a given user
func (c *Client) SetUserGroupMembership(username string, permissionItems *[]types.GuacPermissionItem) error {
request, err := c.CreateJSONRequest(http.MethodPatch, fmt.Sprintf("%s/%s/%s/userGroups", c.baseURL, usersBasePath, username), permissionItems)
if err != nil {
return err
}
err = c.Call(request, nil)
if err != nil {
return err
}
return nil
}
// GetUserGroupMembership gets parent groups of a given user
func (c *Client) GetUserGroupMembership(username string) ([]string, error) {
var ret []string
request, err := c.CreateJSONRequest(http.MethodGet, fmt.Sprintf("%s/%s/%s/userGroups", c.baseURL, usersBasePath, username), nil)
if err != nil {
return ret, err
}
err = c.Call(request, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
// SetUserPermissions gets parent groups of a given user
func (c *Client) SetUserPermissions(username string, permissionItems *[]types.GuacPermissionItem) error {
request, err := c.CreateJSONRequest(http.MethodPatch, fmt.Sprintf("%s/%s/%s/permissions", c.baseURL, usersBasePath, username), permissionItems)
if err != nil {
return err
}
err = c.Call(request, nil)
if err != nil {
return err
}
return nil
}