Skip to content

Commit 3e2e76e

Browse files
wxiaoguangGiteaBot
andauthored
Refactor web routes (#30519)
Re-organize the routes in web.go and use ctx constants instead of `context.UnitTypes()` --------- Co-authored-by: Giteabot <[email protected]>
1 parent 4f276a3 commit 3e2e76e

23 files changed

+391
-374
lines changed

models/organization/team.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,27 @@ func (t *Team) LoadMembers(ctx context.Context) (err error) {
174174
return err
175175
}
176176

177-
// UnitEnabled returns if the team has the given unit type enabled
177+
// UnitEnabled returns true if the team has the given unit type enabled
178178
func (t *Team) UnitEnabled(ctx context.Context, tp unit.Type) bool {
179179
return t.UnitAccessMode(ctx, tp) > perm.AccessModeNone
180180
}
181181

182-
// UnitAccessMode returns if the team has the given unit type enabled
182+
// UnitAccessMode returns the access mode for the given unit type, "none" for non-existent units
183183
func (t *Team) UnitAccessMode(ctx context.Context, tp unit.Type) perm.AccessMode {
184+
accessMode, _ := t.UnitAccessModeEx(ctx, tp)
185+
return accessMode
186+
}
187+
188+
func (t *Team) UnitAccessModeEx(ctx context.Context, tp unit.Type) (accessMode perm.AccessMode, exist bool) {
184189
if err := t.LoadUnits(ctx); err != nil {
185190
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
186191
}
187-
188-
for _, unit := range t.Units {
189-
if unit.Type == tp {
190-
return unit.AccessMode
192+
for _, u := range t.Units {
193+
if u.Type == tp {
194+
return u.AccessMode, true
191195
}
192196
}
193-
return perm.AccessModeNone
197+
return perm.AccessModeNone, false
194198
}
195199

196200
// IsUsableTeamName tests if a name could be as team name

models/perm/access/repo_permission.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
102102
return p.CanWrite(unit.TypeIssues)
103103
}
104104

105+
func (p *Permission) ReadableUnitTypes() []unit.Type {
106+
types := make([]unit.Type, 0, len(p.Units))
107+
for _, u := range p.Units {
108+
if p.CanRead(u.Type) {
109+
types = append(types, u.Type)
110+
}
111+
}
112+
return types
113+
}
114+
105115
func (p *Permission) LogString() string {
106116
format := "<Permission AccessMode=%s, %d Units, %d UnitsMode(s): [ "
107117
args := []any{p.AccessMode.String(), len(p.Units), len(p.UnitsMode)}
@@ -229,12 +239,8 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
229239
for _, u := range repo.Units {
230240
var found bool
231241
for _, team := range teams {
232-
teamMode := team.UnitAccessMode(ctx, u.Type)
233-
if teamMode > perm_model.AccessModeNone {
234-
m := perm.UnitsMode[u.Type]
235-
if m < teamMode {
236-
perm.UnitsMode[u.Type] = teamMode
237-
}
242+
if teamMode, exist := team.UnitAccessModeEx(ctx, u.Type); exist {
243+
perm.UnitsMode[u.Type] = max(perm.UnitsMode[u.Type], teamMode)
238244
found = true
239245
}
240246
}

routers/web/repo/view.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,12 @@ func checkHomeCodeViewable(ctx *context.Context) {
721721
}
722722

723723
var firstUnit *unit_model.Unit
724-
for _, repoUnit := range ctx.Repo.Units {
725-
if repoUnit.Type == unit_model.TypeCode {
724+
for _, repoUnitType := range ctx.Repo.Permission.ReadableUnitTypes() {
725+
if repoUnitType == unit_model.TypeCode {
726726
return
727727
}
728728

729-
unit, ok := unit_model.Units[repoUnit.Type]
729+
unit, ok := unit_model.Units[repoUnitType]
730730
if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
731731
firstUnit = &unit
732732
}

0 commit comments

Comments
 (0)