org.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/routers/api/v1/convert"
  10. "github.com/gogits/gogs/routers/api/v1/user"
  11. )
  12. func listUserOrgs(ctx *context.APIContext, u *models.User, all bool) {
  13. if err := u.GetOrganizations(all); err != nil {
  14. ctx.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. ctx.JSON(200, &apiOrgs)
  22. }
  23. // https://github.com/gogits/go-gogs-client/wiki/Organizations#list-your-organizations
  24. func ListMyOrgs(ctx *context.APIContext) {
  25. listUserOrgs(ctx, ctx.User, true)
  26. }
  27. // https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations
  28. func ListUserOrgs(ctx *context.APIContext) {
  29. u := user.GetUserByParams(ctx)
  30. if ctx.Written() {
  31. return
  32. }
  33. listUserOrgs(ctx, u, false)
  34. }
  35. // https://github.com/gogits/go-gogs-client/wiki/Organizations#get-an-organization
  36. func Get(ctx *context.APIContext) {
  37. ctx.JSON(200, convert.ToOrganization(ctx.Org.Organization))
  38. }
  39. // https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
  40. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  41. org := ctx.Org.Organization
  42. if !org.IsOwnedBy(ctx.User.ID) {
  43. ctx.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. ctx.Error(500, "UpdateUser", err)
  52. return
  53. }
  54. ctx.JSON(200, convert.ToOrganization(org))
  55. }