org.go 2.4 KB

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