forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
269 lines (222 loc) · 7.8 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package api
import (
"bytes"
"cf/api/resources"
"cf/configuration"
"cf/errors"
"cf/models"
"cf/net"
"encoding/json"
"fmt"
"net/http"
neturl "net/url"
"strings"
)
var orgRoleToPathMap = map[string]string{
models.ORG_USER: "users",
models.ORG_MANAGER: "managers",
models.BILLING_MANAGER: "billing_managers",
models.ORG_AUDITOR: "auditors",
}
var spaceRoleToPathMap = map[string]string{
models.SPACE_MANAGER: "managers",
models.SPACE_DEVELOPER: "developers",
models.SPACE_AUDITOR: "auditors",
}
type UserRepository interface {
FindByUsername(username string) (user models.UserFields, apiErr error)
ListUsersInOrgForRole(orgGuid string, role string) ([]models.UserFields, error)
ListUsersInSpaceForRole(spaceGuid string, role string) ([]models.UserFields, error)
Create(username, password string) (apiErr error)
Delete(userGuid string) (apiErr error)
SetOrgRole(userGuid, orgGuid, role string) (apiErr error)
UnsetOrgRole(userGuid, orgGuid, role string) (apiErr error)
SetSpaceRole(userGuid, spaceGuid, orgGuid, role string) (apiErr error)
UnsetSpaceRole(userGuid, spaceGuid, role string) (apiErr error)
}
type CloudControllerUserRepository struct {
config configuration.Reader
uaaGateway net.Gateway
ccGateway net.Gateway
}
func NewCloudControllerUserRepository(config configuration.Reader, uaaGateway net.Gateway, ccGateway net.Gateway) (repo CloudControllerUserRepository) {
repo.config = config
repo.uaaGateway = uaaGateway
repo.ccGateway = ccGateway
return
}
func (repo CloudControllerUserRepository) FindByUsername(username string) (user models.UserFields, apiErr error) {
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return
}
usernameFilter := neturl.QueryEscape(fmt.Sprintf(`userName Eq "%s"`, username))
path := fmt.Sprintf("%s/Users?attributes=id,userName&filter=%s", uaaEndpoint, usernameFilter)
users, apiErr := repo.updateOrFindUsersWithUAAPath([]models.UserFields{}, path)
if len(users) == 0 {
apiErr = errors.NewModelNotFoundError("User", username)
return
}
user = users[0]
return
}
func (repo CloudControllerUserRepository) ListUsersInOrgForRole(orgGuid string, roleName string) (users []models.UserFields, apiErr error) {
return repo.listUsersWithPath(fmt.Sprintf("/v2/organizations/%s/%s", orgGuid, orgRoleToPathMap[roleName]))
}
func (repo CloudControllerUserRepository) ListUsersInSpaceForRole(spaceGuid string, roleName string) (users []models.UserFields, apiErr error) {
return repo.listUsersWithPath(fmt.Sprintf("/v2/spaces/%s/%s", spaceGuid, spaceRoleToPathMap[roleName]))
}
func (repo CloudControllerUserRepository) listUsersWithPath(path string) (users []models.UserFields, apiErr error) {
guidFilters := []string{}
apiErr = repo.ccGateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
path,
resources.UserResource{},
func(resource interface{}) bool {
user := resource.(resources.UserResource).ToFields()
users = append(users, user)
guidFilters = append(guidFilters, fmt.Sprintf(`Id eq "%s"`, user.Guid))
return true
})
if apiErr != nil {
return
}
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return
}
filter := strings.Join(guidFilters, " or ")
usersURL := fmt.Sprintf("%s/Users?attributes=id,userName&filter=%s", uaaEndpoint, neturl.QueryEscape(filter))
users, apiErr = repo.updateOrFindUsersWithUAAPath(users, usersURL)
return
}
func (repo CloudControllerUserRepository) updateOrFindUsersWithUAAPath(ccUsers []models.UserFields, path string) (updatedUsers []models.UserFields, apiErr error) {
uaaResponse := new(resources.UAAUserResources)
apiErr = repo.uaaGateway.GetResource(path, uaaResponse)
if apiErr != nil {
return
}
for _, uaaResource := range uaaResponse.Resources {
var ccUserFields models.UserFields
for _, u := range ccUsers {
if u.Guid == uaaResource.Id {
ccUserFields = u
break
}
}
updatedUsers = append(updatedUsers, models.UserFields{
Guid: uaaResource.Id,
Username: uaaResource.Username,
IsAdmin: ccUserFields.IsAdmin,
})
}
return
}
func (repo CloudControllerUserRepository) Create(username, password string) (err error) {
uaaEndpoint, err := repo.getAuthEndpoint()
if err != nil {
return
}
path := fmt.Sprintf("%s/Users", uaaEndpoint)
body, err := json.Marshal(resources.NewUAAUserResource(username, password))
if err != nil {
return
}
createUserResponse := &resources.UAAUserFields{}
err = repo.uaaGateway.CreateResource(path, bytes.NewReader(body), createUserResponse)
switch httpErr := err.(type) {
case nil:
case errors.HttpError:
if httpErr.StatusCode() == http.StatusConflict {
err = errors.NewModelAlreadyExistsError("user", username)
return
}
default:
return
}
path = fmt.Sprintf("%s/v2/users", repo.config.ApiEndpoint())
body, err = json.Marshal(resources.Metadata{
Guid: createUserResponse.Id,
})
if err != nil {
return
}
return repo.ccGateway.CreateResource(path, bytes.NewReader(body))
}
func (repo CloudControllerUserRepository) Delete(userGuid string) (apiErr error) {
path := fmt.Sprintf("%s/v2/users/%s", repo.config.ApiEndpoint(), userGuid)
apiErr = repo.ccGateway.DeleteResource(path)
if httpErr, ok := apiErr.(errors.HttpError); ok && httpErr.ErrorCode() != errors.USER_NOT_FOUND {
return
}
uaaEndpoint, apiErr := repo.getAuthEndpoint()
if apiErr != nil {
return
}
path = fmt.Sprintf("%s/Users/%s", uaaEndpoint, userGuid)
return repo.uaaGateway.DeleteResource(path)
}
func (repo CloudControllerUserRepository) SetOrgRole(userGuid string, orgGuid string, role string) (apiErr error) {
apiErr = repo.setOrUnsetOrgRole("PUT", userGuid, orgGuid, role)
if apiErr != nil {
return
}
return repo.addOrgUserRole(userGuid, orgGuid)
}
func (repo CloudControllerUserRepository) UnsetOrgRole(userGuid, orgGuid, role string) (apiErr error) {
return repo.setOrUnsetOrgRole("DELETE", userGuid, orgGuid, role)
}
func (repo CloudControllerUserRepository) setOrUnsetOrgRole(verb, userGuid, orgGuid, role string) (apiErr error) {
rolePath, found := orgRoleToPathMap[role]
if !found {
apiErr = errors.NewWithFmt("Invalid Role %s", role)
return
}
path := fmt.Sprintf("%s/v2/organizations/%s/%s/%s", repo.config.ApiEndpoint(), orgGuid, rolePath, userGuid)
request, apiErr := repo.ccGateway.NewRequest(verb, path, repo.config.AccessToken(), nil)
if apiErr != nil {
return
}
_, apiErr = repo.ccGateway.PerformRequest(request)
if apiErr != nil {
return
}
return
}
func (repo CloudControllerUserRepository) SetSpaceRole(userGuid, spaceGuid, orgGuid, role string) (apiErr error) {
rolePath, apiErr := repo.checkSpaceRole(userGuid, spaceGuid, role)
if apiErr != nil {
return
}
apiErr = repo.addOrgUserRole(userGuid, orgGuid)
if apiErr != nil {
return
}
return repo.ccGateway.UpdateResource(rolePath, nil)
}
func (repo CloudControllerUserRepository) UnsetSpaceRole(userGuid, spaceGuid, role string) (apiErr error) {
rolePath, apiErr := repo.checkSpaceRole(userGuid, spaceGuid, role)
if apiErr != nil {
return
}
return repo.ccGateway.DeleteResource(rolePath)
}
func (repo CloudControllerUserRepository) checkSpaceRole(userGuid, spaceGuid, role string) (fullPath string, apiErr error) {
rolePath, found := spaceRoleToPathMap[role]
if !found {
apiErr = errors.NewWithFmt("Invalid Role %s", role)
}
fullPath = fmt.Sprintf("%s/v2/spaces/%s/%s/%s", repo.config.ApiEndpoint(), spaceGuid, rolePath, userGuid)
return
}
func (repo CloudControllerUserRepository) addOrgUserRole(userGuid, orgGuid string) (apiErr error) {
path := fmt.Sprintf("%s/v2/organizations/%s/users/%s", repo.config.ApiEndpoint(), orgGuid, userGuid)
return repo.ccGateway.UpdateResource(path, nil)
}
func (repo CloudControllerUserRepository) getAuthEndpoint() (string, error) {
uaaEndpoint := repo.config.UaaEndpoint()
if uaaEndpoint == "" {
return "", errors.New("UAA endpoint missing from config file")
}
return uaaEndpoint, nil
}