org.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "strings"
  7. "gopkg.in/macaron.v1"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/setting"
  10. )
  11. func HandleOrgAssignment(ctx *Context, args ...bool) {
  12. var (
  13. requireMember bool
  14. requireOwner bool
  15. requireTeamMember bool
  16. requireTeamAdmin 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. requireTeamMember = args[2]
  26. }
  27. if len(args) >= 4 {
  28. requireTeamAdmin = args[3]
  29. }
  30. orgName := ctx.Params(":org")
  31. var err error
  32. ctx.Org.Organization, err = models.GetUserByName(orgName)
  33. if err != nil {
  34. if models.IsErrUserNotExist(err) {
  35. ctx.Handle(404, "GetUserByName", err)
  36. } else {
  37. ctx.Handle(500, "GetUserByName", err)
  38. }
  39. return
  40. }
  41. org := ctx.Org.Organization
  42. ctx.Data["Org"] = org
  43. // Force redirection when username is actually a user.
  44. if !org.IsOrganization() {
  45. ctx.Redirect("/" + org.Name)
  46. return
  47. }
  48. // Admin has super access.
  49. if ctx.IsSigned && ctx.User.IsAdmin {
  50. ctx.Org.IsOwner = true
  51. ctx.Org.IsMember = true
  52. ctx.Org.IsTeamMember = true
  53. ctx.Org.IsTeamAdmin = true
  54. } else if ctx.IsSigned {
  55. ctx.Org.IsOwner = org.IsOwnedBy(ctx.User.Id)
  56. if ctx.Org.IsOwner {
  57. ctx.Org.IsMember = true
  58. ctx.Org.IsTeamMember = true
  59. ctx.Org.IsTeamAdmin = true
  60. } else {
  61. if org.IsOrgMember(ctx.User.Id) {
  62. ctx.Org.IsMember = true
  63. }
  64. }
  65. } else {
  66. // Fake data.
  67. ctx.Data["SignedUser"] = &models.User{}
  68. }
  69. if (requireMember && !ctx.Org.IsMember) ||
  70. (requireOwner && !ctx.Org.IsOwner) {
  71. ctx.Handle(404, "OrgAssignment", err)
  72. return
  73. }
  74. ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
  75. ctx.Data["IsOrganizationMember"] = ctx.Org.IsMember
  76. ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + org.Name
  77. ctx.Data["OrgLink"] = ctx.Org.OrgLink
  78. // Team.
  79. if ctx.Org.IsMember {
  80. if ctx.Org.IsOwner {
  81. if err := org.GetTeams(); err != nil {
  82. ctx.Handle(500, "GetUserTeams", err)
  83. return
  84. }
  85. } else {
  86. if err := org.GetUserTeams(ctx.User.Id); err != nil {
  87. ctx.Handle(500, "GetUserTeams", err)
  88. return
  89. }
  90. }
  91. }
  92. teamName := ctx.Params(":team")
  93. if len(teamName) > 0 {
  94. teamExists := false
  95. for _, team := range org.Teams {
  96. if team.LowerName == strings.ToLower(teamName) {
  97. teamExists = true
  98. ctx.Org.Team = team
  99. ctx.Org.IsTeamMember = true
  100. ctx.Data["Team"] = ctx.Org.Team
  101. break
  102. }
  103. }
  104. if !teamExists {
  105. ctx.Handle(404, "OrgAssignment", err)
  106. return
  107. }
  108. ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
  109. if requireTeamMember && !ctx.Org.IsTeamMember {
  110. ctx.Handle(404, "OrgAssignment", err)
  111. return
  112. }
  113. ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.ACCESS_MODE_ADMIN
  114. ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
  115. if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
  116. ctx.Handle(404, "OrgAssignment", err)
  117. return
  118. }
  119. }
  120. }
  121. func OrgAssignment(args ...bool) macaron.Handler {
  122. return func(ctx *Context) {
  123. HandleOrgAssignment(ctx, args...)
  124. }
  125. }