mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-24 03:21:49 +01:00
8eb1cd9264
Adds a feature [like GitHub has](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork) (step 7). If you create a new PR from a forked repo, you can select (and change later, but only if you are the PR creator/poster) the "Allow edits from maintainers" option. Then users with write access to the base branch get more permissions on this branch: * use the update pull request button * push directly from the command line (`git push`) * edit/delete/upload files via web UI * use related API endpoints You can't merge PRs to this branch with this enabled, you'll need "full" code write permissions. This feature has a pretty big impact on the permission system. I might forgot changing some things or didn't find security vulnerabilities. In this case, please leave a review or comment on this PR. Closes #17728 Co-authored-by: 6543 <6543@obermui.de>
109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package context
|
|
|
|
import (
|
|
"code.gitea.io/gitea/models/unit"
|
|
"code.gitea.io/gitea/modules/log"
|
|
)
|
|
|
|
// RequireRepoAdmin returns a middleware for requiring repository admin permission
|
|
func RequireRepoAdmin() func(ctx *Context) {
|
|
return func(ctx *Context) {
|
|
if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// RequireRepoWriter returns a middleware for requiring repository write to the specify unitType
|
|
func RequireRepoWriter(unitType unit.Type) func(ctx *Context) {
|
|
return func(ctx *Context) {
|
|
if !ctx.Repo.CanWrite(unitType) {
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// CanEnableEditor checks if the user is allowed to write to the branch of the repo
|
|
func CanEnableEditor() func(ctx *Context) {
|
|
return func(ctx *Context) {
|
|
if !ctx.Repo.Permission.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
|
|
ctx.NotFound("CanWriteToBranch denies permission", nil)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// RequireRepoWriterOr returns a middleware for requiring repository write to one of the unit permission
|
|
func RequireRepoWriterOr(unitTypes ...unit.Type) func(ctx *Context) {
|
|
return func(ctx *Context) {
|
|
for _, unitType := range unitTypes {
|
|
if ctx.Repo.CanWrite(unitType) {
|
|
return
|
|
}
|
|
}
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
}
|
|
}
|
|
|
|
// RequireRepoReader returns a middleware for requiring repository read to the specify unitType
|
|
func RequireRepoReader(unitType unit.Type) func(ctx *Context) {
|
|
return func(ctx *Context) {
|
|
if !ctx.Repo.CanRead(unitType) {
|
|
if log.IsTrace() {
|
|
if ctx.IsSigned {
|
|
log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+
|
|
"User in Repo has Permissions: %-+v",
|
|
ctx.Doer,
|
|
unitType,
|
|
ctx.Repo.Repository,
|
|
ctx.Repo.Permission)
|
|
} else {
|
|
log.Trace("Permission Denied: Anonymous user cannot read %-v in Repo %-v\n"+
|
|
"Anonymous user in Repo has Permissions: %-+v",
|
|
unitType,
|
|
ctx.Repo.Repository,
|
|
ctx.Repo.Permission)
|
|
}
|
|
}
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// RequireRepoReaderOr returns a middleware for requiring repository write to one of the unit permission
|
|
func RequireRepoReaderOr(unitTypes ...unit.Type) func(ctx *Context) {
|
|
return func(ctx *Context) {
|
|
for _, unitType := range unitTypes {
|
|
if ctx.Repo.CanRead(unitType) {
|
|
return
|
|
}
|
|
}
|
|
if log.IsTrace() {
|
|
var format string
|
|
var args []interface{}
|
|
if ctx.IsSigned {
|
|
format = "Permission Denied: User %-v cannot read ["
|
|
args = append(args, ctx.Doer)
|
|
} else {
|
|
format = "Permission Denied: Anonymous user cannot read ["
|
|
}
|
|
for _, unit := range unitTypes {
|
|
format += "%-v, "
|
|
args = append(args, unit)
|
|
}
|
|
|
|
format = format[:len(format)-2] + "] in Repo %-v\n" +
|
|
"User in Repo has Permissions: %-+v"
|
|
args = append(args, ctx.Repo.Repository, ctx.Repo.Permission)
|
|
log.Trace(format, args...)
|
|
}
|
|
ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
|
|
}
|
|
}
|