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.

131 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 org
  5. import (
  6. "github.com/go-martini/martini"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/auth"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. const (
  14. TEAMS base.TplName = "org/teams"
  15. TEAM_NEW base.TplName = "org/team_new"
  16. )
  17. func Teams(ctx *middleware.Context, params martini.Params) {
  18. ctx.Data["Title"] = "Organization " + params["org"] + " Teams"
  19. org, err := models.GetUserByName(params["org"])
  20. if err != nil {
  21. if err == models.ErrUserNotExist {
  22. ctx.Handle(404, "org.Teams(GetUserByName)", err)
  23. } else {
  24. ctx.Handle(500, "org.Teams(GetUserByName)", err)
  25. }
  26. return
  27. }
  28. ctx.Data["Org"] = org
  29. if err = org.GetTeams(); err != nil {
  30. ctx.Handle(500, "org.Teams(GetTeams)", err)
  31. return
  32. }
  33. for _, t := range org.Teams {
  34. if err = t.GetMembers(); err != nil {
  35. ctx.Handle(500, "org.Home(GetMembers)", err)
  36. return
  37. }
  38. }
  39. ctx.Data["Teams"] = org.Teams
  40. ctx.HTML(200, TEAMS)
  41. }
  42. func NewTeam(ctx *middleware.Context, params martini.Params) {
  43. org, err := models.GetUserByName(params["org"])
  44. if err != nil {
  45. if err == models.ErrUserNotExist {
  46. ctx.Handle(404, "org.NewTeam(GetUserByName)", err)
  47. } else {
  48. ctx.Handle(500, "org.NewTeam(GetUserByName)", err)
  49. }
  50. return
  51. }
  52. ctx.Data["Org"] = org
  53. // Check ownership of organization.
  54. if !org.IsOrgOwner(ctx.User.Id) {
  55. ctx.Error(403)
  56. return
  57. }
  58. ctx.HTML(200, TEAM_NEW)
  59. }
  60. func NewTeamPost(ctx *middleware.Context, params martini.Params, form auth.CreateTeamForm) {
  61. org, err := models.GetUserByName(params["org"])
  62. if err != nil {
  63. if err == models.ErrUserNotExist {
  64. ctx.Handle(404, "org.NewTeamPost(GetUserByName)", err)
  65. } else {
  66. ctx.Handle(500, "org.NewTeamPost(GetUserByName)", err)
  67. }
  68. return
  69. }
  70. ctx.Data["Org"] = org
  71. // Check ownership of organization.
  72. if !org.IsOrgOwner(ctx.User.Id) {
  73. ctx.Error(403)
  74. return
  75. }
  76. if ctx.HasError() {
  77. ctx.HTML(200, TEAM_NEW)
  78. return
  79. }
  80. // Validate permission level.
  81. var auth models.AuthorizeType
  82. switch form.Permission {
  83. case "read":
  84. auth = models.ORG_READABLE
  85. case "write":
  86. auth = models.ORG_WRITABLE
  87. case "admin":
  88. auth = models.ORG_ADMIN
  89. default:
  90. ctx.Error(401)
  91. return
  92. }
  93. t := &models.Team{
  94. OrgId: org.Id,
  95. Name: form.TeamName,
  96. Description: form.Description,
  97. Authorize: auth,
  98. }
  99. if err = models.NewTeam(t); err != nil {
  100. if err == models.ErrTeamAlreadyExist {
  101. ctx.Data["Err_TeamName"] = true
  102. ctx.RenderWithErr("Team name has already been used", TEAM_NEW, &form)
  103. } else {
  104. ctx.Handle(500, "org.NewTeamPost(NewTeam)", err)
  105. }
  106. return
  107. }
  108. log.Trace("%s Team created: %s/%s", ctx.Req.RequestURI, org.Name, t.Name)
  109. ctx.Redirect("/org/" + org.LowerName + "/teams/" + t.LowerName)
  110. }
  111. func EditTeam(ctx *middleware.Context, params martini.Params) {
  112. ctx.Data["Title"] = "Organization " + params["org"] + " Edit Team"
  113. ctx.HTML(200, "org/edit_team")
  114. }