org.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.Context, u *models.User, all bool) {
  13. if err := u.GetOrganizations(all); err != nil {
  14. ctx.APIError(500, "GetOrganizations", err)
  15. return
  16. }
  17. apiOrgs := make([]*api.Organization, len(u.Orgs))
  18. for i := range u.Orgs {
  19. apiOrgs[i] = convert.ToApiOrganization(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.Context) {
  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.Context) {
  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.Context) {
  37. org := user.GetUserByParamsName(ctx, ":orgname")
  38. if ctx.Written() {
  39. return
  40. }
  41. ctx.JSON(200, convert.ToApiOrganization(org))
  42. }
  43. // https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
  44. func Edit(ctx *context.Context, 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.Error(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.APIError(500, "UpdateUser", err)
  59. return
  60. }
  61. ctx.JSON(200, convert.ToApiOrganization(org))
  62. }