teams.go 6.0 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. "net/http"
  7. "path"
  8. "github.com/unknwon/com"
  9. log "unknwon.dev/clog/v2"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/db"
  12. "gogs.io/gogs/internal/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.Error(err, "get members")
  27. return
  28. }
  29. }
  30. c.Data["Teams"] = org.Teams
  31. c.Success(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.NotFound()
  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.NotFound()
  53. return
  54. }
  55. err = c.Org.Team.RemoveMember(uid)
  56. page = "team"
  57. case "add":
  58. if !c.Org.IsOwner {
  59. c.NotFound()
  60. return
  61. }
  62. uname := c.Query("uname")
  63. var u *db.User
  64. u, err = db.GetUserByName(uname)
  65. if err != nil {
  66. if db.IsErrUserNotExist(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.Error(err, "get user by name")
  71. }
  72. return
  73. }
  74. err = c.Org.Team.AddMember(u.ID)
  75. page = "team"
  76. }
  77. if err != nil {
  78. if db.IsErrLastOrgOwner(err) {
  79. c.Flash.Error(c.Tr("form.last_org_owner"))
  80. } else {
  81. log.Error("Action(%s): %v", c.Params(":action"), err)
  82. c.JSONSuccess(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.NotFound()
  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 *db.Repository
  106. repo, err = db.GetRepositoryByName(c.Org.Organization.ID, repoName)
  107. if err != nil {
  108. if db.IsErrRepoNotExist(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.Error(err, "get repository by name")
  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. c.Errorf(err, "action %q", c.Params(":action"))
  122. return
  123. }
  124. c.Redirect(c.Org.OrgLink + "/teams/" + c.Org.Team.LowerName + "/repositories")
  125. }
  126. func NewTeam(c *context.Context) {
  127. c.Data["Title"] = c.Org.Organization.FullName
  128. c.Data["PageIsOrgTeams"] = true
  129. c.Data["PageIsOrgTeamsNew"] = true
  130. c.Data["Team"] = &db.Team{}
  131. c.Success(TEAM_NEW)
  132. }
  133. func NewTeamPost(c *context.Context, f form.CreateTeam) {
  134. c.Data["Title"] = c.Org.Organization.FullName
  135. c.Data["PageIsOrgTeams"] = true
  136. c.Data["PageIsOrgTeamsNew"] = true
  137. t := &db.Team{
  138. OrgID: c.Org.Organization.ID,
  139. Name: f.TeamName,
  140. Description: f.Description,
  141. Authorize: db.ParseAccessMode(f.Permission),
  142. }
  143. c.Data["Team"] = t
  144. if c.HasError() {
  145. c.Success(TEAM_NEW)
  146. return
  147. }
  148. if err := db.NewTeam(t); err != nil {
  149. c.Data["Err_TeamName"] = true
  150. switch {
  151. case db.IsErrTeamAlreadyExist(err):
  152. c.RenderWithErr(c.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
  153. case db.IsErrNameNotAllowed(err):
  154. c.RenderWithErr(c.Tr("org.form.team_name_not_allowed", err.(db.ErrNameNotAllowed).Value()), TEAM_NEW, &f)
  155. default:
  156. c.Error(err, "new team")
  157. }
  158. return
  159. }
  160. log.Trace("Team created: %s/%s", c.Org.Organization.Name, t.Name)
  161. c.Redirect(c.Org.OrgLink + "/teams/" + t.LowerName)
  162. }
  163. func TeamMembers(c *context.Context) {
  164. c.Data["Title"] = c.Org.Team.Name
  165. c.Data["PageIsOrgTeams"] = true
  166. if err := c.Org.Team.GetMembers(); err != nil {
  167. c.Error(err, "get members")
  168. return
  169. }
  170. c.Success(TEAM_MEMBERS)
  171. }
  172. func TeamRepositories(c *context.Context) {
  173. c.Data["Title"] = c.Org.Team.Name
  174. c.Data["PageIsOrgTeams"] = true
  175. if err := c.Org.Team.GetRepositories(); err != nil {
  176. c.Error(err, "get repositories")
  177. return
  178. }
  179. c.Success(TEAM_REPOSITORIES)
  180. }
  181. func EditTeam(c *context.Context) {
  182. c.Data["Title"] = c.Org.Organization.FullName
  183. c.Data["PageIsOrgTeams"] = true
  184. c.Data["team_name"] = c.Org.Team.Name
  185. c.Data["desc"] = c.Org.Team.Description
  186. c.Success(TEAM_NEW)
  187. }
  188. func EditTeamPost(c *context.Context, f form.CreateTeam) {
  189. t := c.Org.Team
  190. c.Data["Title"] = c.Org.Organization.FullName
  191. c.Data["PageIsOrgTeams"] = true
  192. c.Data["Team"] = t
  193. if c.HasError() {
  194. c.Success(TEAM_NEW)
  195. return
  196. }
  197. isAuthChanged := false
  198. if !t.IsOwnerTeam() {
  199. // Validate permission level.
  200. var auth db.AccessMode
  201. switch f.Permission {
  202. case "read":
  203. auth = db.AccessModeRead
  204. case "write":
  205. auth = db.AccessModeWrite
  206. case "admin":
  207. auth = db.AccessModeAdmin
  208. default:
  209. c.Status(http.StatusUnauthorized)
  210. return
  211. }
  212. t.Name = f.TeamName
  213. if t.Authorize != auth {
  214. isAuthChanged = true
  215. t.Authorize = auth
  216. }
  217. }
  218. t.Description = f.Description
  219. if err := db.UpdateTeam(t, isAuthChanged); err != nil {
  220. c.Data["Err_TeamName"] = true
  221. switch {
  222. case db.IsErrTeamAlreadyExist(err):
  223. c.RenderWithErr(c.Tr("form.team_name_been_taken"), TEAM_NEW, &f)
  224. default:
  225. c.Error(err, "update team")
  226. }
  227. return
  228. }
  229. c.Redirect(c.Org.OrgLink + "/teams/" + t.LowerName)
  230. }
  231. func DeleteTeam(c *context.Context) {
  232. if err := db.DeleteTeam(c.Org.Team); err != nil {
  233. c.Flash.Error("DeleteTeam: " + err.Error())
  234. } else {
  235. c.Flash.Success(c.Tr("org.teams.delete_team_success"))
  236. }
  237. c.JSONSuccess(map[string]interface{}{
  238. "redirect": c.Org.OrgLink + "/teams",
  239. })
  240. }