本站源代码
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.

333 lines
8.5KB

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