org.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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(c *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 := c.Params(":org")
  41. var err error
  42. c.Org.Organization, err = models.GetUserByName(orgName)
  43. if err != nil {
  44. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  45. return
  46. }
  47. org := c.Org.Organization
  48. c.Data["Org"] = org
  49. // Force redirection when username is actually a user.
  50. if !org.IsOrganization() {
  51. c.Redirect("/" + org.Name)
  52. return
  53. }
  54. // Admin has super access.
  55. if c.IsLogged && c.User.IsAdmin {
  56. c.Org.IsOwner = true
  57. c.Org.IsMember = true
  58. c.Org.IsTeamMember = true
  59. c.Org.IsTeamAdmin = true
  60. } else if c.IsLogged {
  61. c.Org.IsOwner = org.IsOwnedBy(c.User.ID)
  62. if c.Org.IsOwner {
  63. c.Org.IsMember = true
  64. c.Org.IsTeamMember = true
  65. c.Org.IsTeamAdmin = true
  66. } else {
  67. if org.IsOrgMember(c.User.ID) {
  68. c.Org.IsMember = true
  69. }
  70. }
  71. } else {
  72. // Fake data.
  73. c.Data["SignedUser"] = &models.User{}
  74. }
  75. if (requireMember && !c.Org.IsMember) ||
  76. (requireOwner && !c.Org.IsOwner) {
  77. c.Handle(404, "OrgAssignment", err)
  78. return
  79. }
  80. c.Data["IsOrganizationOwner"] = c.Org.IsOwner
  81. c.Data["IsOrganizationMember"] = c.Org.IsMember
  82. c.Org.OrgLink = setting.AppSubURL + "/org/" + org.Name
  83. c.Data["OrgLink"] = c.Org.OrgLink
  84. // Team.
  85. if c.Org.IsMember {
  86. if c.Org.IsOwner {
  87. if err := org.GetTeams(); err != nil {
  88. c.Handle(500, "GetTeams", err)
  89. return
  90. }
  91. } else {
  92. org.Teams, err = org.GetUserTeams(c.User.ID)
  93. if err != nil {
  94. c.Handle(500, "GetUserTeams", err)
  95. return
  96. }
  97. }
  98. }
  99. teamName := c.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. c.Org.Team = team
  106. c.Org.IsTeamMember = true
  107. c.Data["Team"] = c.Org.Team
  108. break
  109. }
  110. }
  111. if !teamExists {
  112. c.Handle(404, "OrgAssignment", err)
  113. return
  114. }
  115. c.Data["IsTeamMember"] = c.Org.IsTeamMember
  116. if requireTeamMember && !c.Org.IsTeamMember {
  117. c.Handle(404, "OrgAssignment", err)
  118. return
  119. }
  120. c.Org.IsTeamAdmin = c.Org.Team.IsOwnerTeam() || c.Org.Team.Authorize >= models.ACCESS_MODE_ADMIN
  121. c.Data["IsTeamAdmin"] = c.Org.IsTeamAdmin
  122. if requireTeamAdmin && !c.Org.IsTeamAdmin {
  123. c.Handle(404, "OrgAssignment", err)
  124. return
  125. }
  126. }
  127. }
  128. func OrgAssignment(args ...bool) macaron.Handler {
  129. return func(c *Context) {
  130. HandleOrgAssignment(c, args...)
  131. }
  132. }