@@ -102,16 +102,26 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
102
102
)
103
103
defer commitAudit ()
104
104
105
- currentMembers , currentMembersErr := api .Database .GetGroupMembers (ctx , group .ID )
106
- if currentMembersErr != nil {
107
- httpapi .InternalServerError (rw , currentMembersErr )
105
+ var req codersdk.PatchGroupRequest
106
+ if ! httpapi .Read (ctx , rw , r , & req ) {
108
107
return
109
108
}
110
109
111
- aReq .Old = group .Auditable (currentMembers )
110
+ // If the name matches the existing group name pretend we aren't
111
+ // updating the name at all.
112
+ if req .Name == group .Name {
113
+ req .Name = ""
114
+ }
112
115
113
- var req codersdk.PatchGroupRequest
114
- if ! httpapi .Read (ctx , rw , r , & req ) {
116
+ // TODO:
117
+ // - Test no add/remove users to everyone group.
118
+ // - Test no update everyone group name.
119
+ // - Test no update everyone display name.
120
+
121
+ if group .ID == group .OrganizationID && (req .Name != "" || req .DisplayName != nil ) {
122
+ httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
123
+ Message : fmt .Sprintf ("Cannot rename the %q group!" , database .AllUsersGroup ),
124
+ })
115
125
return
116
126
}
117
127
@@ -122,16 +132,27 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
122
132
return
123
133
}
124
134
125
- // If the name matches the existing group name pretend we aren't
126
- // updating the name at all.
127
- if req .Name == group .Name {
128
- req .Name = ""
129
- }
130
-
131
135
users := make ([]string , 0 , len (req .AddUsers )+ len (req .RemoveUsers ))
132
136
users = append (users , req .AddUsers ... )
133
137
users = append (users , req .RemoveUsers ... )
134
138
139
+ if len (users ) > 0 && group .Name == database .AllUsersGroup {
140
+ httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
141
+ Message : fmt .Sprintf ("Cannot add or remove users from the %q group!" , database .AllUsersGroup ),
142
+ })
143
+ return
144
+ }
145
+
146
+ currentMembers , currentMembersErr := api .Database .GetGroupMembers (ctx , database.GetGroupMembersParams {
147
+ ID : group .ID ,
148
+ OrganizationID : group .OrganizationID ,
149
+ })
150
+ if currentMembersErr != nil {
151
+ httpapi .InternalServerError (rw , currentMembersErr )
152
+ return
153
+ }
154
+ aReq .Old = group .Auditable (currentMembers )
155
+
135
156
for _ , id := range users {
136
157
if _ , err := uuid .Parse (id ); err != nil {
137
158
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
@@ -156,6 +177,7 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
156
177
return
157
178
}
158
179
}
180
+
159
181
if req .Name != "" && req .Name != group .Name {
160
182
_ , err := api .Database .GetGroupByOrgAndName (ctx , database.GetGroupByOrgAndNameParams {
161
183
OrganizationID : group .OrganizationID ,
@@ -230,7 +252,9 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
230
252
}
231
253
}
232
254
return nil
233
- }, nil )
255
+ }, & sql.TxOptions {
256
+ Isolation : sql .LevelRepeatableRead ,
257
+ })
234
258
if database .IsUniqueViolation (err ) {
235
259
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
236
260
Message : "Cannot add the same user to a group twice!" ,
@@ -250,7 +274,10 @@ func (api *API) patchGroup(rw http.ResponseWriter, r *http.Request) {
250
274
return
251
275
}
252
276
253
- patchedMembers , err := api .Database .GetGroupMembers (ctx , group .ID )
277
+ patchedMembers , err := api .Database .GetGroupMembers (ctx , database.GetGroupMembersParams {
278
+ ID : group .ID ,
279
+ OrganizationID : group .OrganizationID ,
280
+ })
254
281
if err != nil {
255
282
httpapi .InternalServerError (rw , err )
256
283
return
@@ -283,21 +310,24 @@ func (api *API) deleteGroup(rw http.ResponseWriter, r *http.Request) {
283
310
)
284
311
defer commitAudit ()
285
312
286
- groupMembers , getMembersErr := api .Database .GetGroupMembers (ctx , group .ID )
287
- if getMembersErr != nil {
288
- httpapi .InternalServerError (rw , getMembersErr )
289
- return
290
- }
291
-
292
- aReq .Old = group .Auditable (groupMembers )
293
-
294
313
if group .Name == database .AllUsersGroup {
295
314
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
296
315
Message : fmt .Sprintf ("%q is a reserved group and cannot be deleted!" , database .AllUsersGroup ),
297
316
})
298
317
return
299
318
}
300
319
320
+ groupMembers , getMembersErr := api .Database .GetGroupMembers (ctx , database.GetGroupMembersParams {
321
+ ID : group .ID ,
322
+ OrganizationID : group .OrganizationID ,
323
+ })
324
+ if getMembersErr != nil {
325
+ httpapi .InternalServerError (rw , getMembersErr )
326
+ return
327
+ }
328
+
329
+ aReq .Old = group .Auditable (groupMembers )
330
+
301
331
err := api .Database .DeleteGroupByID (ctx , group .ID )
302
332
if err != nil {
303
333
httpapi .InternalServerError (rw , err )
@@ -336,7 +366,10 @@ func (api *API) group(rw http.ResponseWriter, r *http.Request) {
336
366
group = httpmw .GroupParam (r )
337
367
)
338
368
339
- users , err := api .Database .GetGroupMembers (ctx , group .ID )
369
+ users , err := api .Database .GetGroupMembers (ctx , database.GetGroupMembersParams {
370
+ ID : group .ID ,
371
+ OrganizationID : group .OrganizationID ,
372
+ })
340
373
if err != nil && ! xerrors .Is (err , sql .ErrNoRows ) {
341
374
httpapi .InternalServerError (rw , err )
342
375
return
@@ -381,7 +414,10 @@ func (api *API) groups(rw http.ResponseWriter, r *http.Request) {
381
414
382
415
resp := make ([]codersdk.Group , 0 , len (groups ))
383
416
for _ , group := range groups {
384
- members , err := api .Database .GetGroupMembers (ctx , group .ID )
417
+ members , err := api .Database .GetGroupMembers (ctx , database.GetGroupMembersParams {
418
+ ID : group .ID ,
419
+ OrganizationID : group .OrganizationID ,
420
+ })
385
421
if err != nil {
386
422
httpapi .InternalServerError (rw , err )
387
423
return
0 commit comments