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.

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