org.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package v1
  2. import (
  3. "net/http"
  4. "gogs.io/gogs/internal/context"
  5. "gogs.io/gogs/internal/database"
  6. "gogs.io/gogs/internal/route/api/v1/types"
  7. )
  8. type createOrgRequest struct {
  9. UserName string `json:"username" binding:"Required"`
  10. FullName string `json:"full_name"`
  11. Description string `json:"description"`
  12. Website string `json:"website"`
  13. Location string `json:"location"`
  14. }
  15. func createOrgForUser(c *context.APIContext, apiForm createOrgRequest, user *database.User) {
  16. if c.Written() {
  17. return
  18. }
  19. org := &database.User{
  20. Name: apiForm.UserName,
  21. FullName: apiForm.FullName,
  22. Description: apiForm.Description,
  23. Website: apiForm.Website,
  24. Location: apiForm.Location,
  25. IsActive: true,
  26. Type: database.UserTypeOrganization,
  27. }
  28. if err := database.CreateOrganization(org, user); err != nil {
  29. if database.IsErrUserAlreadyExist(err) ||
  30. database.IsErrNameNotAllowed(err) {
  31. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  32. } else {
  33. c.Error(err, "create organization")
  34. }
  35. return
  36. }
  37. c.JSON(201, toOrganization(org))
  38. }
  39. func listOrgsOfUser(c *context.APIContext, u *database.User, all bool) {
  40. orgs, err := database.Handle.Organizations().List(
  41. c.Req.Context(),
  42. database.ListOrgsOptions{
  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([]*types.Organization, len(orgs))
  52. for i := range orgs {
  53. apiOrgs[i] = toOrganization(orgs[i])
  54. }
  55. c.JSONSuccess(&apiOrgs)
  56. }
  57. func listMyOrgs(c *context.APIContext) {
  58. listOrgsOfUser(c, c.User, true)
  59. }
  60. func createMyOrg(c *context.APIContext, apiForm createOrgRequest) {
  61. createOrgForUser(c, apiForm, c.User)
  62. }
  63. func listUserOrgs(c *context.APIContext) {
  64. u := getUserByParams(c)
  65. if c.Written() {
  66. return
  67. }
  68. listOrgsOfUser(c, u, false)
  69. }
  70. func getOrg(c *context.APIContext) {
  71. c.JSONSuccess(toOrganization(c.Org.Organization))
  72. }
  73. type editOrgRequest struct {
  74. FullName string `json:"full_name"`
  75. Description string `json:"description"`
  76. Website string `json:"website"`
  77. Location string `json:"location"`
  78. }
  79. func editOrg(c *context.APIContext, form editOrgRequest) {
  80. org := c.Org.Organization
  81. if !org.IsOwnedBy(c.User.ID) {
  82. c.Status(http.StatusForbidden)
  83. return
  84. }
  85. err := database.Handle.Users().Update(
  86. c.Req.Context(),
  87. c.Org.Organization.ID,
  88. database.UpdateUserOptions{
  89. FullName: &form.FullName,
  90. Website: &form.Website,
  91. Location: &form.Location,
  92. Description: &form.Description,
  93. },
  94. )
  95. if err != nil {
  96. c.Error(err, "update organization")
  97. return
  98. }
  99. org, err = database.GetOrgByName(org.Name)
  100. if err != nil {
  101. c.Error(err, "get organization")
  102. return
  103. }
  104. c.JSONSuccess(toOrganization(org))
  105. }