org.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "net/http"
  7. api "github.com/gogs/go-gogs-client"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/db"
  10. "gogs.io/gogs/internal/route/api/v1/convert"
  11. "gogs.io/gogs/internal/route/api/v1/user"
  12. )
  13. func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *db.User) {
  14. if c.Written() {
  15. return
  16. }
  17. org, err := db.Organizations.Create(
  18. c.Req.Context(),
  19. apiForm.UserName,
  20. user.ID,
  21. db.CreateOrganizationOptions{
  22. FullName: apiForm.FullName,
  23. Location: apiForm.Location,
  24. Website: apiForm.Website,
  25. Description: apiForm.Description,
  26. },
  27. )
  28. if err != nil {
  29. if db.IsErrOrganizationAlreadyExist(err) ||
  30. db.IsErrNameNotAllowed(err) {
  31. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  32. } else {
  33. c.Error(err, "create organization")
  34. }
  35. return
  36. }
  37. c.JSON(http.StatusCreated, convert.ToOrganization(org))
  38. }
  39. func listUserOrgs(c *context.APIContext, u *db.User, all bool) {
  40. orgs, err := db.Organizations.List(
  41. c.Req.Context(),
  42. db.ListOrganizationsOptions{
  43. MemberID: u.ID,
  44. IncludePrivateMembers: all,
  45. },
  46. )
  47. if err != nil {
  48. c.Error(err, "list organizations")
  49. return
  50. }
  51. apiOrgs := make([]*api.Organization, len(orgs))
  52. for i := range orgs {
  53. apiOrgs[i] = convert.ToOrganization(orgs[i])
  54. }
  55. c.JSONSuccess(&apiOrgs)
  56. }
  57. func ListMyOrgs(c *context.APIContext) {
  58. listUserOrgs(c, c.User, true)
  59. }
  60. func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
  61. CreateOrgForUser(c, apiForm, c.User)
  62. }
  63. func ListUserOrgs(c *context.APIContext) {
  64. u := user.GetUserByParams(c)
  65. if c.Written() {
  66. return
  67. }
  68. listUserOrgs(c, u, false)
  69. }
  70. func Get(c *context.APIContext) {
  71. c.JSONSuccess(convert.ToOrganization(c.Org.Organization))
  72. }
  73. func Edit(c *context.APIContext, form api.EditOrgOption) {
  74. org := c.Org.Organization
  75. if !org.IsOwnedBy(c.User.ID) {
  76. c.Status(http.StatusForbidden)
  77. return
  78. }
  79. err := db.Users.Update(
  80. c.Req.Context(),
  81. c.Org.Organization.ID,
  82. db.UpdateUserOptions{
  83. FullName: &form.FullName,
  84. Website: &form.Website,
  85. Location: &form.Location,
  86. Description: &form.Description,
  87. },
  88. )
  89. if err != nil {
  90. c.Error(err, "update organization")
  91. return
  92. }
  93. org, err = db.Organizations.GetByName(c.Req.Context(), org.Name)
  94. if err != nil {
  95. c.Error(err, "get organization")
  96. return
  97. }
  98. c.JSONSuccess(convert.ToOrganization(org))
  99. }