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.

155 lines
3.5 KiB

8 years ago
  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 context
  5. import (
  6. "strings"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/setting"
  9. macaron "gopkg.in/macaron.v1"
  10. )
  11. // Organization contains organization context
  12. type Organization struct {
  13. IsOwner bool
  14. IsMember bool
  15. IsTeamMember bool // Is member of team.
  16. IsTeamAdmin bool // In owner team or team that has admin permission level.
  17. Organization *models.User
  18. OrgLink string
  19. Team *models.Team
  20. }
  21. // HandleOrgAssignment handles organization assignment
  22. func HandleOrgAssignment(ctx *Context, args ...bool) {
  23. var (
  24. requireMember bool
  25. requireOwner bool
  26. requireTeamMember bool
  27. requireTeamAdmin bool
  28. )
  29. if len(args) >= 1 {
  30. requireMember = args[0]
  31. }
  32. if len(args) >= 2 {
  33. requireOwner = args[1]
  34. }
  35. if len(args) >= 3 {
  36. requireTeamMember = args[2]
  37. }
  38. if len(args) >= 4 {
  39. requireTeamAdmin = args[3]
  40. }
  41. orgName := ctx.Params(":org")
  42. var err error
  43. ctx.Org.Organization, err = models.GetUserByName(orgName)
  44. if err != nil {
  45. if models.IsErrUserNotExist(err) {
  46. ctx.Handle(404, "GetUserByName", err)
  47. } else {
  48. ctx.Handle(500, "GetUserByName", err)
  49. }
  50. return
  51. }
  52. org := ctx.Org.Organization
  53. ctx.Data["Org"] = org
  54. // Force redirection when username is actually a user.
  55. if !org.IsOrganization() {
  56. ctx.Redirect("/" + org.Name)
  57. return
  58. }
  59. // Admin has super access.
  60. if ctx.IsSigned && ctx.User.IsAdmin {
  61. ctx.Org.IsOwner = true
  62. ctx.Org.IsMember = true
  63. ctx.Org.IsTeamMember = true
  64. ctx.Org.IsTeamAdmin = true
  65. } else if ctx.IsSigned {
  66. ctx.Org.IsOwner = org.IsOwnedBy(ctx.User.ID)
  67. if ctx.Org.IsOwner {
  68. ctx.Org.IsMember = true
  69. ctx.Org.IsTeamMember = true
  70. ctx.Org.IsTeamAdmin = true
  71. } else {
  72. if org.IsOrgMember(ctx.User.ID) {
  73. ctx.Org.IsMember = true
  74. }
  75. }
  76. } else {
  77. // Fake data.
  78. ctx.Data["SignedUser"] = &models.User{}
  79. }
  80. if (requireMember && !ctx.Org.IsMember) ||
  81. (requireOwner && !ctx.Org.IsOwner) {
  82. ctx.Handle(404, "OrgAssignment", err)
  83. return
  84. }
  85. ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
  86. ctx.Data["IsOrganizationMember"] = ctx.Org.IsMember
  87. ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + org.Name
  88. ctx.Data["OrgLink"] = ctx.Org.OrgLink
  89. // Team.
  90. if ctx.Org.IsMember {
  91. if ctx.Org.IsOwner {
  92. if err := org.GetTeams(); err != nil {
  93. ctx.Handle(500, "GetTeams", err)
  94. return
  95. }
  96. } else {
  97. org.Teams, err = org.GetUserTeams(ctx.User.ID)
  98. if err != nil {
  99. ctx.Handle(500, "GetUserTeams", err)
  100. return
  101. }
  102. }
  103. }
  104. teamName := ctx.Params(":team")
  105. if len(teamName) > 0 {
  106. teamExists := false
  107. for _, team := range org.Teams {
  108. if team.LowerName == strings.ToLower(teamName) {
  109. teamExists = true
  110. ctx.Org.Team = team
  111. ctx.Org.IsTeamMember = true
  112. ctx.Data["Team"] = ctx.Org.Team
  113. break
  114. }
  115. }
  116. if !teamExists {
  117. ctx.Handle(404, "OrgAssignment", err)
  118. return
  119. }
  120. ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
  121. if requireTeamMember && !ctx.Org.IsTeamMember {
  122. ctx.Handle(404, "OrgAssignment", err)
  123. return
  124. }
  125. ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.AccessModeAdmin
  126. ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
  127. if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
  128. ctx.Handle(404, "OrgAssignment", err)
  129. return
  130. }
  131. }
  132. }
  133. // OrgAssignment returns a macaron middleware to handle organization assignment
  134. func OrgAssignment(args ...bool) macaron.Handler {
  135. return func(ctx *Context) {
  136. HandleOrgAssignment(ctx, args...)
  137. }
  138. }