teams.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. const (
  15. TEAMS base.TplName = "org/team/teams"
  16. TEAM_NEW base.TplName = "org/team/new"
  17. TEAM_MEMBERS base.TplName = "org/team/members"
  18. TEAM_REPOSITORIES base.TplName = "org/team/repositories"
  19. )
  20. func Teams(ctx *middleware.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 *middleware.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 models.IsErrUserNotExist(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 *middleware.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 models.IsErrRepoNotExist(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 *middleware.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 *middleware.Context, form auth.CreateTeamForm) {
  135. ctx.Data["Title"] = ctx.Org.Organization.FullName
  136. ctx.Data["PageIsOrgTeams"] = true
  137. ctx.Data["PageIsOrgTeamsNew"] = true
  138. // Validate permission level.
  139. var auth models.AccessMode
  140. switch form.Permission {
  141. case "read":
  142. auth = models.ACCESS_MODE_READ
  143. case "write":
  144. auth = models.ACCESS_MODE_WRITE
  145. case "admin":
  146. auth = models.ACCESS_MODE_ADMIN
  147. default:
  148. ctx.Error(401)
  149. return
  150. }
  151. t := &models.Team{
  152. OrgID: ctx.Org.Organization.Id,
  153. Name: form.TeamName,
  154. Description: form.Description,
  155. Authorize: auth,
  156. }
  157. ctx.Data["Team"] = t
  158. if ctx.HasError() {
  159. ctx.HTML(200, TEAM_NEW)
  160. return
  161. }
  162. if err := models.NewTeam(t); err != nil {
  163. ctx.Data["Err_TeamName"] = true
  164. switch {
  165. case models.IsErrTeamAlreadyExist(err):
  166. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &form)
  167. default:
  168. ctx.Handle(500, "NewTeam", err)
  169. }
  170. return
  171. }
  172. log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name)
  173. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  174. }
  175. func TeamMembers(ctx *middleware.Context) {
  176. ctx.Data["Title"] = ctx.Org.Team.Name
  177. ctx.Data["PageIsOrgTeams"] = true
  178. if err := ctx.Org.Team.GetMembers(); err != nil {
  179. ctx.Handle(500, "GetMembers", err)
  180. return
  181. }
  182. ctx.HTML(200, TEAM_MEMBERS)
  183. }
  184. func TeamRepositories(ctx *middleware.Context) {
  185. ctx.Data["Title"] = ctx.Org.Team.Name
  186. ctx.Data["PageIsOrgTeams"] = true
  187. if err := ctx.Org.Team.GetRepositories(); err != nil {
  188. ctx.Handle(500, "GetRepositories", err)
  189. return
  190. }
  191. ctx.HTML(200, TEAM_REPOSITORIES)
  192. }
  193. func EditTeam(ctx *middleware.Context) {
  194. ctx.Data["Title"] = ctx.Org.Organization.FullName
  195. ctx.Data["PageIsOrgTeams"] = true
  196. ctx.Data["team_name"] = ctx.Org.Team.Name
  197. ctx.Data["desc"] = ctx.Org.Team.Description
  198. ctx.HTML(200, TEAM_NEW)
  199. }
  200. func EditTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
  201. t := ctx.Org.Team
  202. ctx.Data["Title"] = ctx.Org.Organization.FullName
  203. ctx.Data["PageIsOrgTeams"] = true
  204. ctx.Data["Team"] = t
  205. if ctx.HasError() {
  206. ctx.HTML(200, TEAM_NEW)
  207. return
  208. }
  209. isAuthChanged := false
  210. if !t.IsOwnerTeam() {
  211. // Validate permission level.
  212. var auth models.AccessMode
  213. switch form.Permission {
  214. case "read":
  215. auth = models.ACCESS_MODE_READ
  216. case "write":
  217. auth = models.ACCESS_MODE_WRITE
  218. case "admin":
  219. auth = models.ACCESS_MODE_ADMIN
  220. default:
  221. ctx.Error(401)
  222. return
  223. }
  224. t.Name = form.TeamName
  225. if t.Authorize != auth {
  226. isAuthChanged = true
  227. t.Authorize = auth
  228. }
  229. }
  230. t.Description = form.Description
  231. if err := models.UpdateTeam(t, isAuthChanged); err != nil {
  232. ctx.Data["Err_TeamName"] = true
  233. switch {
  234. case models.IsErrTeamAlreadyExist(err):
  235. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), TEAM_NEW, &form)
  236. default:
  237. ctx.Handle(500, "UpdateTeam", err)
  238. }
  239. return
  240. }
  241. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  242. }
  243. func DeleteTeam(ctx *middleware.Context) {
  244. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  245. ctx.Flash.Error("DeleteTeam: " + err.Error())
  246. } else {
  247. ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success"))
  248. }
  249. ctx.JSON(200, map[string]interface{}{
  250. "redirect": ctx.Org.OrgLink + "/teams",
  251. })
  252. }