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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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"
  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 models.UnitType) 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. // RequireRepoWriterOr returns a middleware for requiring repository write to one of the unit permission
  28. func RequireRepoWriterOr(unitTypes ...models.UnitType) func(ctx *Context) {
  29. return func(ctx *Context) {
  30. for _, unitType := range unitTypes {
  31. if ctx.Repo.CanWrite(unitType) {
  32. return
  33. }
  34. }
  35. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  36. }
  37. }
  38. // RequireRepoReader returns a middleware for requiring repository read to the specify unitType
  39. func RequireRepoReader(unitType models.UnitType) func(ctx *Context) {
  40. return func(ctx *Context) {
  41. if !ctx.Repo.CanRead(unitType) {
  42. if log.IsTrace() {
  43. if ctx.IsSigned {
  44. log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+
  45. "User in Repo has Permissions: %-+v",
  46. ctx.User,
  47. unitType,
  48. ctx.Repo.Repository,
  49. ctx.Repo.Permission)
  50. } else {
  51. log.Trace("Permission Denied: Anonymous user cannot read %-v in Repo %-v\n"+
  52. "Anonymous user in Repo has Permissions: %-+v",
  53. unitType,
  54. ctx.Repo.Repository,
  55. ctx.Repo.Permission)
  56. }
  57. }
  58. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  59. return
  60. }
  61. }
  62. }
  63. // RequireRepoReaderOr returns a middleware for requiring repository write to one of the unit permission
  64. func RequireRepoReaderOr(unitTypes ...models.UnitType) func(ctx *Context) {
  65. return func(ctx *Context) {
  66. for _, unitType := range unitTypes {
  67. if ctx.Repo.CanRead(unitType) {
  68. return
  69. }
  70. }
  71. if log.IsTrace() {
  72. var format string
  73. var args []interface{}
  74. if ctx.IsSigned {
  75. format = "Permission Denied: User %-v cannot read ["
  76. args = append(args, ctx.User)
  77. } else {
  78. format = "Permission Denied: Anonymous user cannot read ["
  79. }
  80. for _, unit := range unitTypes {
  81. format += "%-v, "
  82. args = append(args, unit)
  83. }
  84. format = format[:len(format)-2] + "] in Repo %-v\n" +
  85. "User in Repo has Permissions: %-+v"
  86. args = append(args, ctx.Repo.Repository, ctx.Repo.Permission)
  87. log.Trace(format, args...)
  88. }
  89. ctx.NotFound(ctx.Req.URL.RequestURI(), nil)
  90. }
  91. }