You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

org.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package admin
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/routers/api/v1/user"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. )
  15. // CreateOrg api for create organization
  16. func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
  17. // swagger:operation POST /admin/users/{username}/orgs admin adminCreateOrg
  18. // ---
  19. // summary: Create an organization
  20. // consumes:
  21. // - application/json
  22. // produces:
  23. // - application/json
  24. // parameters:
  25. // - name: username
  26. // in: path
  27. // description: username of the user that will own the created organization
  28. // type: string
  29. // required: true
  30. // - name: organization
  31. // in: body
  32. // required: true
  33. // schema: { "$ref": "#/definitions/CreateOrgOption" }
  34. // responses:
  35. // "201":
  36. // "$ref": "#/responses/Organization"
  37. // "403":
  38. // "$ref": "#/responses/forbidden"
  39. // "422":
  40. // "$ref": "#/responses/validationError"
  41. u := user.GetUserByParams(ctx)
  42. if ctx.Written() {
  43. return
  44. }
  45. visibility := api.VisibleTypePublic
  46. if form.Visibility != "" {
  47. visibility = api.VisibilityModes[form.Visibility]
  48. }
  49. org := &models.User{
  50. Name: form.UserName,
  51. FullName: form.FullName,
  52. Description: form.Description,
  53. Website: form.Website,
  54. Location: form.Location,
  55. IsActive: true,
  56. Type: models.UserTypeOrganization,
  57. Visibility: visibility,
  58. }
  59. if err := models.CreateOrganization(org, u); err != nil {
  60. if models.IsErrUserAlreadyExist(err) ||
  61. models.IsErrNameReserved(err) ||
  62. models.IsErrNameCharsNotAllowed(err) ||
  63. models.IsErrNamePatternNotAllowed(err) {
  64. ctx.Error(http.StatusUnprocessableEntity, "", err)
  65. } else {
  66. ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
  67. }
  68. return
  69. }
  70. ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
  71. }
  72. //GetAllOrgs API for getting information of all the organizations
  73. func GetAllOrgs(ctx *context.APIContext) {
  74. // swagger:operation GET /admin/orgs admin adminGetAllOrgs
  75. // ---
  76. // summary: List all organizations
  77. // produces:
  78. // - application/json
  79. // parameters:
  80. // - name: page
  81. // in: query
  82. // description: page number of results to return (1-based)
  83. // type: integer
  84. // - name: limit
  85. // in: query
  86. // description: page size of results, maximum page size is 50
  87. // type: integer
  88. // responses:
  89. // "200":
  90. // "$ref": "#/responses/OrganizationList"
  91. // "403":
  92. // "$ref": "#/responses/forbidden"
  93. users, _, err := models.SearchUsers(&models.SearchUserOptions{
  94. Type: models.UserTypeOrganization,
  95. OrderBy: models.SearchOrderByAlphabetically,
  96. ListOptions: utils.GetListOptions(ctx),
  97. Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate},
  98. })
  99. if err != nil {
  100. ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
  101. return
  102. }
  103. orgs := make([]*api.Organization, len(users))
  104. for i := range users {
  105. orgs[i] = convert.ToOrganization(users[i])
  106. }
  107. ctx.JSON(http.StatusOK, &orgs)
  108. }