org_team.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/pkg/context"
  9. "github.com/gogits/gogs/routers/api/v1/convert"
  10. "github.com/gogits/gogs/routers/api/v1/user"
  11. )
  12. func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
  13. team := &models.Team{
  14. OrgID: ctx.Org.Organization.ID,
  15. Name: form.Name,
  16. Description: form.Description,
  17. Authorize: models.ParseAccessMode(form.Permission),
  18. }
  19. if err := models.NewTeam(team); err != nil {
  20. if models.IsErrTeamAlreadyExist(err) {
  21. ctx.Error(422, "", err)
  22. } else {
  23. ctx.Error(500, "NewTeam", err)
  24. }
  25. return
  26. }
  27. ctx.JSON(201, convert.ToTeam(team))
  28. }
  29. func AddTeamMember(ctx *context.APIContext) {
  30. u := user.GetUserByParams(ctx)
  31. if ctx.Written() {
  32. return
  33. }
  34. if err := ctx.Org.Team.AddMember(u.ID); err != nil {
  35. ctx.Error(500, "AddMember", err)
  36. return
  37. }
  38. ctx.Status(204)
  39. }
  40. func RemoveTeamMember(ctx *context.APIContext) {
  41. u := user.GetUserByParams(ctx)
  42. if ctx.Written() {
  43. return
  44. }
  45. if err := ctx.Org.Team.RemoveMember(u.ID); err != nil {
  46. ctx.Error(500, "RemoveMember", err)
  47. return
  48. }
  49. ctx.Status(204)
  50. }