gitea源码

permission.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "net/http"
  6. "slices"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/models/unit"
  10. )
  11. // RequireRepoAdmin returns a middleware for requiring repository admin permission
  12. func RequireRepoAdmin() func(ctx *Context) {
  13. return func(ctx *Context) {
  14. if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
  15. ctx.NotFound(nil)
  16. return
  17. }
  18. }
  19. }
  20. // CanWriteToBranch checks if the user is allowed to write to the branch of the repo
  21. func CanWriteToBranch() func(ctx *Context) {
  22. return func(ctx *Context) {
  23. if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) {
  24. ctx.NotFound(nil)
  25. return
  26. }
  27. }
  28. }
  29. // RequireUnitWriter returns a middleware for requiring repository write to one of the unit permission
  30. func RequireUnitWriter(unitTypes ...unit.Type) func(ctx *Context) {
  31. return func(ctx *Context) {
  32. if slices.ContainsFunc(unitTypes, ctx.Repo.CanWrite) {
  33. return
  34. }
  35. ctx.NotFound(nil)
  36. }
  37. }
  38. // RequireUnitReader returns a middleware for requiring repository write to one of the unit permission
  39. func RequireUnitReader(unitTypes ...unit.Type) func(ctx *Context) {
  40. return func(ctx *Context) {
  41. for _, unitType := range unitTypes {
  42. if ctx.Repo.CanRead(unitType) {
  43. return
  44. }
  45. if unitType == unit.TypeCode && canWriteAsMaintainer(ctx) {
  46. return
  47. }
  48. }
  49. ctx.NotFound(nil)
  50. }
  51. }
  52. // CheckRepoScopedToken check whether personal access token has repo scope
  53. func CheckRepoScopedToken(ctx *Context, repo *repo_model.Repository, level auth_model.AccessTokenScopeLevel) {
  54. if !ctx.IsBasicAuth || ctx.Data["IsApiToken"] != true {
  55. return
  56. }
  57. scope, ok := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope)
  58. if ok { // it's a personal access token but not oauth2 token
  59. var scopeMatched bool
  60. requiredScopes := auth_model.GetRequiredScopes(level, auth_model.AccessTokenScopeCategoryRepository)
  61. // check if scope only applies to public resources
  62. publicOnly, err := scope.PublicOnly()
  63. if err != nil {
  64. ctx.ServerError("HasScope", err)
  65. return
  66. }
  67. if publicOnly && repo.IsPrivate {
  68. ctx.HTTPError(http.StatusForbidden)
  69. return
  70. }
  71. scopeMatched, err = scope.HasScope(requiredScopes...)
  72. if err != nil {
  73. ctx.ServerError("HasScope", err)
  74. return
  75. }
  76. if !scopeMatched {
  77. ctx.HTTPError(http.StatusForbidden)
  78. return
  79. }
  80. }
  81. }