You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

permission.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "code.gitea.io/gitea/models/unit"
  7. "code.gitea.io/gitea/modules/log"
  8. )
  9. // RequireRepoAdmin returns a middleware for requiring repository admin permission
  10. func RequireRepoAdmin() func(ctx *Context) {
  11. return func(ctx *Context) {
  12. if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
  13. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  14. return
  15. }
  16. }
  17. }
  18. // RequireRepoWriter returns a middleware for requiring repository write to the specify unitType
  19. func RequireRepoWriter(unitType unit.Type) func(ctx *Context) {
  20. return func(ctx *Context) {
  21. if !ctx.Repo.CanWrite(unitType) {
  22. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  23. return
  24. }
  25. }
  26. }
  27. // CanEnableEditor checks if the user is allowed to write to the branch of the repo
  28. func CanEnableEditor() func(ctx *Context) {
  29. return func(ctx *Context) {
  30. if !ctx.Repo.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
  31. ctx.NotFound("CanWriteToBranch denies permission", nil)
  32. return
  33. }
  34. }
  35. }
  36. // RequireRepoWriterOr returns a middleware for requiring repository write to one of the unit permission
  37. func RequireRepoWriterOr(unitTypes ...unit.Type) func(ctx *Context) {
  38. return func(ctx *Context) {
  39. for _, unitType := range unitTypes {
  40. if ctx.Repo.CanWrite(unitType) {
  41. return
  42. }
  43. }
  44. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  45. }
  46. }
  47. // RequireRepoReader returns a middleware for requiring repository read to the specify unitType
  48. func RequireRepoReader(unitType unit.Type) func(ctx *Context) {
  49. return func(ctx *Context) {
  50. if !ctx.Repo.CanRead(unitType) {
  51. if log.IsTrace() {
  52. if ctx.IsSigned {
  53. log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+
  54. "User in Repo has Permissions: %-+v",
  55. ctx.Doer,
  56. unitType,
  57. ctx.Repo.Repository,
  58. ctx.Repo.Permission)
  59. } else {
  60. log.Trace("Permission Denied: Anonymous user cannot read %-v in Repo %-v\n"+
  61. "Anonymous user in Repo has Permissions: %-+v",
  62. unitType,
  63. ctx.Repo.Repository,
  64. ctx.Repo.Permission)
  65. }
  66. }
  67. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  68. return
  69. }
  70. }
  71. }
  72. // RequireRepoReaderOr returns a middleware for requiring repository write to one of the unit permission
  73. func RequireRepoReaderOr(unitTypes ...unit.Type) func(ctx *Context) {
  74. return func(ctx *Context) {
  75. for _, unitType := range unitTypes {
  76. if ctx.Repo.CanRead(unitType) {
  77. return
  78. }
  79. }
  80. if log.IsTrace() {
  81. var format string
  82. var args []interface{}
  83. if ctx.IsSigned {
  84. format = "Permission Denied: User %-v cannot read ["
  85. args = append(args, ctx.Doer)
  86. } else {
  87. format = "Permission Denied: Anonymous user cannot read ["
  88. }
  89. for _, unit := range unitTypes {
  90. format += "%-v, "
  91. args = append(args, unit)
  92. }
  93. format = format[:len(format)-2] + "] in Repo %-v\n" +
  94. "User in Repo has Permissions: %-+v"
  95. args = append(args, ctx.Repo.Repository, ctx.Repo.Permission)
  96. log.Trace(format, args...)
  97. }
  98. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  99. }
  100. }