org.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/db"
  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 *db.User
  17. OrgLink string
  18. Team *db.Team
  19. }
  20. func HandleOrgAssignment(c *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 := c.Params(":org")
  40. var err error
  41. c.Org.Organization, err = db.GetUserByName(orgName)
  42. if err != nil {
  43. c.NotFoundOrError(err, "get organization by name")
  44. return
  45. }
  46. org := c.Org.Organization
  47. c.Data["Org"] = org
  48. // Force redirection when username is actually a user.
  49. if !org.IsOrganization() {
  50. c.Redirect("/" + org.Name)
  51. return
  52. }
  53. // Admin has super access.
  54. if c.IsLogged && c.User.IsAdmin {
  55. c.Org.IsOwner = true
  56. c.Org.IsMember = true
  57. c.Org.IsTeamMember = true
  58. c.Org.IsTeamAdmin = true
  59. } else if c.IsLogged {
  60. c.Org.IsOwner = org.IsOwnedBy(c.User.ID)
  61. if c.Org.IsOwner {
  62. c.Org.IsMember = true
  63. c.Org.IsTeamMember = true
  64. c.Org.IsTeamAdmin = true
  65. } else {
  66. if org.IsOrgMember(c.User.ID) {
  67. c.Org.IsMember = true
  68. }
  69. }
  70. } else {
  71. // Fake data.
  72. c.Data["SignedUser"] = &db.User{}
  73. }
  74. if (requireMember && !c.Org.IsMember) ||
  75. (requireOwner && !c.Org.IsOwner) {
  76. c.NotFound()
  77. return
  78. }
  79. c.Data["IsOrganizationOwner"] = c.Org.IsOwner
  80. c.Data["IsOrganizationMember"] = c.Org.IsMember
  81. c.Org.OrgLink = conf.Server.Subpath + "/org/" + org.Name
  82. c.Data["OrgLink"] = c.Org.OrgLink
  83. // Team.
  84. if c.Org.IsMember {
  85. if c.Org.IsOwner {
  86. if err := org.GetTeams(); err != nil {
  87. c.Error(err, "get teams")
  88. return
  89. }
  90. } else {
  91. org.Teams, err = org.GetUserTeams(c.User.ID)
  92. if err != nil {
  93. c.Error(err, "get user teams")
  94. return
  95. }
  96. }
  97. }
  98. teamName := c.Params(":team")
  99. if len(teamName) > 0 {
  100. teamExists := false
  101. for _, team := range org.Teams {
  102. if team.LowerName == strings.ToLower(teamName) {
  103. teamExists = true
  104. c.Org.Team = team
  105. c.Org.IsTeamMember = true
  106. c.Data["Team"] = c.Org.Team
  107. break
  108. }
  109. }
  110. if !teamExists {
  111. c.NotFound()
  112. return
  113. }
  114. c.Data["IsTeamMember"] = c.Org.IsTeamMember
  115. if requireTeamMember && !c.Org.IsTeamMember {
  116. c.NotFound()
  117. return
  118. }
  119. c.Org.IsTeamAdmin = c.Org.Team.IsOwnerTeam() || c.Org.Team.Authorize >= db.ACCESS_MODE_ADMIN
  120. c.Data["IsTeamAdmin"] = c.Org.IsTeamAdmin
  121. if requireTeamAdmin && !c.Org.IsTeamAdmin {
  122. c.NotFound()
  123. return
  124. }
  125. }
  126. }
  127. func OrgAssignment(args ...bool) macaron.Handler {
  128. return func(c *Context) {
  129. HandleOrgAssignment(c, args...)
  130. }
  131. }