org.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2014 The Gogs 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 middleware
  5. import (
  6. "github.com/Unknwon/macaron"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/log"
  9. "github.com/gogits/gogs/modules/setting"
  10. )
  11. func OrgAssignment(redirect bool, args ...bool) macaron.Handler {
  12. return func(ctx *Context) {
  13. var (
  14. requireMember bool
  15. requireOwner bool
  16. requireAdminTeam bool
  17. )
  18. if len(args) >= 1 {
  19. requireMember = args[0]
  20. }
  21. if len(args) >= 2 {
  22. requireOwner = args[1]
  23. }
  24. if len(args) >= 3 {
  25. requireAdminTeam = args[2]
  26. }
  27. orgName := ctx.Params(":org")
  28. var err error
  29. ctx.Org.Organization, err = models.GetUserByName(orgName)
  30. if err != nil {
  31. if err == models.ErrUserNotExist {
  32. ctx.Handle(404, "GetUserByName", err)
  33. } else if redirect {
  34. log.Error(4, "GetUserByName", err)
  35. ctx.Redirect(setting.AppSubUrl + "/")
  36. } else {
  37. ctx.Handle(500, "GetUserByName", err)
  38. }
  39. return
  40. }
  41. org := ctx.Org.Organization
  42. ctx.Data["Org"] = org
  43. if ctx.IsSigned {
  44. ctx.Org.IsOwner = org.IsOrgOwner(ctx.User.Id)
  45. if ctx.Org.IsOwner {
  46. ctx.Org.IsMember = true
  47. ctx.Org.IsAdminTeam = true
  48. } else {
  49. if org.IsOrgMember(ctx.User.Id) {
  50. ctx.Org.IsMember = true
  51. }
  52. }
  53. } else {
  54. // Fake data.
  55. ctx.Data["SignedUser"] = &models.User{}
  56. }
  57. if (requireMember && !ctx.Org.IsMember) ||
  58. (requireOwner && !ctx.Org.IsOwner) {
  59. ctx.Handle(404, "OrgAssignment", err)
  60. return
  61. }
  62. ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
  63. ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + org.Name
  64. ctx.Data["OrgLink"] = ctx.Org.OrgLink
  65. // Team.
  66. teamName := ctx.Params(":team")
  67. if len(teamName) > 0 {
  68. ctx.Org.Team, err = org.GetTeam(teamName)
  69. if err != nil {
  70. if err == models.ErrTeamNotExist {
  71. ctx.Handle(404, "GetTeam", err)
  72. } else if redirect {
  73. log.Error(4, "GetTeam", err)
  74. ctx.Redirect(setting.AppSubUrl + "/")
  75. } else {
  76. ctx.Handle(500, "GetTeam", err)
  77. }
  78. return
  79. }
  80. ctx.Data["Team"] = ctx.Org.Team
  81. ctx.Org.IsAdminTeam = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize == models.ORG_ADMIN
  82. }
  83. ctx.Data["IsAdminTeam"] = ctx.Org.IsAdminTeam
  84. if requireAdminTeam && !ctx.Org.IsAdminTeam {
  85. ctx.Handle(404, "OrgAssignment", err)
  86. return
  87. }
  88. }
  89. }