You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

  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. "github.com/Unknwon/macaron"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/log"
  9. "github.com/gogits/gogs/modules/setting"
  10. )
  11. func OrgAssignment(redirect bool, args ...bool) macaron.Handler {
  12. return func(ctx *Context) {
  13. var (
  14. requireMember bool
  15. requireOwner bool
  16. requireAdminTeam bool
  17. )
  18. if len(args) >= 1 {
  19. requireMember = args[0]
  20. }
  21. if len(args) >= 2 {
  22. requireOwner = args[1]
  23. }
  24. if len(args) >= 3 {
  25. requireAdminTeam = args[2]
  26. }
  27. orgName := ctx.Params(":org")
  28. var err error
  29. ctx.Org.Organization, err = models.GetUserByName(orgName)
  30. if err != nil {
  31. if models.IsErrUserNotExist(err) {
  32. ctx.Handle(404, "GetUserByName", err)
  33. } else if redirect {
  34. log.Error(4, "GetUserByName", err)
  35. ctx.Redirect(setting.AppSubUrl + "/")
  36. } else {
  37. ctx.Handle(500, "GetUserByName", err)
  38. }
  39. return
  40. }
  41. org := ctx.Org.Organization
  42. ctx.Data["Org"] = org
  43. // Force redirection when username is actually a user.
  44. if !org.IsOrganization() {
  45. ctx.Redirect("/" + org.Name)
  46. return
  47. }
  48. 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.Org.OrgLink = setting.AppSubUrl + "/org/" + org.Name
  69. ctx.Data["OrgLink"] = ctx.Org.OrgLink
  70. // Team.
  71. teamName := ctx.Params(":team")
  72. if len(teamName) > 0 {
  73. ctx.Org.Team, err = org.GetTeam(teamName)
  74. if err != nil {
  75. if err == models.ErrTeamNotExist {
  76. ctx.Handle(404, "GetTeam", err)
  77. } else if redirect {
  78. log.Error(4, "GetTeam", err)
  79. ctx.Redirect(setting.AppSubUrl + "/")
  80. } else {
  81. ctx.Handle(500, "GetTeam", err)
  82. }
  83. return
  84. }
  85. ctx.Data["Team"] = ctx.Org.Team
  86. ctx.Org.IsAdminTeam = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.Authorize >= models.ACCESS_MODE_ADMIN
  87. }
  88. ctx.Data["IsAdminTeam"] = ctx.Org.IsAdminTeam
  89. if requireAdminTeam && !ctx.Org.IsAdminTeam {
  90. ctx.Handle(404, "OrgAssignment", err)
  91. return
  92. }
  93. }
  94. }