Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

teams.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.Data["Units"] = models.Units
  143. ctx.HTML(200, tplTeamNew)
  144. }
  145. // NewTeamPost response for create new team
  146. func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
  147. ctx.Data["Title"] = ctx.Org.Organization.FullName
  148. ctx.Data["PageIsOrgTeams"] = true
  149. ctx.Data["PageIsOrgTeamsNew"] = true
  150. t := &models.Team{
  151. OrgID: ctx.Org.Organization.ID,
  152. Name: form.TeamName,
  153. Description: form.Description,
  154. Authorize: models.ParseAccessMode(form.Permission),
  155. UnitTypes: form.Units,
  156. }
  157. ctx.Data["Team"] = t
  158. if ctx.HasError() {
  159. ctx.HTML(200, tplTeamNew)
  160. return
  161. }
  162. if err := models.NewTeam(t); err != nil {
  163. ctx.Data["Err_TeamName"] = true
  164. switch {
  165. case models.IsErrTeamAlreadyExist(err):
  166. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
  167. default:
  168. ctx.Handle(500, "NewTeam", err)
  169. }
  170. return
  171. }
  172. log.Trace("Team created: %s/%s", ctx.Org.Organization.Name, t.Name)
  173. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  174. }
  175. // TeamMembers render team members page
  176. func TeamMembers(ctx *context.Context) {
  177. ctx.Data["Title"] = ctx.Org.Team.Name
  178. ctx.Data["PageIsOrgTeams"] = true
  179. if err := ctx.Org.Team.GetMembers(); err != nil {
  180. ctx.Handle(500, "GetMembers", err)
  181. return
  182. }
  183. ctx.HTML(200, tplTeamMembers)
  184. }
  185. // TeamRepositories show the repositories of team
  186. func TeamRepositories(ctx *context.Context) {
  187. ctx.Data["Title"] = ctx.Org.Team.Name
  188. ctx.Data["PageIsOrgTeams"] = true
  189. if err := ctx.Org.Team.GetRepositories(); err != nil {
  190. ctx.Handle(500, "GetRepositories", err)
  191. return
  192. }
  193. ctx.HTML(200, tplTeamRepositories)
  194. }
  195. // EditTeam render team edit page
  196. func EditTeam(ctx *context.Context) {
  197. ctx.Data["Title"] = ctx.Org.Organization.FullName
  198. ctx.Data["PageIsOrgTeams"] = true
  199. ctx.Data["team_name"] = ctx.Org.Team.Name
  200. ctx.Data["desc"] = ctx.Org.Team.Description
  201. ctx.Data["Units"] = models.Units
  202. ctx.HTML(200, tplTeamNew)
  203. }
  204. // EditTeamPost response for modify team information
  205. func EditTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
  206. t := ctx.Org.Team
  207. ctx.Data["Title"] = ctx.Org.Organization.FullName
  208. ctx.Data["PageIsOrgTeams"] = true
  209. ctx.Data["Team"] = t
  210. if ctx.HasError() {
  211. ctx.HTML(200, tplTeamNew)
  212. return
  213. }
  214. isAuthChanged := false
  215. if !t.IsOwnerTeam() {
  216. // Validate permission level.
  217. var auth models.AccessMode
  218. switch form.Permission {
  219. case "read":
  220. auth = models.AccessModeRead
  221. case "write":
  222. auth = models.AccessModeWrite
  223. case "admin":
  224. auth = models.AccessModeAdmin
  225. default:
  226. ctx.Error(401)
  227. return
  228. }
  229. t.Name = form.TeamName
  230. if t.Authorize != auth {
  231. isAuthChanged = true
  232. t.Authorize = auth
  233. }
  234. }
  235. t.Description = form.Description
  236. t.UnitTypes = form.Units
  237. if err := models.UpdateTeam(t, isAuthChanged); err != nil {
  238. ctx.Data["Err_TeamName"] = true
  239. switch {
  240. case models.IsErrTeamAlreadyExist(err):
  241. ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form)
  242. default:
  243. ctx.Handle(500, "UpdateTeam", err)
  244. }
  245. return
  246. }
  247. ctx.Redirect(ctx.Org.OrgLink + "/teams/" + t.LowerName)
  248. }
  249. // DeleteTeam response for the delete team request
  250. func DeleteTeam(ctx *context.Context) {
  251. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  252. ctx.Flash.Error("DeleteTeam: " + err.Error())
  253. } else {
  254. ctx.Flash.Success(ctx.Tr("org.teams.delete_team_success"))
  255. }
  256. ctx.JSON(200, map[string]interface{}{
  257. "redirect": ctx.Org.OrgLink + "/teams",
  258. })
  259. }