teams.go 6.2 KB

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