org.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/modules/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. org := user.GetUserByParamsName(ctx, ":orgname")
  38. if ctx.Written() {
  39. return
  40. }
  41. ctx.JSON(200, convert.ToOrganization(org))
  42. }
  43. // https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
  44. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  45. org := user.GetUserByParamsName(ctx, ":orgname")
  46. if ctx.Written() {
  47. return
  48. }
  49. if !org.IsOwnedBy(ctx.User.Id) {
  50. ctx.Status(403)
  51. return
  52. }
  53. org.FullName = form.FullName
  54. org.Description = form.Description
  55. org.Website = form.Website
  56. org.Location = form.Location
  57. if err := models.UpdateUser(org); err != nil {
  58. ctx.Error(500, "UpdateUser", err)
  59. return
  60. }
  61. ctx.JSON(200, convert.ToOrganization(org))
  62. }