Skip to content

Commit df785f6

Browse files
committed
Fix edit team
1 parent a264c46 commit df785f6

File tree

7 files changed

+103
-83
lines changed

7 files changed

+103
-83
lines changed

models/org_team.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"context"
99
"fmt"
10+
"slices"
1011
"strings"
1112

1213
"code.gitea.io/gitea/models/db"
@@ -218,11 +219,14 @@ func NewTeam(ctx context.Context, t *organization.Team) (err error) {
218219
}
219220

220221
// UpdateTeam updates information of team.
221-
func UpdateTeam(ctx context.Context, t *organization.Team, authChanged, includeAllChanged bool) (err error) {
222+
func UpdateTeam(ctx context.Context, t *organization.Team, updateCols ...string) (err error) {
222223
if len(t.Name) == 0 {
223224
return util.NewInvalidArgumentErrorf("empty team name")
224225
}
225226

227+
authChanged := slices.Contains(updateCols, "authorize")
228+
includeAllChanged := slices.Contains(updateCols, "includes_all_repositories")
229+
226230
if len(t.Description) > 255 {
227231
t.Description = t.Description[:255]
228232
}
@@ -246,8 +250,7 @@ func UpdateTeam(ctx context.Context, t *organization.Team, authChanged, includeA
246250
}
247251

248252
sess := db.GetEngine(ctx)
249-
if _, err = sess.ID(t.ID).Cols("name", "lower_name", "description",
250-
"can_create_org_repo", "authorize", "includes_all_repositories").Update(t); err != nil {
253+
if _, err = sess.ID(t.ID).Cols(updateCols...).Update(t); err != nil {
251254
return fmt.Errorf("update: %w", err)
252255
}
253256

modules/structs/org_team.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type EditTeamOption struct {
4444
Description *string `json:"description" binding:"MaxSize(255)"`
4545
IncludesAllRepositories *bool `json:"includes_all_repositories"`
4646
// enum: read,write,admin
47-
Permission string `json:"permission"`
47+
Permission string `json:"permission" binding:"`
4848
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.projects","repo.ext_wiki"]
4949
// Deprecated: This variable should be replaced by UnitsMap and will be dropped in later versions.
5050
Units []string `json:"units"`

routers/api/v1/org/team.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -293,45 +293,41 @@ func EditTeam(ctx *context.APIContext) {
293293
team.CanCreateOrgRepo = team.IsOwnerTeam() || *form.CanCreateOrgRepo
294294
}
295295

296+
teamName := team.Name
296297
if len(form.Name) > 0 {
297-
team.Name = form.Name
298+
teamName = form.Name
298299
}
299300

301+
description := team.Description
300302
if form.Description != nil {
301-
team.Description = *form.Description
303+
description = *form.Description
302304
}
303305

304-
isAuthChanged := false
305-
isIncludeAllChanged := false
306-
if !team.IsOwnerTeam() && len(form.Permission) != 0 {
307-
// Validate permission level.
308-
p := perm.ParseAccessMode(form.Permission)
309-
if p < perm.AccessModeAdmin && len(form.UnitsMap) > 0 {
310-
p = unit_model.MinUnitAccessMode(convertUnitsMap(form.UnitsMap))
311-
}
312-
313-
if team.AccessMode != p {
314-
isAuthChanged = true
315-
team.AccessMode = p
316-
}
306+
includeAllRepos := team.IncludesAllRepositories
307+
if form.IncludesAllRepositories != nil {
308+
includeAllRepos = *form.IncludesAllRepositories
309+
}
317310

318-
if form.IncludesAllRepositories != nil {
319-
isIncludeAllChanged = true
320-
team.IncludesAllRepositories = *form.IncludesAllRepositories
321-
}
311+
canCreateOrgRepo := team.CanCreateOrgRepo
312+
if form.CanCreateOrgRepo != nil {
313+
canCreateOrgRepo = *form.CanCreateOrgRepo
322314
}
323315

316+
unitPerms := make(map[unit_model.Type]perm.AccessMode)
324317
if team.AccessMode < perm.AccessModeAdmin {
325318
if len(form.UnitsMap) > 0 {
326-
attachTeamUnitsMap(team, form.UnitsMap)
327-
} else if len(form.Units) > 0 {
328-
attachTeamUnits(team, form.Units)
319+
unitPerms = convertUnitsMap(form.UnitsMap)
329320
}
330-
} else {
331-
attachAdminTeamUnits(team)
332321
}
333322

334-
if err := models.UpdateTeam(ctx, team, isAuthChanged, isIncludeAllChanged); err != nil {
323+
if err := org_service.UpdateTeam(ctx, team,
324+
teamName,
325+
description,
326+
form.Permission == "admin",
327+
includeAllRepos,
328+
canCreateOrgRepo,
329+
unitPerms,
330+
); err != nil {
335331
ctx.Error(http.StatusInternalServerError, "EditTeam", err)
336332
return
337333
}

routers/web/org/teams.go

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -482,61 +482,24 @@ func EditTeam(ctx *context.Context) {
482482
func EditTeamPost(ctx *context.Context) {
483483
form := web.GetForm(ctx).(*forms.CreateTeamForm)
484484
t := ctx.Org.Team
485-
newAccessMode := perm.ParseAccessMode(form.Permission)
486-
unitPerms := getUnitPerms(ctx.Req.Form, newAccessMode)
487-
if newAccessMode < perm.AccessModeAdmin {
488-
// if newAccessMode is less than admin accessmode, then it should be general accessmode,
489-
// so we should calculate the minial accessmode from units accessmodes.
490-
newAccessMode = unit_model.MinUnitAccessMode(unitPerms)
491-
}
492-
isAuthChanged := false
493-
isIncludeAllChanged := false
494-
includesAllRepositories := form.RepoAccess == "all"
485+
unitPerms := getUnitPerms(ctx.Req.Form, perm.ParseAccessMode(form.Permission))
495486

496487
ctx.Data["Title"] = ctx.Org.Organization.FullName
497488
ctx.Data["PageIsOrgTeams"] = true
498489
ctx.Data["Team"] = t
499490
ctx.Data["Units"] = unit_model.Units
500491

501-
if !t.IsOwnerTeam() {
502-
t.Name = form.TeamName
503-
if t.AccessMode != newAccessMode {
504-
isAuthChanged = true
505-
t.AccessMode = newAccessMode
506-
}
507-
508-
if t.IncludesAllRepositories != includesAllRepositories {
509-
isIncludeAllChanged = true
510-
t.IncludesAllRepositories = includesAllRepositories
511-
}
512-
t.CanCreateOrgRepo = form.CanCreateOrgRepo
513-
} else {
514-
t.CanCreateOrgRepo = true
515-
}
516-
517-
t.Description = form.Description
518-
units := make([]*org_model.TeamUnit, 0, len(unitPerms))
519-
for tp, perm := range unitPerms {
520-
units = append(units, &org_model.TeamUnit{
521-
OrgID: t.OrgID,
522-
TeamID: t.ID,
523-
Type: tp,
524-
AccessMode: perm,
525-
})
526-
}
527-
t.Units = units
528-
529-
if ctx.HasError() {
530-
ctx.HTML(http.StatusOK, tplTeamNew)
531-
return
532-
}
533-
534492
if t.AccessMode < perm.AccessModeAdmin && len(unitPerms) == 0 {
535493
ctx.RenderWithErr(ctx.Tr("form.team_no_units_error"), tplTeamNew, &form)
536494
return
537495
}
538496

539-
if err := models.UpdateTeam(ctx, t, isAuthChanged, isIncludeAllChanged); err != nil {
497+
if err := org_service.UpdateTeam(ctx, t, form.TeamName, form.Description,
498+
form.Permission == "admin",
499+
form.RepoAccess == "all",
500+
form.CanCreateOrgRepo,
501+
unitPerms,
502+
); err != nil {
540503
ctx.Data["Err_TeamName"] = true
541504
switch {
542505
case org_model.IsErrTeamAlreadyExist(err):

services/doctor/fix8312.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func fixOwnerTeamCreateOrgRepo(ctx context.Context, logger log.Logger, autofix b
2929
return nil
3030
}
3131

32-
return models.UpdateTeam(ctx, team, false, false)
32+
return models.UpdateTeam(ctx, team, "can_create_org_repo")
3333
},
3434
)
3535
if err != nil {

services/forms/org.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,12 @@ func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors)
5353
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
5454
}
5555

56-
// ___________
57-
// \__ ___/___ _____ _____
58-
// | |_/ __ \\__ \ / \
59-
// | |\ ___/ / __ \| Y Y \
60-
// |____| \___ >____ /__|_| /
61-
// \/ \/ \/
62-
6356
// CreateTeamForm form for creating team
6457
type CreateTeamForm struct {
6558
TeamName string `binding:"Required;AlphaDashDot;MaxSize(255)"`
6659
Description string `binding:"MaxSize(255)"`
67-
Permission string
68-
RepoAccess string
60+
Permission string `binding:"Required;In(admin, read)"`
61+
RepoAccess string `binding:"Required;In(specified, all)"`
6962
CanCreateOrgRepo bool
7063
}
7164

services/org/team.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package org
5+
6+
import (
7+
"context"
8+
9+
"code.gitea.io/gitea/models"
10+
org_model "code.gitea.io/gitea/models/organization"
11+
"code.gitea.io/gitea/models/perm"
12+
unit_model "code.gitea.io/gitea/models/unit"
13+
)
14+
15+
func UpdateTeam(ctx context.Context, team *org_model.Team, teamName, description string, isAdmin, includesAllRepositories, canCreateOrgRepo bool, unitPerms map[unit_model.Type]perm.AccessMode) error {
16+
var changedCols []string
17+
18+
newAccessMode := perm.AccessModeRead
19+
if isAdmin {
20+
newAccessMode = perm.AccessModeAdmin
21+
} else {
22+
// if newAccessMode is less than admin accessmode, then it should be general accessmode,
23+
// so we should calculate the minial accessmode from units accessmodes.
24+
newAccessMode = unit_model.MinUnitAccessMode(unitPerms)
25+
}
26+
27+
if !team.IsOwnerTeam() {
28+
team.Name = teamName
29+
if team.AccessMode != newAccessMode {
30+
team.AccessMode = newAccessMode
31+
changedCols = append(changedCols, "authorize")
32+
}
33+
34+
if team.IncludesAllRepositories != includesAllRepositories {
35+
team.IncludesAllRepositories = includesAllRepositories
36+
changedCols = append(changedCols, "includes_all_repositories")
37+
}
38+
units := make([]*org_model.TeamUnit, 0, len(unitPerms))
39+
for tp, perm := range unitPerms {
40+
units = append(units, &org_model.TeamUnit{
41+
OrgID: team.OrgID,
42+
TeamID: team.ID,
43+
Type: tp,
44+
AccessMode: perm,
45+
})
46+
}
47+
team.Units = units
48+
changedCols = append(changedCols, "units")
49+
if team.CanCreateOrgRepo != canCreateOrgRepo {
50+
team.CanCreateOrgRepo = canCreateOrgRepo
51+
changedCols = append(changedCols, "can_create_org_repo")
52+
}
53+
} else {
54+
team.CanCreateOrgRepo = true
55+
team.IncludesAllRepositories = true
56+
changedCols = append(changedCols, "can_create_org_repo", "includes_all_repositories")
57+
}
58+
59+
if team.Description != description {
60+
changedCols = append(changedCols, "description")
61+
team.Description = description
62+
}
63+
64+
return models.UpdateTeam(ctx, team, changedCols...)
65+
}

0 commit comments

Comments
 (0)