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.

269 lines
6.4 KiB

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