5
5
package org
6
6
7
7
import (
8
+ "fmt"
8
9
"net/http"
9
10
"net/url"
10
11
"path"
@@ -264,14 +265,26 @@ func NewTeam(ctx *context.Context) {
264
265
ctx .HTML (http .StatusOK , tplTeamNew )
265
266
}
266
267
267
- func getUnitPerms (forms url.Values ) map [unit_model.Type ]perm.AccessMode {
268
+ func getUnitPerms (forms url.Values , teamPermission perm. AccessMode ) map [unit_model.Type ]perm.AccessMode {
268
269
unitPerms := make (map [unit_model.Type ]perm.AccessMode )
269
- for k , v := range forms {
270
- if strings .HasPrefix (k , "unit_" ) {
271
- t , _ := strconv .Atoi (k [5 :])
272
- if t > 0 {
273
- vv , _ := strconv .Atoi (v [0 ])
274
- unitPerms [unit_model .Type (t )] = perm .AccessMode (vv )
270
+ for _ , ut := range unit_model .AllRepoUnitTypes {
271
+ // Default accessmode is none
272
+ unitPerms [ut ] = perm .AccessModeNone
273
+
274
+ v , ok := forms [fmt .Sprintf ("unit_%d" , ut )]
275
+ if ok {
276
+ vv , _ := strconv .Atoi (v [0 ])
277
+ if teamPermission >= perm .AccessModeAdmin {
278
+ unitPerms [ut ] = teamPermission
279
+ // Don't allow `TypeExternal{Tracker,Wiki}` to influence this as they can only be set to READ perms.
280
+ if ut == unit_model .TypeExternalTracker || ut == unit_model .TypeExternalWiki {
281
+ unitPerms [ut ] = perm .AccessModeRead
282
+ }
283
+ } else {
284
+ unitPerms [ut ] = perm .AccessMode (vv )
285
+ if unitPerms [ut ] >= perm .AccessModeAdmin {
286
+ unitPerms [ut ] = perm .AccessModeWrite
287
+ }
275
288
}
276
289
}
277
290
}
@@ -282,8 +295,8 @@ func getUnitPerms(forms url.Values) map[unit_model.Type]perm.AccessMode {
282
295
func NewTeamPost (ctx * context.Context ) {
283
296
form := web .GetForm (ctx ).(* forms.CreateTeamForm )
284
297
includesAllRepositories := form .RepoAccess == "all"
285
- unitPerms := getUnitPerms (ctx .Req .Form )
286
298
p := perm .ParseAccessMode (form .Permission )
299
+ unitPerms := getUnitPerms (ctx .Req .Form , p )
287
300
if p < perm .AccessModeAdmin {
288
301
// if p is less than admin accessmode, then it should be general accessmode,
289
302
// so we should calculate the minial accessmode from units accessmodes.
@@ -299,17 +312,15 @@ func NewTeamPost(ctx *context.Context) {
299
312
CanCreateOrgRepo : form .CanCreateOrgRepo ,
300
313
}
301
314
302
- if t .AccessMode < perm .AccessModeAdmin {
303
- units := make ([]* org_model.TeamUnit , 0 , len (unitPerms ))
304
- for tp , perm := range unitPerms {
305
- units = append (units , & org_model.TeamUnit {
306
- OrgID : ctx .Org .Organization .ID ,
307
- Type : tp ,
308
- AccessMode : perm ,
309
- })
310
- }
311
- t .Units = units
315
+ units := make ([]* org_model.TeamUnit , 0 , len (unitPerms ))
316
+ for tp , perm := range unitPerms {
317
+ units = append (units , & org_model.TeamUnit {
318
+ OrgID : ctx .Org .Organization .ID ,
319
+ Type : tp ,
320
+ AccessMode : perm ,
321
+ })
312
322
}
323
+ t .Units = units
313
324
314
325
ctx .Data ["Title" ] = ctx .Org .Organization .FullName
315
326
ctx .Data ["PageIsOrgTeams" ] = true
@@ -422,8 +433,11 @@ func SearchTeam(ctx *context.Context) {
422
433
func EditTeam (ctx * context.Context ) {
423
434
ctx .Data ["Title" ] = ctx .Org .Organization .FullName
424
435
ctx .Data ["PageIsOrgTeams" ] = true
425
- ctx .Data ["team_name" ] = ctx .Org .Team .Name
426
- ctx .Data ["desc" ] = ctx .Org .Team .Description
436
+ if err := ctx .Org .Team .LoadUnits (ctx ); err != nil {
437
+ ctx .ServerError ("LoadUnits" , err )
438
+ return
439
+ }
440
+ ctx .Data ["Team" ] = ctx .Org .Team
427
441
ctx .Data ["Units" ] = unit_model .Units
428
442
ctx .HTML (http .StatusOK , tplTeamNew )
429
443
}
@@ -432,7 +446,13 @@ func EditTeam(ctx *context.Context) {
432
446
func EditTeamPost (ctx * context.Context ) {
433
447
form := web .GetForm (ctx ).(* forms.CreateTeamForm )
434
448
t := ctx .Org .Team
435
- unitPerms := getUnitPerms (ctx .Req .Form )
449
+ newAccessMode := perm .ParseAccessMode (form .Permission )
450
+ unitPerms := getUnitPerms (ctx .Req .Form , newAccessMode )
451
+ if newAccessMode < perm .AccessModeAdmin {
452
+ // if newAccessMode is less than admin accessmode, then it should be general accessmode,
453
+ // so we should calculate the minial accessmode from units accessmodes.
454
+ newAccessMode = unit_model .MinUnitAccessMode (unitPerms )
455
+ }
436
456
isAuthChanged := false
437
457
isIncludeAllChanged := false
438
458
includesAllRepositories := form .RepoAccess == "all"
@@ -443,14 +463,6 @@ func EditTeamPost(ctx *context.Context) {
443
463
ctx .Data ["Units" ] = unit_model .Units
444
464
445
465
if ! t .IsOwnerTeam () {
446
- // Validate permission level.
447
- newAccessMode := perm .ParseAccessMode (form .Permission )
448
- if newAccessMode < perm .AccessModeAdmin {
449
- // if p is less than admin accessmode, then it should be general accessmode,
450
- // so we should calculate the minial accessmode from units accessmodes.
451
- newAccessMode = unit_model .MinUnitAccessMode (unitPerms )
452
- }
453
-
454
466
t .Name = form .TeamName
455
467
if t .AccessMode != newAccessMode {
456
468
isAuthChanged = true
@@ -467,21 +479,16 @@ func EditTeamPost(ctx *context.Context) {
467
479
}
468
480
469
481
t .Description = form .Description
470
- if t .AccessMode < perm .AccessModeAdmin {
471
- units := make ([]org_model.TeamUnit , 0 , len (unitPerms ))
472
- for tp , perm := range unitPerms {
473
- units = append (units , org_model.TeamUnit {
474
- OrgID : t .OrgID ,
475
- TeamID : t .ID ,
476
- Type : tp ,
477
- AccessMode : perm ,
478
- })
479
- }
480
- if err := org_model .UpdateTeamUnits (t , units ); err != nil {
481
- ctx .Error (http .StatusInternalServerError , "UpdateTeamUnits" , err .Error ())
482
- return
483
- }
482
+ units := make ([]* org_model.TeamUnit , 0 , len (unitPerms ))
483
+ for tp , perm := range unitPerms {
484
+ units = append (units , & org_model.TeamUnit {
485
+ OrgID : t .OrgID ,
486
+ TeamID : t .ID ,
487
+ Type : tp ,
488
+ AccessMode : perm ,
489
+ })
484
490
}
491
+ t .Units = units
485
492
486
493
if ctx .HasError () {
487
494
ctx .HTML (http .StatusOK , tplTeamNew )
0 commit comments