From 14f6e4cad0495d346fd0bcdfd04cc04839054602 Mon Sep 17 00:00:00 2001 From: Aiden Scandella Date: Fri, 29 Nov 2024 11:28:10 -0800 Subject: [PATCH] start on API perms --- routers/api/v1/api.go | 28 ++++++++++++++++++++++++++-- services/context/api.go | 12 ++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index f28ee980e1..e164e607c1 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -432,6 +432,18 @@ func reqRepoWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) { } } +// reqRepoCommitStatusWriter user should have a permission to write to commit +// statuses, or write to a repo, or be a site admin +func reqRepoCommitStatusWriter(unitTypes ...unit.Type) func(ctx *context.APIContext) { + return func(ctx *context.APIContext) { + // TODO + if !ctx.IsUserRepoWriter(unitTypes) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() { + ctx.Error(http.StatusForbidden, "reqRepoCommitStatusWriter", "user should have a permission to write to a repo") + return + } + } +} + // reqRepoBranchWriter user should have a permission to write to a branch, or be a site admin func reqRepoBranchWriter(ctx *context.APIContext) { options, ok := web.GetForm(ctx).(api.FileOptionInterface) @@ -451,6 +463,18 @@ func reqRepoReader(unitType unit.Type) func(ctx *context.APIContext) { } } +// reqRepoReader user should have specific commit status read permission, or +// repo read permission, or be a repo admin or a site admin +func reqRepoCommitStatusReader(unitType unit.Type) func(ctx *context.APIContext) { + return func(ctx *context.APIContext) { + // TODO + if !ctx.Repo.CanRead(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() { + ctx.Error(http.StatusForbidden, "reqRepoCommitStatusReader", "user should have specific read permission or be a repo admin or a site admin") + return + } + } +} + // reqAnyRepoReader user should have any permission to read repository or permissions of site admin func reqAnyRepoReader() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { @@ -1323,8 +1347,8 @@ func Routes() *web.Router { }, mustAllowPulls, reqRepoReader(unit.TypeCode), context.ReferencesGitRepo()) m.Group("/statuses", func() { m.Combo("/{sha}").Get(repo.GetCommitStatuses). - Post(reqToken(), reqRepoWriter(unit.TypeCode), bind(api.CreateStatusOption{}), repo.NewCommitStatus) - }, reqRepoReader(unit.TypeCode)) + Post(reqToken(), reqRepoCommitStatusWriter(unit.TypeCode), bind(api.CreateStatusOption{}), repo.NewCommitStatus) + }, reqRepoCommitStatusReader(unit.TypeCode)) m.Group("/commits", func() { m.Get("", context.ReferencesGitRepo(), repo.GetAllCommits) m.Group("/{ref}", func() { diff --git a/services/context/api.go b/services/context/api.go index b45e80a329..be8e7161e4 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -388,3 +388,15 @@ func (ctx *APIContext) IsUserRepoWriter(unitTypes []unit.Type) bool { return false } + +// IsUserRepoWriter returns true if current user has write commit status privilege in current repo +func (ctx *APIContext) IsUserCommitStatusWriter(unitTypes []unit.Type) bool { + for _, unitType := range unitTypes { + // TODO + if ctx.Repo.CanWrite(unitType) { + return true + } + } + + return false +}