teams.go 6.9 KB

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