diff options
author | Vasek Sraier <vakabus@users.noreply.github.com> | 2019-04-07 22:49:34 +0000 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-04-08 01:49:34 +0300 |
commit | 49b2f45f75960a48676c8dd2555d715da1942bd7 (patch) | |
tree | f9b42a22fd8c940783b271464bb144bf512a82d6 /modules/context/context.go | |
parent | 592e6c398e6c05776d09822e8663928a947c5b90 (diff) | |
download | gitea-49b2f45f75960a48676c8dd2555d715da1942bd7.tar.gz gitea-49b2f45f75960a48676c8dd2555d715da1942bd7.zip |
Cleaned permission checks for API -> site admin can now do anything (#6483)
* cleaned permission checks for API -> site admin can now do anything
Signed-off-by: Vasek Sraier <git@vakabus.cz>
* PR #6483: helper methods moved to context/context.go, added missing return
Signed-off-by: Vasek Sraier <git@vakabus.cz>
* PR #6483: added documentation to new exported helper functions in context/context.go
Signed-off-by: Vasek Sraier <git@vakabus.cz>
Diffstat (limited to 'modules/context/context.go')
-rw-r--r-- | modules/context/context.go | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/modules/context/context.go b/modules/context/context.go index 770d42e2c7..07e2b7b61f 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -25,7 +25,7 @@ import ( "github.com/go-macaron/csrf" "github.com/go-macaron/i18n" "github.com/go-macaron/session" - macaron "gopkg.in/macaron.v1" + "gopkg.in/macaron.v1" ) // Context represents context of a request. @@ -46,6 +46,42 @@ type Context struct { Org *Organization } +// IsUserSiteAdmin returns true if current user is a site admin +func (ctx *Context) IsUserSiteAdmin() bool { + return ctx.IsSigned && ctx.User.IsAdmin +} + +// IsUserRepoOwner returns true if current user owns current repo +func (ctx *Context) IsUserRepoOwner() bool { + return ctx.Repo.IsOwner() +} + +// IsUserRepoAdmin returns true if current user is admin in current repo +func (ctx *Context) IsUserRepoAdmin() bool { + return ctx.Repo.IsAdmin() +} + +// IsUserRepoWriter returns true if current user has write privilege in current repo +func (ctx *Context) IsUserRepoWriter(unitTypes []models.UnitType) bool { + for _, unitType := range unitTypes { + if ctx.Repo.CanWrite(unitType) { + return true + } + } + + return false +} + +// IsUserRepoReaderSpecific returns true if current user can read current repo's specific part +func (ctx *Context) IsUserRepoReaderSpecific(unitType models.UnitType) bool { + return ctx.Repo.CanRead(unitType) +} + +// IsUserRepoReaderAny returns true if current user can read any part of current repo +func (ctx *Context) IsUserRepoReaderAny() bool { + return ctx.Repo.HasAccess() +} + // HasAPIError returns true if error occurs in form validation. func (ctx *Context) HasAPIError() bool { hasErr, ok := ctx.Data["HasError"] |