teams.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. "path"
  7. "github.com/Unknwon/com"
  8. log "gopkg.in/clog.v1"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/models/errors"
  11. "github.com/gogits/gogs/pkg/context"
  12. "github.com/gogits/gogs/pkg/form"
  13. )
  14. const (
  15. TEAMS = "org/team/teams"
  16. TEAM_NEW = "org/team/new"
  17. TEAM_MEMBERS = "org/team/members"
  18. TEAM_REPOSITORIES = "org/team/repositories"
  19. )
  20. func Teams(ctx *context.Context) {
  21. org := ctx.Org.Organization
  22. ctx.Data["Title"] = org.FullName
  23. ctx.Data["PageIsOrgTeams"] = true
  24. for _, t := range org.Teams {
  25. if err := t.GetMembers(); err != nil {
  26. ctx.Handle(500, "GetMembers", err)
  27. return
  28. }
  29. }
  30. ctx.Data["Teams"] = org.Teams
  31. ctx.HTML(200, TEAMS)
  32. }
  33. func TeamsAction(ctx *context.Context) {
  34. uid := com.StrTo(ctx.Query("uid")).MustInt64()
  35. if uid == 0 {
  36. ctx.Redirect(ctx.Org.OrgLink + "/teams")
  37. return
  38. }
  39. page := ctx.Query("page")
  40. var err error
  41. switch ctx.Params(":action") {
  42. case "join":
  43. if !ctx.Org.IsOwner {
  44. ctx.Error(404)
  45. return
  46. }
  47. err = ctx.Org.Team.AddMember(ctx.User.ID)
  48. case "leave":
  49. err = ctx.Org.Team.RemoveMember(ctx.User.ID)
  50. case "remove":
  51. if !ctx.Org.IsOwner {
  52. ctx.Error(404)
  53. return
  54. }
  55. err = ctx.Org.Team.RemoveMember(uid)
  56. page = "team"
  57. case "add":
  58. if !ctx.Org.IsOwner {
  59. ctx.Error(404)
  60. return
  61. }
  62. uname := ctx.Query("uname")
  63. var u *models.User
  64. u, err = models.GetUserByName(uname)
  65. if err != nil {
  66. if errors.IsUserNotExist(err) {
  67. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  68. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName)
  69. } else {
  70. ctx.Handle(500, " GetUserByName", err)
  71. }
  72. return
  73. }
  74. err = ctx.Org.Team.AddMember(u.ID)
  75. page = "team"
  76. }
  77. if err != nil {
  78. if models.IsErrLastOrgOwner(err) {
  79. ctx.Flash.Error(ctx.Tr("form.last_org_owner"))
  80. } else {
  81. log.Error(3, "Action(%s): %v", ctx.Params(":action"), err)
  82. ctx.JSON(200, map[string]interface{}{
  83. "ok": false,
  84. "err": err.Error(),
  85. })
  86. return
  87. }
  88. }
  89. switch page {
  90. case "team":
  91. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName)
  92. default:
  93. ctx.Redirect(ctx.Org.OrgLink + "/teams")
  94. }
  95. }
  96. func TeamsRepoAction(ctx *context.Context) {
  97. if !ctx.Org.IsOwner {
  98. ctx.Error(404)
  99. return
  100. }
  101. var err error
  102. switch ctx.Params(":action") {
  103. case "add":
  104. repoName := path.Base(ctx.Query("repo_name"))
  105. var repo *models.Repository
  106. repo, err = models.GetRepositoryByName(ctx.Org.Organization.ID, repoName)
  107. if err != nil {
  108. if errors.IsRepoNotExist(err) {
  109. ctx.Flash.Error(ctx.Tr("org.teams.add_nonexistent_repo"))
  110. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
  111. return
  112. }
  113. ctx.Handle(500, "GetRepositoryByName", err)
  114. return
  115. }
  116. err = ctx.Org.Team.AddRepository(repo)
  117. case "remove":
  118. err = ctx.Org.Team.RemoveRepository(com.StrTo(ctx.Query("repoid")).MustInt64())
  119. }
  120. if err != nil {
  121. log.Error(3, "Action(%s): '%s' %v", ctx.Params(":action"), ctx.Org.Team.Name, err)
  122. ctx.Handle(500, "TeamsRepoAction", err)
  123. return
  124. }
  125. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
  126. }
  127. func NewTeam(ctx *context.Context) {
  128. ctx.Data["Title"] = ctx.Org.Organization.FullName
  129. ctx.Data["PageIsOrgTeams"] = true
  130. ctx.Data["PageIsOrgTeamsNew"] = true
  131. ctx.Data["Team"] = &models.Team{}
  132. ctx.HTML(200, TEAM_NEW)
  133. }
  134. func NewTeamPost(ctx *context.Context, f form.CreateTeam) {
  135. ctx.Data["Title"] = ctx.Org.Organization.FullName
  136. ctx.Data["PageIsOrgTeams"] = true
  137. ctx.Data["PageIsOrgTeamsNew"] = true
  138. t := &models.Team{
  139. OrgID: ctx.Org.Organization.ID,
  140. Name: f.TeamName,
  141. Description: f.Description,
  142. Authorize: models.ParseAccessMode(f.Permission),
  143. }
  144. ctx.Data["Team"] = t
  145. if ctx.HasError() {
  146. ctx.HTML(200, TEAM_NEW)
  147. return
  148. }
  149. if err := models.NewTeam(t); err != nil {
  150. ctx.Data["Err_TeamName"] = true
  151. switch {
  152. case models.IsErrTeamAlreadyExist(err):
  153. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
  154. case models.IsErrNameReserved(err):
  155. ctx.RenderWithErr(ctx.Tr("org.form.team_name_reserved", err.(models.ErrNameReserved).Name), TEAM_NEW, &f)
  156. default:
  157. ctx.Handle(500, "NewTeam", err)
  158. }
  159. return
  160. }
  161. log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name)
  162. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  163. }
  164. func TeamMembers(ctx *context.Context) {
  165. ctx.Data["Title"] = ctx.Org.Team.Name
  166. ctx.Data["PageIsOrgTeams"] = true
  167. if err := ctx.Org.Team.GetMembers(); err != nil {
  168. ctx.Handle(500, "GetMembers", err)
  169. return
  170. }
  171. ctx.HTML(200, TEAM_MEMBERS)
  172. }
  173. func TeamRepositories(ctx *context.Context) {
  174. ctx.Data["Title"] = ctx.Org.Team.Name
  175. ctx.Data["PageIsOrgTeams"] = true
  176. if err := ctx.Org.Team.GetRepositories(); err != nil {
  177. ctx.Handle(500, "GetRepositories", err)
  178. return
  179. }
  180. ctx.HTML(200, TEAM_REPOSITORIES)
  181. }
  182. func EditTeam(ctx *context.Context) {
  183. ctx.Data["Title"] = ctx.Org.Organization.FullName
  184. ctx.Data["PageIsOrgTeams"] = true
  185. ctx.Data["team_name"] = ctx.Org.Team.Name
  186. ctx.Data["desc"] = ctx.Org.Team.Description
  187. ctx.HTML(200, TEAM_NEW)
  188. }
  189. func EditTeamPost(ctx *context.Context, f form.CreateTeam) {
  190. t := ctx.Org.Team
  191. ctx.Data["Title"] = ctx.Org.Organization.FullName
  192. ctx.Data["PageIsOrgTeams"] = true
  193. ctx.Data["Team"] = t
  194. if ctx.HasError() {
  195. ctx.HTML(200, TEAM_NEW)
  196. return
  197. }
  198. isAuthChanged := false
  199. if !t.IsOwnerTeam() {
  200. // Validate permission level.
  201. var auth models.AccessMode
  202. switch f.Permission {
  203. case "read":
  204. auth = models.ACCESS_MODE_READ
  205. case "write":
  206. auth = models.ACCESS_MODE_WRITE
  207. case "admin":
  208. auth = models.ACCESS_MODE_ADMIN
  209. default:
  210. ctx.Error(401)
  211. return
  212. }
  213. t.Name = f.TeamName
  214. if t.Authorize != auth {
  215. isAuthChanged = true
  216. t.Authorize = auth
  217. }
  218. }
  219. t.Description = f.Description
  220. if err := models.UpdateTeam(t, isAuthChanged); err != nil {
  221. ctx.Data["Err_TeamName"] = true
  222. switch {
  223. case models.IsErrTeamAlreadyExist(err):
  224. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
  225. default:
  226. ctx.Handle(500, "UpdateTeam", err)
  227. }
  228. return
  229. }
  230. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  231. }
  232. func DeleteTeam(ctx *context.Context) {
  233. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  234. ctx.Flash.Error("DeleteTeam: " + err.Error())
  235. } else {
  236. ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success"))
  237. }
  238. ctx.JSON(200, map[string]interface{}{
  239. "redirect": ctx.Org.OrgLink + "/teams",
  240. })
  241. }