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.

88 lines
2.1 KiB

8 years ago
  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 admin
  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. // CreateTeam api for create a team
  13. func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
  14. team := &models.Team{
  15. OrgID: ctx.Org.Organization.ID,
  16. Name: form.Name,
  17. Description: form.Description,
  18. Authorize: models.ParseAccessMode(form.Permission),
  19. }
  20. if err := models.NewTeam(team); err != nil {
  21. if models.IsErrTeamAlreadyExist(err) {
  22. ctx.Error(422, "", err)
  23. } else {
  24. ctx.Error(500, "NewTeam", err)
  25. }
  26. return
  27. }
  28. ctx.JSON(201, convert.ToTeam(team))
  29. }
  30. // EditTeam api for edit a team
  31. func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
  32. team := &models.Team{
  33. ID: ctx.Org.Team.ID,
  34. OrgID: ctx.Org.Team.OrgID,
  35. Name: form.Name,
  36. Description: form.Description,
  37. Authorize: models.ParseAccessMode(form.Permission),
  38. }
  39. if err := models.UpdateTeam(team, true); err != nil {
  40. ctx.Error(500, "EditTeam", err)
  41. return
  42. }
  43. ctx.JSON(200, convert.ToTeam(team))
  44. }
  45. // DeleteTeam api for delete a team
  46. func DeleteTeam(ctx *context.APIContext) {
  47. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  48. ctx.Error(500, "DeleteTeam", err)
  49. return
  50. }
  51. ctx.Status(204)
  52. }
  53. // AddTeamMember api for add a member to a team
  54. func AddTeamMember(ctx *context.APIContext) {
  55. u := user.GetUserByParams(ctx)
  56. if ctx.Written() {
  57. return
  58. }
  59. if err := ctx.Org.Team.AddMember(u.ID); err != nil {
  60. ctx.Error(500, "AddMember", err)
  61. return
  62. }
  63. ctx.Status(204)
  64. }
  65. // RemoveTeamMember api for remove one member from a team
  66. func RemoveTeamMember(ctx *context.APIContext) {
  67. u := user.GetUserByParams(ctx)
  68. if ctx.Written() {
  69. return
  70. }
  71. if err := ctx.Org.Team.RemoveMember(u.ID); err != nil {
  72. ctx.Error(500, "RemoveMember", err)
  73. return
  74. }
  75. ctx.Status(204)
  76. }