org.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2015 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 org
  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/routes/api/v1/convert"
  10. "github.com/gogits/gogs/routes/api/v1/user"
  11. )
  12. func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
  13. if err := u.GetOrganizations(all); err != nil {
  14. c.Error(500, "GetOrganizations", err)
  15. return
  16. }
  17. apiOrgs := make([]*api.Organization, len(u.Orgs))
  18. for i := range u.Orgs {
  19. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  20. }
  21. c.JSON(200, &apiOrgs)
  22. }
  23. // https://github.com/gogits/go-gogs-client/wiki/Organizations#list-your-organizations
  24. func ListMyOrgs(c *context.APIContext) {
  25. listUserOrgs(c, c.User, true)
  26. }
  27. // https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations
  28. func ListUserOrgs(c *context.APIContext) {
  29. u := user.GetUserByParams(c)
  30. if c.Written() {
  31. return
  32. }
  33. listUserOrgs(c, u, false)
  34. }
  35. // https://github.com/gogits/go-gogs-client/wiki/Organizations#get-an-organization
  36. func Get(c *context.APIContext) {
  37. c.JSON(200, convert.ToOrganization(c.Org.Organization))
  38. }
  39. // https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
  40. func Edit(c *context.APIContext, form api.EditOrgOption) {
  41. org := c.Org.Organization
  42. if !org.IsOwnedBy(c.User.ID) {
  43. c.Status(403)
  44. return
  45. }
  46. org.FullName = form.FullName
  47. org.Description = form.Description
  48. org.Website = form.Website
  49. org.Location = form.Location
  50. if err := models.UpdateUser(org); err != nil {
  51. c.Error(500, "UpdateUser", err)
  52. return
  53. }
  54. c.JSON(200, convert.ToOrganization(org))
  55. }