org.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. org.Teams, err = org.GetUserTeams(ctx.User.ID)
  96. if err != nil {
  97. ctx.Handle(500, "GetUserTeams", err)
  98. return
  99. }
  100. }
  101. }
  102. teamName := ctx.Params(":team")
  103. if len(teamName) > 0 {
  104. teamExists := false
  105. for _, team := range org.Teams {
  106. if team.LowerName == strings.ToLower(teamName) {
  107. teamExists = true
  108. ctx.Org.Team = team
  109. ctx.Org.IsTeamMember = true
  110. ctx.Data["Team"] = ctx.Org.Team
  111. break
  112. }
  113. }
  114. if !teamExists {
  115. ctx.Handle(404, "OrgAssignment", err)
  116. return
  117. }
  118. ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
  119. if requireTeamMember && !ctx.Org.IsTeamMember {
  120. ctx.Handle(404, "OrgAssignment", err)
  121. return
  122. }
  123. ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.ACCESS_MODE_ADMIN
  124. ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
  125. if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
  126. ctx.Handle(404, "OrgAssignment", err)
  127. return
  128. }
  129. }
  130. }
  131. func OrgAssignment(args ...bool) macaron.Handler {
  132. return func(ctx *Context) {
  133. HandleOrgAssignment(ctx, args...)
  134. }
  135. }