org.go 2.5 KB

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