org.go 3.3 KB

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