org.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Users.GetByUsername(c.Req.Context(), 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. c.Org.IsMember, _ = db.Organizations.HasMember(c.Req.Context(), org.ID, c.User.ID)
  67. }
  68. } else {
  69. // Fake data.
  70. c.Data["SignedUser"] = &db.User{}
  71. }
  72. if (requireMember && !c.Org.IsMember) ||
  73. (requireOwner && !c.Org.IsOwner) {
  74. c.NotFound()
  75. return
  76. }
  77. c.Data["IsOrganizationOwner"] = c.Org.IsOwner
  78. c.Data["IsOrganizationMember"] = c.Org.IsMember
  79. c.Org.OrgLink = conf.Server.Subpath + "/org/" + org.Name
  80. c.Data["OrgLink"] = c.Org.OrgLink
  81. // Team.
  82. if c.Org.IsMember {
  83. if c.Org.IsOwner {
  84. if err := org.GetTeams(); err != nil {
  85. c.Error(err, "get teams")
  86. return
  87. }
  88. } else {
  89. org.Teams, err = org.GetUserTeams(c.User.ID)
  90. if err != nil {
  91. c.Error(err, "get user teams")
  92. return
  93. }
  94. }
  95. }
  96. teamName := c.Params(":team")
  97. if len(teamName) > 0 {
  98. teamExists := false
  99. for _, team := range org.Teams {
  100. if team.LowerName == strings.ToLower(teamName) {
  101. teamExists = true
  102. c.Org.Team = team
  103. c.Org.IsTeamMember = true
  104. c.Data["Team"] = c.Org.Team
  105. break
  106. }
  107. }
  108. if !teamExists {
  109. c.NotFound()
  110. return
  111. }
  112. c.Data["IsTeamMember"] = c.Org.IsTeamMember
  113. if requireTeamMember && !c.Org.IsTeamMember {
  114. c.NotFound()
  115. return
  116. }
  117. c.Org.IsTeamAdmin = c.Org.Team.IsOwnerTeam() || c.Org.Team.Authorize >= db.AccessModeAdmin
  118. c.Data["IsTeamAdmin"] = c.Org.IsTeamAdmin
  119. if requireTeamAdmin && !c.Org.IsTeamAdmin {
  120. c.NotFound()
  121. return
  122. }
  123. }
  124. }
  125. func OrgAssignment(args ...bool) macaron.Handler {
  126. return func(c *Context) {
  127. HandleOrgAssignment(c, args...)
  128. }
  129. }