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. "github.com/gogs/gogs/models"
  9. "github.com/gogs/gogs/pkg/context"
  10. "github.com/gogs/gogs/routes/api/v1/convert"
  11. "github.com/gogs/gogs/routes/api/v1/user"
  12. )
  13. func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
  14. team := &models.Team{
  15. OrgID: c.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. c.Error(http.StatusUnprocessableEntity, "", err)
  23. } else {
  24. c.ServerError("NewTeam", err)
  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.ServerError("AddMember", err)
  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.ServerError("RemoveMember", err)
  48. return
  49. }
  50. c.NoContent()
  51. }