Skip to content

Commit 9f7d069

Browse files
committed
Improve code
1 parent 9849a13 commit 9f7d069

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

models/org_team.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ func (t *Team) GetUnitNames() (res []string) {
142142
return
143143
}
144144

145-
// HasWriteAccess returns true if team has at least write level access mode.
146-
func (t *Team) HasWriteAccess() bool {
147-
return t.Authorize >= perm.AccessModeWrite
148-
}
149-
150145
// IsOwnerTeam returns true if team is owner team.
151146
func (t *Team) IsOwnerTeam() bool {
152147
return t.Name == ownerTeamName
@@ -442,16 +437,7 @@ func (t *Team) UnitEnabled(tp unit.Type) bool {
442437
}
443438

444439
func (t *Team) unitEnabled(e db.Engine, tp unit.Type) bool {
445-
if err := t.getUnits(e); err != nil {
446-
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
447-
}
448-
449-
for _, unit := range t.Units {
450-
if unit.Type == tp {
451-
return true
452-
}
453-
}
454-
return false
440+
return t.unitAccessMode(e, tp) > AccessModeNone
455441
}
456442

457443
// UnitAccessMode returns if the team has the given unit type enabled

models/perm/access_mode.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package perm
77
import (
88
"fmt"
99

10+
unit_model "code.gitea.io/gitea/models/unit"
1011
"code.gitea.io/gitea/modules/log"
1112
)
1213

@@ -59,3 +60,18 @@ func ParseAccessMode(permission string) AccessMode {
5960
return AccessModeRead
6061
}
6162
}
63+
64+
// MinUnitPerms returns the minial permission of the permission map
65+
func MinUnitPerms(unitsMap map[unit_model.Type]AccessMode) AccessMode {
66+
var res = AccessModeNone
67+
for _, mode := range unitsMap {
68+
if mode > AccessModeNone {
69+
if res == AccessModeNone {
70+
res = mode
71+
} else if mode < res {
72+
res = mode
73+
}
74+
}
75+
}
76+
return res
77+
}

models/unit/unit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ func FindUnitTypes(nameKeys ...string) (res []Type) {
288288
return
289289
}
290290

291-
// UnitTypeFromKey give the unit key name and return unit
292-
func UnitTypeFromKey(nameKey string) Type {
291+
// TypeFromKey give the unit key name and return unit
292+
func TypeFromKey(nameKey string) Type {
293293
for t, u := range Units {
294294
if strings.EqualFold(nameKey, u.NameKey) {
295295
return t

routers/api/v1/org/team.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,20 @@ func attachTeamUnits(team *models.Team, units []string) {
153153
}
154154
}
155155

156+
func convertUnitsMap(unitsMap map[string]string) map[unit_model.Type]models.AccessMode {
157+
var res = make(map[unit_model.Type]models.AccessMode, len(unitsMap))
158+
for unitKey, perm := range unitsMap {
159+
res[unit_model.TypeFromKey(unitKey)] = models.ParseAccessMode(perm)
160+
}
161+
return res
162+
}
163+
156164
func attachTeamUnitsMap(team *models.Team, unitsMap map[string]string) {
157165
team.Units = make([]*models.TeamUnit, 0, len(unitsMap))
158166
for unitKey, perm := range unitsMap {
159167
team.Units = append(team.Units, &models.TeamUnit{
160168
OrgID: team.OrgID,
161-
Type: unit_model.UnitTypeFromKey(unitKey),
169+
Type: unit_model.TypeFromKey(unitKey),
162170
Authorize: perm.ParseAccessMode(perm),
163171
})
164172
}
@@ -189,13 +197,19 @@ func CreateTeam(ctx *context.APIContext) {
189197
// "422":
190198
// "$ref": "#/responses/validationError"
191199
form := web.GetForm(ctx).(*api.CreateTeamOption)
200+
201+
var p = perm.ParseAccessMode(form.Permission)
202+
if p < perm.AccessModeOwner {
203+
p = perm.MinUnitPerms(convertUnitsMap(form.UnitsMap))
204+
}
205+
192206
team := &models.Team{
193207
OrgID: ctx.Org.Organization.ID,
194208
Name: form.Name,
195209
Description: form.Description,
196210
IncludesAllRepositories: form.IncludesAllRepositories,
197211
CanCreateOrgRepo: form.CanCreateOrgRepo,
198-
Authorize: perm.ParseAccessMode(form.Permission),
212+
Authorize: p,
199213
}
200214

201215
if team.Authorize < perm.AccessModeOwner {
@@ -268,11 +282,14 @@ func EditTeam(ctx *context.APIContext) {
268282
isIncludeAllChanged := false
269283
if !team.IsOwnerTeam() && len(form.Permission) != 0 {
270284
// Validate permission level.
271-
auth := perm.ParseAccessMode(form.Permission)
285+
var p = perm.ParseAccessMode(form.Permission)
286+
if p < perm.AccessModeOwner {
287+
p = perm.MinUnitPerms(convertUnitsMap(form.UnitsMap))
288+
}
272289

273-
if team.Authorize != auth {
290+
if team.Authorize != p {
274291
isAuthChanged = true
275-
team.Authorize = auth
292+
team.Authorize = p
276293
}
277294

278295
if form.IncludesAllRepositories != nil {

routers/web/org/teams.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,16 @@ func NewTeamPost(ctx *context.Context) {
247247
ctx.Data["Units"] = unit_model.Units
248248
var includesAllRepositories = form.RepoAccess == "all"
249249
var unitPerms = getUnitPerms(ctx.Req.Form)
250+
var p = perm.ParseAccessMode(form.Permission)
251+
if p < perm.AccessModeOwner {
252+
p = perm.MinUnitPerms(unitPerms)
253+
}
250254

251255
t := &models.Team{
252256
OrgID: ctx.Org.Organization.ID,
253257
Name: form.TeamName,
254258
Description: form.Description,
255-
Authorize: perm.ParseAccessMode(form.Permission),
259+
Authorize: p,
256260
IncludesAllRepositories: includesAllRepositories,
257261
CanCreateOrgRepo: form.CanCreateOrgRepo,
258262
}

0 commit comments

Comments
 (0)