55package org
66
77import (
8+ "fmt"
89 "net/http"
910 "net/url"
1011 "path"
@@ -264,14 +265,26 @@ func NewTeam(ctx *context.Context) {
264265 ctx .HTML (http .StatusOK , tplTeamNew )
265266}
266267
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 {
268269 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+ }
275288 }
276289 }
277290 }
@@ -282,8 +295,8 @@ func getUnitPerms(forms url.Values) map[unit_model.Type]perm.AccessMode {
282295func NewTeamPost (ctx * context.Context ) {
283296 form := web .GetForm (ctx ).(* forms.CreateTeamForm )
284297 includesAllRepositories := form .RepoAccess == "all"
285- unitPerms := getUnitPerms (ctx .Req .Form )
286298 p := perm .ParseAccessMode (form .Permission )
299+ unitPerms := getUnitPerms (ctx .Req .Form , p )
287300 if p < perm .AccessModeAdmin {
288301 // if p is less than admin accessmode, then it should be general accessmode,
289302 // so we should calculate the minial accessmode from units accessmodes.
@@ -299,17 +312,15 @@ func NewTeamPost(ctx *context.Context) {
299312 CanCreateOrgRepo : form .CanCreateOrgRepo ,
300313 }
301314
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+ })
312322 }
323+ t .Units = units
313324
314325 ctx .Data ["Title" ] = ctx .Org .Organization .FullName
315326 ctx .Data ["PageIsOrgTeams" ] = true
@@ -422,8 +433,11 @@ func SearchTeam(ctx *context.Context) {
422433func EditTeam (ctx * context.Context ) {
423434 ctx .Data ["Title" ] = ctx .Org .Organization .FullName
424435 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
427441 ctx .Data ["Units" ] = unit_model .Units
428442 ctx .HTML (http .StatusOK , tplTeamNew )
429443}
@@ -432,7 +446,13 @@ func EditTeam(ctx *context.Context) {
432446func EditTeamPost (ctx * context.Context ) {
433447 form := web .GetForm (ctx ).(* forms.CreateTeamForm )
434448 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+ }
436456 isAuthChanged := false
437457 isIncludeAllChanged := false
438458 includesAllRepositories := form .RepoAccess == "all"
@@ -443,14 +463,6 @@ func EditTeamPost(ctx *context.Context) {
443463 ctx .Data ["Units" ] = unit_model .Units
444464
445465 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-
454466 t .Name = form .TeamName
455467 if t .AccessMode != newAccessMode {
456468 isAuthChanged = true
@@ -467,21 +479,16 @@ func EditTeamPost(ctx *context.Context) {
467479 }
468480
469481 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+ })
484490 }
491+ t .Units = units
485492
486493 if ctx .HasError () {
487494 ctx .HTML (http .StatusOK , tplTeamNew )
0 commit comments