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.

288 lines
6.9 KiB

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