You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

teams.go 7.3 kB

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