/
handler_organizations.go
378 lines (319 loc) · 12.4 KB
/
handler_organizations.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"github.com/gazebo-web/fuel-server/bundles/users"
"github.com/gazebo-web/gz-go/v7"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"net/http"
)
// OrganizationCreate creates a new organization
// You can request this method with the following cURL request:
//
// curl -k -H "Content-Type: application/json" -X POST -d '{"name":"OSRF",
// "description":"non-profit", "email":"myemail@org.org"}'
// https://localhost:4430/1.0/organizations
// --header 'authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
func OrganizationCreate(tx *gorm.DB, w http.ResponseWriter,
r *http.Request) (interface{}, *gz.ErrMsg) {
var organization users.CreateOrganization
if em := ParseStruct(&organization, r, false); em != nil {
return nil, em
}
// Sanity check: Find the user associated to the given JWT. Fail if no user.
user, ok, errMsg := getUserFromJWT(tx, r)
if !ok {
return nil, &errMsg
}
response, em := (&users.OrganizationService{}).CreateOrganization(r.Context(),
tx, organization, user)
if em != nil {
return nil, em
}
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
return response, nil
}
// OrganizationList returns a list with all organizations.
// You can request this method with the following cURL request:
//
// curl -k -X GET --url https://localhost:4430/1.0/organizations
// --header 'authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
func OrganizationList(p *gz.PaginationRequest, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.PaginationResult, *gz.ErrMsg) {
return (&users.OrganizationService{}).OrganizationList(p, tx, user, false)
}
// OrganizationUserList returns a paginated list with the users of an organization.
func OrganizationUserList(p *gz.PaginationRequest, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.PaginationResult, *gz.ErrMsg) {
orgName, em := getName(tx, r)
if em != nil {
return nil, nil, em
}
orgSvc := &users.OrganizationService{}
return orgSvc.GetOrgUsers(p, tx, *orgName, user)
}
// OrganizationIndex returns a single organization
// You can request this method with the following cURL request:
//
// curl -k -X GET --url https://localhost:4430/1.0/organizations/{name}
// --header 'authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
//
// Or you can use the following request for retrieving only the public data:
//
// curl -k -X GET --url https://localhost:4430/1.0/organizations/{name}
func OrganizationIndex(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
orgSvc := &users.OrganizationService{}
org, errMsg := orgSvc.GetOrganization(r.Context(), tx, orgName, false)
if org == nil {
return nil, errMsg
}
response := orgSvc.CreateOrganizationResponse(org, jwtUser, false)
return response, nil
}
// OrganizationRemove deletes an organization.
// You can request this method with the following cURL request:
//
// curl -k -X DELETE --url https://localhost:4430/1.0/organizations/{name}
// --header 'authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
func OrganizationRemove(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
orgSvc := &users.OrganizationService{}
response, em := orgSvc.RemoveOrganization(r.Context(), tx, orgName, jwtUser)
if em != nil {
return nil, em
}
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbDelete, err)
}
return response, nil
}
// getName returns the value of the "name" parameter from the HTTP route.
// Returns an gz.ErrMsg if not present
func getName(tx *gorm.DB, r *http.Request) (*string, *gz.ErrMsg) {
// Extract the organization name from the request.
params := mux.Vars(r)
// Get the organization
orgName, present := params["name"]
// If the key does not exist
if !present {
return nil, gz.NewErrorMessage(gz.ErrorUserNotInRequest)
}
return &orgName, nil
}
// getTeamName returns the value of the "teamname" parameter from the HTTP route.
// Returns an gz.ErrMsg if not present
func getTeamName(r *http.Request) (string, *gz.ErrMsg) {
// get team name from request
params := mux.Vars(r)
teamName, present := params["teamname"]
// If the key does not exist
if !present {
return "", gz.NewErrorMessage(gz.ErrorIDNotInRequest)
}
return teamName, nil
}
// OrganizationUpdate modifies an existing organization.
// You can request this method with the following cURL request:
//
// curl -k -X PATCH -d '{"description":"New Description"}'
// https://localhost:4430/1.0/organizations/{name} -H "Content-Type: application/json"
// -H 'Authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
func OrganizationUpdate(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
var uo users.UpdateOrganization
if em := ParseStruct(&uo, r, false); em != nil {
return nil, em
}
if uo.IsEmpty() {
return nil, gz.NewErrorMessage(gz.ErrorFormInvalidValue)
}
orgSvc := &users.OrganizationService{}
org, em := orgSvc.UpdateOrganization(r.Context(), tx, orgName, &uo, jwtUser)
if em != nil {
return nil, em
}
// Commit the DB transaction.
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
infoStr := "Organization has been updated:" +
"\n\t name: " + *org.Name +
"\n\t description: " + *org.Description
gz.LoggerFromRequest(r).Info(infoStr)
// If the user can update the org, then it can see its private info
response := (&users.OrganizationService{}).CreateOrganizationResponse(org, jwtUser, false)
return response, nil
}
// OrganizationUserCreate adds a user to an organization with a given role.
// You can request this method with the following cURL request:
//
// curl -k -X POST https://localhost:4430/1.0/organizations/{orgName}/users
// -H "Content-Type: application/json"
// -d '{"username":"theUserToAdd", "role":"owner|admin|member"}'
// --header 'authorization: Bearer <your-jwt-token-here>'
//
// It returns the added user
func OrganizationUserCreate(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
var orgUser users.AddUserToOrgInput
if em := ParseStruct(&orgUser, r, false); em != nil {
return nil, em
}
resp, em := (&users.OrganizationService{}).AddUserToOrg(r.Context(), tx, orgName, orgUser.Username, orgUser.Role, jwtUser)
if em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
return resp, nil
}
// OrganizationUserRemove removes a user from an organization.
// You can request this method with the following cURL request:
//
// curl -k -X DELETE https://localhost:4430/1.0/organizations/{orgName}/users/{username}
// --header 'authorization: Bearer <your-jwt-token-here>'
//
// It returns the added user
func OrganizationUserRemove(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Extract the username of the user to remove from the request.
params := mux.Vars(r)
userToRemove, present := params["username"]
// If the key does not exist
if !present {
return nil, gz.NewErrorMessage(gz.ErrorUserNotInRequest)
}
resp, em := (&users.OrganizationService{}).RemoveUserFromOrg(r.Context(), tx,
orgName, userToRemove, jwtUser)
if em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
return resp, nil
}
// OrganizationTeamsList returns a paginated list with the teams of an organization.
func OrganizationTeamsList(p *gz.PaginationRequest, user *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.PaginationResult, *gz.ErrMsg) {
orgName, em := getName(tx, r)
if em != nil {
return nil, nil, em
}
orgSvc := &users.OrganizationService{}
return orgSvc.GetTeams(p, tx, *orgName, user)
}
// OrganizationTeamCreate adds a team to an organization.
// You can request this method with the following cURL request:
//
// curl -k -X POST https://localhost:4430/1.0/organizations/{orgName}/teams
// -H "Content-Type: application/json"
// -d '{"name":"teamName", "visible":"aBool", "description":"desc"}'
// --header 'authorization: Bearer <your-jwt-token-here>'
//
// It returns the created team
func OrganizationTeamCreate(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
var teamInput users.CreateTeamForm
if em := ParseStruct(&teamInput, r, false); em != nil {
return nil, em
}
response, em := (&users.OrganizationService{}).CreateTeam(r.Context(), tx, orgName, teamInput, jwtUser)
if em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
return response, nil
}
// OrganizationTeamRemove removes a team from an organization.
// You can request this method with the following cURL request:
//
// curl -k -X DELETE https://localhost:4430/1.0/organizations/{orgName}/teams/{teamname}
// --header 'authorization: Bearer <your-jwt-token-here>'
//
// It returns the team
func OrganizationTeamRemove(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Extract the team name from the request.
teamName, em := getTeamName(r)
if em != nil {
return nil, em
}
response, em := (&users.OrganizationService{}).RemoveTeam(r.Context(), tx,
orgName, teamName, jwtUser)
if em != nil {
return nil, em
}
// commit the DB transaction
// Note: we commit the TX here on purpose, to be able to detect DB errors
// before writing "data" to ResponseWriter. Once you write data (not headers)
// into it the status code is set to 200 (OK).
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
return response, nil
}
// OrganizationTeamUpdate modifies an existing team.
// You can request this method with the following cURL request:
//
// curl -k -X PATCH -d '{"description":"New Description"}'
// https://localhost:4430/1.0/organizations/{name}/teams/{teamname} -H "Content-Type: application/json"
// -H 'Authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
//
// It returns the updated team
func OrganizationTeamUpdate(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Extract the team name from the request.
teamName, em := getTeamName(r)
if em != nil {
return nil, em
}
var ut users.UpdateTeamForm
if em := ParseStruct(&ut, r, false); em != nil {
return nil, em
}
orgSvc := &users.OrganizationService{}
response, em := orgSvc.UpdateTeam(r.Context(), tx, orgName, teamName, ut, jwtUser)
if em != nil {
return nil, em
}
// Commit the DB transaction.
if err := tx.Commit().Error; err != nil {
return nil, gz.NewErrorMessageWithBase(gz.ErrorDbSave, err)
}
infoStr := "Organization Team has been updated: \n\t name: " + teamName
gz.LoggerFromRequest(r).Info(infoStr)
return response, nil
}
// OrganizationTeamIndex returns a single team.
// You can request this method with the following cURL request:
//
// curl -k -X GET --url https://localhost:4430/1.0/organizations/{name}/teams/{teamname}
// --header 'authorization: Bearer <A_VALID_AUTH0_JWT_TOKEN>'
func OrganizationTeamIndex(orgName string, jwtUser *users.User, tx *gorm.DB,
w http.ResponseWriter, r *http.Request) (interface{}, *gz.ErrMsg) {
// Extract the team name from the request.
teamName, em := getTeamName(r)
if em != nil {
return nil, em
}
orgSvc := &users.OrganizationService{}
return orgSvc.GetTeamDetails(r.Context(), tx, orgName, teamName, jwtUser)
}