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.

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