org_team.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/modules/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. }