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