refactor: use named Permission field in Repository struct instead of anonymous embedding (#37441)
The `Repository` struct in `services/context/repo.go` embedded `access_model.Permission` anonymously, causing all permission methods to be promoted directly onto `Repository`. This made it unclear at call sites whether a method belonged to `Repository` itself or to its embedded `Permission`. ### Changes - **`services/context/repo.go`**: Replace anonymous `access_model.Permission` with named field `Permission access_model.Permission` - **49 files** updated to route permission method calls through the named field: ```go // Before ctx.Repo.IsAdmin() ctx.Repo.CanWrite(unit.TypeCode) ctx.Repo.CanReadIssuesOrPulls(isPull) slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite) // After ctx.Repo.Permission.IsAdmin() ctx.Repo.Permission.CanWrite(unit.TypeCode) ctx.Repo.Permission.CanReadIssuesOrPulls(isPull) slices.ContainsFunc(unitTypes, ctx.Repo.Permission.CanWrite) ``` Methods defined directly on `*Repository` (`CanWriteToBranch`, `CanCreateBranch`, etc.) are unchanged. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Nicolas <bircni@icloud.com>
This commit is contained in:
+10
-10
@@ -47,8 +47,8 @@ const (
|
||||
|
||||
// MustEnableWiki check if wiki is enabled, if external then redirect
|
||||
func MustEnableWiki(ctx *context.Context) {
|
||||
if !ctx.Repo.CanRead(unit.TypeWiki) &&
|
||||
!ctx.Repo.CanRead(unit.TypeExternalWiki) {
|
||||
if !ctx.Repo.Permission.CanRead(unit.TypeWiki) &&
|
||||
!ctx.Repo.Permission.CanRead(unit.TypeExternalWiki) {
|
||||
if log.IsTrace() {
|
||||
log.Trace("Permission Denied: User %-v cannot read %-v or %-v of repo %-v\n"+
|
||||
"User in repo has Permissions: %-+v",
|
||||
@@ -423,14 +423,14 @@ func renderEditPage(ctx *context.Context) {
|
||||
func WikiPost(ctx *context.Context) {
|
||||
switch ctx.FormString("action") {
|
||||
case "_new":
|
||||
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
||||
if !ctx.Repo.Permission.CanWrite(unit.TypeWiki) {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
NewWikiPost(ctx)
|
||||
return
|
||||
case "_delete":
|
||||
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
||||
if !ctx.Repo.Permission.CanWrite(unit.TypeWiki) {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
@@ -438,7 +438,7 @@ func WikiPost(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
||||
if !ctx.Repo.Permission.CanWrite(unit.TypeWiki) {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
@@ -447,7 +447,7 @@ func WikiPost(ctx *context.Context) {
|
||||
|
||||
// Wiki renders single wiki page
|
||||
func Wiki(ctx *context.Context) {
|
||||
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
||||
ctx.Data["CanWriteWiki"] = ctx.Repo.Permission.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
||||
|
||||
switch ctx.FormString("action") {
|
||||
case "_pages":
|
||||
@@ -457,14 +457,14 @@ func Wiki(ctx *context.Context) {
|
||||
WikiRevision(ctx)
|
||||
return
|
||||
case "_edit":
|
||||
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
||||
if !ctx.Repo.Permission.CanWrite(unit.TypeWiki) {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
EditWiki(ctx)
|
||||
return
|
||||
case "_new":
|
||||
if !ctx.Repo.CanWrite(unit.TypeWiki) {
|
||||
if !ctx.Repo.Permission.CanWrite(unit.TypeWiki) {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
@@ -506,7 +506,7 @@ func Wiki(ctx *context.Context) {
|
||||
|
||||
// WikiRevision renders file revision list of wiki page
|
||||
func WikiRevision(ctx *context.Context) {
|
||||
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
||||
ctx.Data["CanWriteWiki"] = ctx.Repo.Permission.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
||||
|
||||
if !repo_service.HasWiki(ctx, ctx.Repo.Repository) {
|
||||
ctx.Data["Title"] = ctx.Tr("repo.wiki")
|
||||
@@ -544,7 +544,7 @@ func WikiPages(ctx *context.Context) {
|
||||
}
|
||||
|
||||
ctx.Data["Title"] = ctx.Tr("repo.wiki.pages")
|
||||
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
||||
ctx.Data["CanWriteWiki"] = ctx.Repo.Permission.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived
|
||||
|
||||
_, commit, err := findWikiRepoCommit(ctx)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user