org.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 middleware
  5. import (
  6. "gopkg.in/macaron.v1"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/setting"
  9. )
  10. func HandleOrgAssignment(ctx *Context, args ...bool) {
  11. var (
  12. requireMember bool
  13. requireOwner bool
  14. requireAdminTeam bool
  15. )
  16. if len(args) >= 1 {
  17. requireMember = args[0]
  18. }
  19. if len(args) >= 2 {
  20. requireOwner = args[1]
  21. }
  22. if len(args) >= 3 {
  23. requireAdminTeam = args[2]
  24. }
  25. orgName := ctx.Params(":org")
  26. var err error
  27. ctx.Org.Organization, err = models.GetUserByName(orgName)
  28. if err != nil {
  29. if models.IsErrUserNotExist(err) {
  30. ctx.Handle(404, "GetUserByName", err)
  31. } else {
  32. ctx.Handle(500, "GetUserByName", err)
  33. }
  34. return
  35. }
  36. org := ctx.Org.Organization
  37. ctx.Data["Org"] = org
  38. // Force redirection when username is actually a user.
  39. if !org.IsOrganization() {
  40. ctx.Redirect("/" + org.Name)
  41. return
  42. }
  43. // Admin has super access.
  44. if ctx.IsSigned && ctx.User.IsAdmin {
  45. ctx.Org.IsOwner = true
  46. ctx.Org.IsMember = true
  47. ctx.Org.IsAdminTeam = true
  48. } else if ctx.IsSigned {
  49. ctx.Org.IsOwner = org.IsOwnedBy(ctx.User.Id)
  50. if ctx.Org.IsOwner {
  51. ctx.Org.IsMember = true
  52. ctx.Org.IsAdminTeam = true
  53. } else {
  54. if org.IsOrgMember(ctx.User.Id) {
  55. ctx.Org.IsMember = true
  56. }
  57. }
  58. } else {
  59. // Fake data.
  60. ctx.Data["SignedUser"] = &models.User{}
  61. }
  62. if (requireMember && !ctx.Org.IsMember) ||
  63. (requireOwner && !ctx.Org.IsOwner) {
  64. ctx.Handle(404, "OrgAssignment", err)
  65. return
  66. }
  67. ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
  68. ctx.Data["IsOrganizationMember"] = ctx.Org.IsMember
  69. ctx.Org.OrgLink = setting.AppSubUrl + "/org/" + org.Name
  70. ctx.Data["OrgLink"] = ctx.Org.OrgLink
  71. // Team.
  72. teamName := ctx.Params(":team")
  73. if len(teamName) > 0 {
  74. ctx.Org.Team, err = org.GetTeam(teamName)
  75. if err != nil {
  76. if err == models.ErrTeamNotExist {
  77. ctx.Handle(404, "GetTeam", err)
  78. } else {
  79. ctx.Handle(500, "GetTeam", err)
  80. }
  81. return
  82. }
  83. ctx.Data["Team"] = ctx.Org.Team
  84. ctx.Org.IsAdminTeam = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.ACCESS_MODE_ADMIN
  85. }
  86. ctx.Data["IsAdminTeam"] = ctx.Org.IsAdminTeam
  87. if requireAdminTeam && !ctx.Org.IsAdminTeam {
  88. ctx.Handle(404, "OrgAssignment", err)
  89. return
  90. }
  91. }
  92. func OrgAssignment(args ...bool) macaron.Handler {
  93. return func(ctx *Context) {
  94. HandleOrgAssignment(ctx, args...)
  95. }
  96. }