org_team.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }