Skip to content

Commit ee9de20

Browse files
update context assignment functions for repositories and repo groups
assign group and show 404 if necessary when `group_id` parameter is present
1 parent a3a6b8a commit ee9de20

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

services/context/group.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,38 @@ type GroupAssignmentOptions struct {
9090
RequireGroupAdmin bool
9191
}
9292

93+
func groupAssignment(ctx *Context) {
94+
if ctx.RepoGroup.Group == nil {
95+
GetGroupByParams(ctx)
96+
}
97+
if ctx.Written() {
98+
return
99+
}
100+
canAccess, err := ctx.RepoGroup.Group.CanAccess(ctx, ctx.Doer)
101+
if err != nil {
102+
ctx.ServerError("error checking group access", err)
103+
return
104+
}
105+
if !canAccess {
106+
ctx.NotFound(nil)
107+
return
108+
}
109+
}
110+
93111
func GroupAssignment(args GroupAssignmentOptions) func(ctx *Context) {
94112
return func(ctx *Context) {
95113
var err error
96114

97-
if ctx.RepoGroup.Group == nil {
98-
GetGroupByParams(ctx)
99-
if ctx.Written() {
100-
return
101-
}
115+
groupAssignment(ctx)
116+
if ctx.Written() {
117+
return
102118
}
103119

104120
group := ctx.RepoGroup.Group
105121
if ctx.RepoGroup.Group.Visibility != structs.VisibleTypePublic && !ctx.IsSigned {
106122
ctx.NotFound(err)
107123
return
108124
}
109-
canAccess, err := ctx.RepoGroup.Group.CanAccess(ctx, ctx.Doer)
110-
if err != nil {
111-
ctx.ServerError("error checking group access", err)
112-
return
113-
}
114-
if !canAccess {
115-
ctx.NotFound(nil)
116-
return
117-
}
118125

119126
if ctx.RepoGroup.Group.Visibility == structs.VisibleTypePrivate {
120127
args.RequireMember = true

services/context/permission.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func CanWriteToBranch() func(ctx *Context) {
3535
// RequireUnitWriter returns a middleware for requiring repository write to one of the unit permission
3636
func RequireUnitWriter(unitTypes ...unit.Type) func(ctx *Context) {
3737
return func(ctx *Context) {
38-
if slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite) {
38+
if slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite) || (ctx.RepoGroup != nil && slices.ContainsFunc(unitTypes, func(u unit.Type) bool {
39+
return ctx.RepoGroup.CanWriteUnit(ctx, u)
40+
})) {
3941
return
4042
}
4143
ctx.NotFound(nil)
@@ -46,7 +48,7 @@ func RequireUnitWriter(unitTypes ...unit.Type) func(ctx *Context) {
4648
func RequireUnitReader(unitTypes ...unit.Type) func(ctx *Context) {
4749
return func(ctx *Context) {
4850
for _, unitType := range unitTypes {
49-
if ctx.Repo.CanRead(unitType) {
51+
if ctx.Repo.CanRead(unitType) || ctx.RepoGroup.CanReadUnit(ctx, unitType) {
5052
return
5153
}
5254
if unitType == unit.TypeCode && canWriteAsMaintainer(ctx) {

services/context/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ func RepoAssignment(ctx *Context) {
525525
if repo.GroupID != gid {
526526
ctx.NotFound(nil)
527527
}
528+
if gid > 0 {
529+
groupAssignment(ctx)
530+
}
531+
if ctx.Written() {
532+
return
533+
}
528534
repo.Owner = ctx.Repo.Owner
529535

530536
repoAssignment(ctx, repo)

0 commit comments

Comments
 (0)