本站源代码
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

164 Zeilen
3.7KB

  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. "gitea.com/macaron/macaron"
  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.NotFound("GetUserByName", err)
  47. } else {
  48. ctx.ServerError("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(setting.AppSubURL + "/" + 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, err = org.IsOwnedBy(ctx.User.ID)
  67. if err != nil {
  68. ctx.ServerError("IsOwnedBy", err)
  69. return
  70. }
  71. if ctx.Org.IsOwner {
  72. ctx.Org.IsMember = true
  73. ctx.Org.IsTeamMember = true
  74. ctx.Org.IsTeamAdmin = true
  75. } else {
  76. ctx.Org.IsMember, err = org.IsOrgMember(ctx.User.ID)
  77. if err != nil {
  78. ctx.ServerError("IsOrgMember", err)
  79. return
  80. }
  81. }
  82. } else {
  83. // Fake data.
  84. ctx.Data["SignedUser"] = &models.User{}
  85. }
  86. if (requireMember && !ctx.Org.IsMember) ||
  87. (requireOwner && !ctx.Org.IsOwner) {
  88. ctx.NotFound("OrgAssignment", err)
  89. return
  90. }
  91. ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
  92. ctx.Data["IsOrganizationMember"] = ctx.Org.IsMember
  93. ctx.Org.OrgLink = setting.AppSubURL + "/org/" + org.Name
  94. ctx.Data["OrgLink"] = ctx.Org.OrgLink
  95. // Team.
  96. if ctx.Org.IsMember {
  97. if ctx.Org.IsOwner {
  98. if err := org.GetTeams(); err != nil {
  99. ctx.ServerError("GetTeams", err)
  100. return
  101. }
  102. } else {
  103. org.Teams, err = org.GetUserTeams(ctx.User.ID)
  104. if err != nil {
  105. ctx.ServerError("GetUserTeams", err)
  106. return
  107. }
  108. }
  109. }
  110. teamName := ctx.Params(":team")
  111. if len(teamName) > 0 {
  112. teamExists := false
  113. for _, team := range org.Teams {
  114. if team.LowerName == strings.ToLower(teamName) {
  115. teamExists = true
  116. ctx.Org.Team = team
  117. ctx.Org.IsTeamMember = true
  118. ctx.Data["Team"] = ctx.Org.Team
  119. break
  120. }
  121. }
  122. if !teamExists {
  123. ctx.NotFound("OrgAssignment", err)
  124. return
  125. }
  126. ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
  127. if requireTeamMember && !ctx.Org.IsTeamMember {
  128. ctx.NotFound("OrgAssignment", err)
  129. return
  130. }
  131. ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.AccessModeAdmin
  132. ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
  133. if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
  134. ctx.NotFound("OrgAssignment", err)
  135. return
  136. }
  137. }
  138. }
  139. // OrgAssignment returns a macaron middleware to handle organization assignment
  140. func OrgAssignment(args ...bool) macaron.Handler {
  141. return func(ctx *Context) {
  142. HandleOrgAssignment(ctx, args...)
  143. }
  144. }
上海开阖软件有限公司 沪ICP备12045867号-1