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

283 lines
7.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
  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. default:
  99. ctx.Redirect(ctx.Org.OrgLink + "/teams")
  100. }
  101. }
  102. // TeamsRepoAction operate team's repository
  103. func TeamsRepoAction(ctx *context.Context) {
  104. if !ctx.Org.IsOwner {
  105. ctx.Error(404)
  106. return
  107. }
  108. var err error
  109. switch ctx.Params(":action") {
  110. case "add":
  111. repoName := path.Base(ctx.Query("repo_name"))
  112. var repo *models.Repository
  113. repo, err = models.GetRepositoryByName(ctx.Org.Organization.ID, repoName)
  114. if err != nil {
  115. if models.IsErrRepoNotExist(err) {
  116. ctx.Flash.Error(ctx.Tr("org.teams.add_nonexistent_repo"))
  117. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
  118. return
  119. }
  120. ctx.Handle(500, "GetRepositoryByName", err)
  121. return
  122. }
  123. err = ctx.Org.Team.AddRepository(repo)
  124. case "remove":
  125. err = ctx.Org.Team.RemoveRepository(com.StrTo(ctx.Query("repoid")).MustInt64())
  126. }
  127. if err != nil {
  128. log.Error(3, "Action(%s): '%s' %v", ctx.Params(":action"), ctx.Org.Team.Name, err)
  129. ctx.Handle(500, "TeamsRepoAction", err)
  130. return
  131. }
  132. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories")
  133. }
  134. // NewTeam render create new team page
  135. func NewTeam(ctx *context.Context) {
  136. ctx.Data["Title"] = ctx.Org.Organization.FullName
  137. ctx.Data["PageIsOrgTeams"] = true
  138. ctx.Data["PageIsOrgTeamsNew"] = true
  139. ctx.Data["Team"] = &models.Team{}
  140. ctx.HTML(200, tplTeamNew)
  141. }
  142. // NewTeamPost response for create new team
  143. func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
  144. ctx.Data["Title"] = ctx.Org.Organization.FullName
  145. ctx.Data["PageIsOrgTeams"] = true
  146. ctx.Data["PageIsOrgTeamsNew"] = true
  147. t := &models.Team{
  148. OrgID: ctx.Org.Organization.ID,
  149. Name: form.TeamName,
  150. Description: form.Description,
  151. Authorize: models.ParseAccessMode(form.Permission),
  152. }
  153. ctx.Data["Team"] = t
  154. if ctx.HasError() {
  155. ctx.HTML(200, tplTeamNew)
  156. return
  157. }
  158. if err := models.NewTeam(t); err != nil {
  159. ctx.Data["Err_TeamName"] = true
  160. switch {
  161. case models.IsErrTeamAlreadyExist(err):
  162. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
  163. default:
  164. ctx.Handle(500, "NewTeam", err)
  165. }
  166. return
  167. }
  168. log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name)
  169. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  170. }
  171. // TeamMembers render team members page
  172. func TeamMembers(ctx *context.Context) {
  173. ctx.Data["Title"] = ctx.Org.Team.Name
  174. ctx.Data["PageIsOrgTeams"] = true
  175. if err := ctx.Org.Team.GetMembers(); err != nil {
  176. ctx.Handle(500, "GetMembers", err)
  177. return
  178. }
  179. ctx.HTML(200, tplTeamMembers)
  180. }
  181. // TeamRepositories show the repositories of team
  182. func TeamRepositories(ctx *context.Context) {
  183. ctx.Data["Title"] = ctx.Org.Team.Name
  184. ctx.Data["PageIsOrgTeams"] = true
  185. if err := ctx.Org.Team.GetRepositories(); err != nil {
  186. ctx.Handle(500, "GetRepositories", err)
  187. return
  188. }
  189. ctx.HTML(200, tplTeamRepositories)
  190. }
  191. // EditTeam render team edit page
  192. func EditTeam(ctx *context.Context) {
  193. ctx.Data["Title"] = ctx.Org.Organization.FullName
  194. ctx.Data["PageIsOrgTeams"] = true
  195. ctx.Data["team_name"] = ctx.Org.Team.Name
  196. ctx.Data["desc"] = ctx.Org.Team.Description
  197. ctx.HTML(200, tplTeamNew)
  198. }
  199. // EditTeamPost response for modify team information
  200. func EditTeamPost(ctx *context.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, tplTeamNew)
  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.AccessModeRead
  216. case "write":
  217. auth = models.AccessModeWrite
  218. case "admin":
  219. auth = models.AccessModeAdmin
  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"), tplTeamNew, &form)
  236. default:
  237. ctx.Handle(500, "UpdateTeam", err)
  238. }
  239. return
  240. }
  241. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  242. }
  243. // DeleteTeam response for the delete team request
  244. func DeleteTeam(ctx *context.Context) {
  245. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  246. ctx.Flash.Error("DeleteTeam: " + err.Error())
  247. } else {
  248. ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success"))
  249. }
  250. ctx.JSON(200, map[string]interface{}{
  251. "redirect": ctx.Org.OrgLink + "/teams",
  252. })
  253. }