org_team.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/db"
  10. "gogs.io/gogs/internal/route/api/v1/convert"
  11. "gogs.io/gogs/internal/route/api/v1/user"
  12. )
  13. func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
  14. team := &db.Team{
  15. OrgID: c.Org.Organization.ID,
  16. Name: form.Name,
  17. Description: form.Description,
  18. Authorize: db.ParseAccessMode(form.Permission),
  19. }
  20. if err := db.NewTeam(team); err != nil {
  21. if db.IsErrTeamAlreadyExist(err) {
  22. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  23. } else {
  24. c.Error(err, "new team")
  25. }
  26. return
  27. }
  28. c.JSON(http.StatusCreated, convert.ToTeam(team))
  29. }
  30. func AddTeamMember(c *context.APIContext) {
  31. u := user.GetUserByParams(c)
  32. if c.Written() {
  33. return
  34. }
  35. if err := c.Org.Team.AddMember(u.ID); err != nil {
  36. c.Error(err, "add member")
  37. return
  38. }
  39. c.NoContent()
  40. }
  41. func RemoveTeamMember(c *context.APIContext) {
  42. u := user.GetUserByParams(c)
  43. if c.Written() {
  44. return
  45. }
  46. if err := c.Org.Team.RemoveMember(u.ID); err != nil {
  47. c.Error(err, "remove member")
  48. return
  49. }
  50. c.NoContent()
  51. }
  52. func ListTeamMembers(c *context.APIContext) {
  53. team := c.Org.Team
  54. if err := team.GetMembers(); err != nil {
  55. c.Error(err, "get team members")
  56. return
  57. }
  58. apiMembers := make([]*api.User, len(team.Members))
  59. for i := range team.Members {
  60. apiMembers[i] = team.Members[i].APIFormat()
  61. }
  62. c.JSONSuccess(apiMembers)
  63. }