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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. macaron "gopkg.in/macaron.v1"
  8. )
  9. // RequireRepoAdmin returns a macaron middleware for requiring repository admin permission
  10. func RequireRepoAdmin() macaron.Handler {
  11. return func(ctx *Context) {
  12. if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
  13. ctx.NotFound(ctx.Req.RequestURI, nil)
  14. return
  15. }
  16. }
  17. }
  18. // RequireRepoWriter returns a macaron middleware for requiring repository write to the specify unitType
  19. func RequireRepoWriter(unitType models.UnitType) macaron.Handler {
  20. return func(ctx *Context) {
  21. if !ctx.Repo.CanWrite(unitType) {
  22. ctx.NotFound(ctx.Req.RequestURI, nil)
  23. return
  24. }
  25. }
  26. }
  27. // RequireRepoWriterOr returns a macaron middleware for requiring repository write to one of the unit permission
  28. func RequireRepoWriterOr(unitTypes ...models.UnitType) macaron.Handler {
  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.RequestURI, nil)
  36. }
  37. }
  38. // RequireRepoReader returns a macaron middleware for requiring repository read to the specify unitType
  39. func RequireRepoReader(unitType models.UnitType) macaron.Handler {
  40. return func(ctx *Context) {
  41. if !ctx.Repo.CanRead(unitType) {
  42. ctx.NotFound(ctx.Req.RequestURI, nil)
  43. return
  44. }
  45. }
  46. }
  47. // RequireRepoReaderOr returns a macaron middleware for requiring repository write to one of the unit permission
  48. func RequireRepoReaderOr(unitTypes ...models.UnitType) macaron.Handler {
  49. return func(ctx *Context) {
  50. for _, unitType := range unitTypes {
  51. if ctx.Repo.CanRead(unitType) {
  52. return
  53. }
  54. }
  55. ctx.NotFound(ctx.Req.RequestURI, nil)
  56. }
  57. }