You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

team.go 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2016 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. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. "code.gitea.io/gitea/routers/api/v1/user"
  11. )
  12. // ListTeams list all the teams of an organization
  13. func ListTeams(ctx *context.APIContext) {
  14. org := ctx.Org.Organization
  15. if !org.IsOrgMember(ctx.User.ID) {
  16. ctx.Error(403, "", "Must be a member of the organization")
  17. return
  18. }
  19. if err := org.GetTeams(); err != nil {
  20. ctx.Error(500, "GetTeams", err)
  21. return
  22. }
  23. apiTeams := make([]*api.Team, len(org.Teams))
  24. for i := range org.Teams {
  25. apiTeams[i] = convert.ToTeam(org.Teams[i])
  26. }
  27. ctx.JSON(200, apiTeams)
  28. }
  29. // GetTeam api for get a team
  30. func GetTeam(ctx *context.APIContext) {
  31. if !models.IsOrganizationMember(ctx.Org.Team.OrgID, ctx.User.ID) {
  32. ctx.Status(404)
  33. return
  34. }
  35. ctx.JSON(200, convert.ToTeam(ctx.Org.Team))
  36. }
  37. // GetTeamRepos api for get a team's repos
  38. func GetTeamRepos(ctx *context.APIContext) {
  39. team := ctx.Org.Team
  40. if !models.IsOrganizationMember(team.OrgID, ctx.User.ID) {
  41. ctx.Status(404)
  42. return
  43. }
  44. if err := team.GetRepositories(); err != nil {
  45. ctx.Error(500, "GetTeamRepos", err)
  46. }
  47. repos := make([]*api.Repository, len(team.Repos))
  48. for i, repo := range team.Repos {
  49. access, err := models.AccessLevel(ctx.User, repo)
  50. if err != nil {
  51. ctx.Error(500, "GetTeamRepos", err)
  52. return
  53. }
  54. repos[i] = repo.APIFormat(access)
  55. }
  56. ctx.JSON(200, repos)
  57. }
  58. // CreateTeam api for create a team
  59. func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
  60. if !ctx.Org.Organization.IsOrgMember(ctx.User.ID) {
  61. ctx.Error(403, "", "Must be an organization member")
  62. }
  63. team := &models.Team{
  64. OrgID: ctx.Org.Organization.ID,
  65. Name: form.Name,
  66. Description: form.Description,
  67. Authorize: models.ParseAccessMode(form.Permission),
  68. }
  69. if err := models.NewTeam(team); err != nil {
  70. if models.IsErrTeamAlreadyExist(err) {
  71. ctx.Error(422, "", err)
  72. } else {
  73. ctx.Error(500, "NewTeam", err)
  74. }
  75. return
  76. }
  77. ctx.JSON(201, convert.ToTeam(team))
  78. }
  79. // EditTeam api for edit a team
  80. func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
  81. if !ctx.User.IsUserOrgOwner(ctx.Org.Team.OrgID) {
  82. ctx.Error(403, "", "Must be an organization owner")
  83. return
  84. }
  85. team := &models.Team{
  86. ID: ctx.Org.Team.ID,
  87. OrgID: ctx.Org.Team.OrgID,
  88. Name: form.Name,
  89. Description: form.Description,
  90. Authorize: models.ParseAccessMode(form.Permission),
  91. }
  92. if err := models.UpdateTeam(team, true); err != nil {
  93. ctx.Error(500, "EditTeam", err)
  94. return
  95. }
  96. ctx.JSON(200, convert.ToTeam(team))
  97. }
  98. // DeleteTeam api for delete a team
  99. func DeleteTeam(ctx *context.APIContext) {
  100. if !ctx.User.IsUserOrgOwner(ctx.Org.Team.OrgID) {
  101. ctx.Error(403, "", "Must be an organization owner")
  102. return
  103. }
  104. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  105. ctx.Error(500, "DeleteTeam", err)
  106. return
  107. }
  108. ctx.Status(204)
  109. }
  110. // GetTeamMembers api for get a team's members
  111. func GetTeamMembers(ctx *context.APIContext) {
  112. if !models.IsOrganizationMember(ctx.Org.Team.OrgID, ctx.User.ID) {
  113. ctx.Status(404)
  114. return
  115. }
  116. team := ctx.Org.Team
  117. if err := team.GetMembers(); err != nil {
  118. ctx.Error(500, "GetTeamMembers", err)
  119. return
  120. }
  121. members := make([]*api.User, len(team.Members))
  122. for i, member := range team.Members {
  123. members[i] = member.APIFormat()
  124. }
  125. ctx.JSON(200, members)
  126. }
  127. // AddTeamMember api for add a member to a team
  128. func AddTeamMember(ctx *context.APIContext) {
  129. if !ctx.User.IsUserOrgOwner(ctx.Org.Team.OrgID) {
  130. ctx.Error(403, "", "Must be an organization owner")
  131. return
  132. }
  133. u := user.GetUserByParams(ctx)
  134. if ctx.Written() {
  135. return
  136. }
  137. if err := ctx.Org.Team.AddMember(u.ID); err != nil {
  138. ctx.Error(500, "AddMember", err)
  139. return
  140. }
  141. ctx.Status(204)
  142. }
  143. // RemoveTeamMember api for remove one member from a team
  144. func RemoveTeamMember(ctx *context.APIContext) {
  145. if !ctx.User.IsUserOrgOwner(ctx.Org.Team.OrgID) {
  146. ctx.Error(403, "", "Must be an organization owner")
  147. return
  148. }
  149. u := user.GetUserByParams(ctx)
  150. if ctx.Written() {
  151. return
  152. }
  153. if err := ctx.Org.Team.RemoveMember(u.ID); err != nil {
  154. ctx.Error(500, "RemoveMember", err)
  155. return
  156. }
  157. ctx.Status(204)
  158. }