Skip to content

Commit a278fd1

Browse files
committed
Fix test
1 parent 0637dcb commit a278fd1

File tree

6 files changed

+90
-56
lines changed

6 files changed

+90
-56
lines changed

models/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ var migrations = []Migration{
360360
// v202 -> v203
361361
NewMigration("Create key/value table for user settings", createUserSettingsTable),
362362
// v203 -> v204
363-
NewMigration("Add column authorize column for team_unit table", addAuthorizeColForTeamUnit),
363+
NewMigration("Add authorize column to team_unit table", addAuthorizeColForTeamUnit),
364364
}
365365

366366
// GetCurrentDBVersion returns the current db version

models/unit/unit.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ type Type int
1717

1818
// Enumerate all the unit types
1919
const (
20-
TypeCode Type = iota + 1 // 1 code
21-
TypeIssues // 2 issues
22-
TypePullRequests // 3 PRs
23-
TypeReleases // 4 Releases
24-
TypeUncyclo // 5 Uncyclo
25-
TypeExternalUncyclo // 6 ExternalUncyclo
26-
TypeExternalTracker // 7 ExternalTracker
27-
TypeProjects // 8 Kanban board
20+
TypeInvalid Type = iota // 0 invalid
21+
TypeCode // 1 code
22+
TypeIssues // 2 issues
23+
TypePullRequests // 3 PRs
24+
TypeReleases // 4 Releases
25+
TypeUncyclo // 5 Uncyclo
26+
TypeExternalUncyclo // 6 ExternalUncyclo
27+
TypeExternalTracker // 7 ExternalTracker
28+
TypeProjects // 8 Kanban board
2829
)
2930

3031
// Value returns integer value for unit type
@@ -269,15 +270,30 @@ var (
269270
}
270271
)
271272

272-
// FindUnitTypes give the unit key name and return unit
273+
// FindUnitTypes give the unit key names and return unit
273274
func FindUnitTypes(nameKeys ...string) (res []Type) {
274275
for _, key := range nameKeys {
276+
var found bool
275277
for t, u := range Units {
276278
if strings.EqualFold(key, u.NameKey) {
277279
res = append(res, t)
280+
found = true
278281
break
279282
}
280283
}
284+
if !found {
285+
res = append(res, TypeInvalid)
286+
}
281287
}
282288
return
283289
}
290+
291+
// UnitTypeFromKey give the unit key name and return unit
292+
func UnitTypeFromKey(nameKey string) Type {
293+
for t, u := range Units {
294+
if strings.EqualFold(nameKey, u.NameKey) {
295+
return t
296+
}
297+
}
298+
return TypeInvalid
299+
}

modules/structs/org_team.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ type Team struct {
1515
// enum: none,read,write,admin,owner
1616
Permission string `json:"permission"`
1717
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.projects","repo.ext_wiki"]
18-
Units []string `json:"units"`
19-
CanCreateOrgRepo bool `json:"can_create_org_repo"`
18+
Units []string `json:"units"`
19+
// example: {"repo.code":"read","repo.issues":"write","repo.ext_issues":"none","repo.wiki":"admin","repo.pulls":"owner","repo.releases":"none","repo.projects":"none","repo.ext_wiki":"none"]
20+
UnitsMap map[string]string `json:"units_map"`
21+
CanCreateOrgRepo bool `json:"can_create_org_repo"`
2022
}
2123

2224
// CreateTeamOption options for creating a team
@@ -28,8 +30,10 @@ type CreateTeamOption struct {
2830
// enum: read,write,admin
2931
Permission string `json:"permission"`
3032
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.projects","repo.ext_wiki"]
31-
Units []string `json:"units"`
32-
CanCreateOrgRepo bool `json:"can_create_org_repo"`
33+
Units []string `json:"units"`
34+
// example: {"repo.code":"read","repo.issues":"write","repo.ext_issues":"none","repo.wiki":"admin","repo.pulls":"owner","repo.releases":"none","repo.projects":"none","repo.ext_wiki":"none"]
35+
UnitsMap map[string]string `json:"units_map"`
36+
CanCreateOrgRepo bool `json:"can_create_org_repo"`
3337
}
3438

3539
// EditTeamOption options for editing a team
@@ -41,6 +45,8 @@ type EditTeamOption struct {
4145
// enum: read,write,admin
4246
Permission string `json:"permission"`
4347
// example: ["repo.code","repo.issues","repo.ext_issues","repo.wiki","repo.pulls","repo.releases","repo.projects","repo.ext_wiki"]
44-
Units []string `json:"units"`
45-
CanCreateOrgRepo *bool `json:"can_create_org_repo"`
48+
Units []string `json:"units"`
49+
// example: {"repo.code":"read","repo.issues":"write","repo.ext_issues":"none","repo.wiki":"admin","repo.pulls":"owner","repo.releases":"none","repo.projects":"none","repo.ext_wiki":"none"]
50+
UnitsMap map[string]string `json:"units_map"`
51+
CanCreateOrgRepo *bool `json:"can_create_org_repo"`
4652
}

options/locale/locale_en-US.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,8 +2234,8 @@ teams.leave = Leave
22342234
teams.leave.detail = Leave %s?
22352235
teams.can_create_org_repo = Create repositories
22362236
teams.can_create_org_repo_helper = Members can create new repositories in organization. Creator will get administrator access to the new repository.
2237-
teams.none_access = None Access
2238-
teams.none_access_helper = Members can view or do any other action on this unit.
2237+
teams.none_access = No Access
2238+
teams.none_access_helper = Members cannot view or do any other action on this unit.
22392239
teams.general_access = General Access
22402240
teams.general_access_helper = Members permissions will be decided by below permission table.
22412241
teams.read_access = Read Access

routers/api/v1/org/team.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org
77

88
import (
9+
"errors"
910
"net/http"
1011

1112
"code.gitea.io/gitea/models"
@@ -140,6 +141,29 @@ func GetTeam(ctx *context.APIContext) {
140141
ctx.JSON(http.StatusOK, convert.ToTeam(ctx.Org.Team))
141142
}
142143

144+
func attachTeamUnits(team *models.Team, units []string) {
145+
unitTypes := unit_model.FindUnitTypes(units...)
146+
team.Units = make([]*models.TeamUnit, 0, len(units))
147+
for _, tp := range unitTypes {
148+
team.Units = append(team.Units, &models.TeamUnit{
149+
OrgID: team.OrgID,
150+
Type: tp,
151+
Authorize: team.Authorize,
152+
})
153+
}
154+
}
155+
156+
func attachTeamUnitsMap(team *models.Team, unitsMap map[string]string) {
157+
team.Units = make([]*models.TeamUnit, 0, len(unitsMap))
158+
for unitKey, perm := range unitsMap {
159+
team.Units = append(team.Units, &models.TeamUnit{
160+
OrgID: team.OrgID,
161+
Type: unit_model.UnitTypeFromKey(unitKey),
162+
Authorize: perm.ParseAccessMode(perm),
163+
})
164+
}
165+
}
166+
143167
// CreateTeam api for create a team
144168
func CreateTeam(ctx *context.APIContext) {
145169
// swagger:operation POST /orgs/{org}/teams organization orgCreateTeam
@@ -174,17 +198,15 @@ func CreateTeam(ctx *context.APIContext) {
174198
Authorize: perm.ParseAccessMode(form.Permission),
175199
}
176200

177-
unitTypes := unit_model.FindUnitTypes(form.Units...)
178-
179201
if team.Authorize < perm.AccessModeOwner {
180-
var units = make([]*models.TeamUnit, 0, len(form.Units))
181-
for _, tp := range unitTypes {
182-
units = append(units, &models.TeamUnit{
183-
OrgID: ctx.Org.Organization.ID,
184-
Type: tp,
185-
})
202+
if len(form.UnitsMap) > 0 {
203+
attachTeamUnitsMap(team, form.UnitsMap)
204+
} else if len(form.Units) > 0 {
205+
attachTeamUnits(team, form.Units)
206+
} else {
207+
ctx.Error(http.StatusInternalServerError, "getTeamUnits", errors.New("units permission should not be empty"))
208+
return
186209
}
187-
team.Units = units
188210
}
189211

190212
if err := models.NewTeam(team); err != nil {
@@ -260,16 +282,10 @@ func EditTeam(ctx *context.APIContext) {
260282
}
261283

262284
if team.Authorize < perm.AccessModeOwner {
263-
if len(form.Units) > 0 {
264-
var units = make([]*models.TeamUnit, 0, len(form.Units))
265-
unitTypes := unit_model.FindUnitTypes(form.Units...)
266-
for _, tp := range unitTypes {
267-
units = append(units, &models.TeamUnit{
268-
OrgID: ctx.Org.Team.OrgID,
269-
Type: tp,
270-
})
271-
}
272-
team.Units = units
285+
if len(form.UnitsMap) > 0 {
286+
attachTeamUnitsMap(team, form.UnitsMap)
287+
} else if len(form.Units) > 0 {
288+
attachTeamUnits(team, form.Units)
273289
}
274290
}
275291

routers/web/org/teams.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,9 @@ func NewTeam(ctx *context.Context) {
224224
ctx.HTML(http.StatusOK, tplTeamNew)
225225
}
226226

227-
// NewTeamPost response for create new team
228-
func NewTeamPost(ctx *context.Context) {
229-
form := web.GetForm(ctx).(*forms.CreateTeamForm)
230-
ctx.Data["Title"] = ctx.Org.Organization.FullName
231-
ctx.Data["PageIsOrgTeams"] = true
232-
ctx.Data["PageIsOrgTeamsNew"] = true
233-
ctx.Data["Units"] = unit_model.Units
234-
var includesAllRepositories = form.RepoAccess == "all"
227+
func getUnitPerms(forms url.Values) map[unit_model.Type]perm.AccessMode {
235228
var unitPerms = make(map[unit_model.Type]perm.AccessMode)
236-
for k, v := range ctx.Req.Form {
229+
for k, v := range forms {
237230
if strings.HasPrefix(k, "unit_") {
238231
t, _ := strconv.Atoi(k[5:])
239232
if t > 0 {
@@ -242,6 +235,18 @@ func NewTeamPost(ctx *context.Context) {
242235
}
243236
}
244237
}
238+
return unitPerms
239+
}
240+
241+
// NewTeamPost response for create new team
242+
func NewTeamPost(ctx *context.Context) {
243+
form := web.GetForm(ctx).(*forms.CreateTeamForm)
244+
ctx.Data["Title"] = ctx.Org.Organization.FullName
245+
ctx.Data["PageIsOrgTeams"] = true
246+
ctx.Data["PageIsOrgTeamsNew"] = true
247+
ctx.Data["Units"] = unit_model.Units
248+
var includesAllRepositories = form.RepoAccess == "all"
249+
var unitPerms = getUnitPerms(ctx.Req.Form)
245250

246251
t := &models.Team{
247252
OrgID: ctx.Org.Organization.ID,
@@ -333,16 +338,7 @@ func EditTeamPost(ctx *context.Context) {
333338
ctx.Data["Team"] = t
334339
ctx.Data["Units"] = unit_model.Units
335340

336-
var unitPerms = make(map[unit_model.Type]perm.AccessMode)
337-
for k, v := range ctx.Req.Form {
338-
if strings.HasPrefix(k, "unit_") {
339-
t, _ := strconv.Atoi(k[5:])
340-
if t > 0 {
341-
vv, _ := strconv.Atoi(v[0])
342-
unitPerms[unit_model.Type(t)] = perm.AccessMode(vv)
343-
}
344-
}
345-
}
341+
var unitPerms = getUnitPerms(ctx.Req.Form)
346342

347343
isAuthChanged := false
348344
isIncludeAllChanged := false

0 commit comments

Comments
 (0)