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/middleware"
  9. "github.com/gogits/gogs/routers/api/v1/convert"
  10. "github.com/gogits/gogs/routers/api/v1/user"
  11. )
  12. func listUserOrgs(ctx *middleware.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 *middleware.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 *middleware.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 *middleware.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 *middleware.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. }