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

org.go 3.2 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. )
  14. // CreateOrg api for create organization
  15. func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
  16. // swagger:operation POST /admin/users/{username}/orgs admin adminCreateOrg
  17. // ---
  18. // summary: Create an organization
  19. // consumes:
  20. // - application/json
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: username
  25. // in: path
  26. // description: username of the user that will own the created organization
  27. // type: string
  28. // required: true
  29. // - name: organization
  30. // in: body
  31. // required: true
  32. // schema: { "$ref": "#/definitions/CreateOrgOption" }
  33. // responses:
  34. // "201":
  35. // "$ref": "#/responses/Organization"
  36. // "403":
  37. // "$ref": "#/responses/forbidden"
  38. // "422":
  39. // "$ref": "#/responses/validationError"
  40. u := user.GetUserByParams(ctx)
  41. if ctx.Written() {
  42. return
  43. }
  44. visibility := api.VisibleTypePublic
  45. if form.Visibility != "" {
  46. visibility = api.VisibilityModes[form.Visibility]
  47. }
  48. org := &models.User{
  49. Name: form.UserName,
  50. FullName: form.FullName,
  51. Description: form.Description,
  52. Website: form.Website,
  53. Location: form.Location,
  54. IsActive: true,
  55. Type: models.UserTypeOrganization,
  56. Visibility: visibility,
  57. }
  58. if err := models.CreateOrganization(org, u); err != nil {
  59. if models.IsErrUserAlreadyExist(err) ||
  60. models.IsErrNameReserved(err) ||
  61. models.IsErrNamePatternNotAllowed(err) {
  62. ctx.Error(http.StatusUnprocessableEntity, "", err)
  63. } else {
  64. ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
  65. }
  66. return
  67. }
  68. ctx.JSON(http.StatusCreated, convert.ToOrganization(org))
  69. }
  70. //GetAllOrgs API for getting information of all the organizations
  71. func GetAllOrgs(ctx *context.APIContext) {
  72. // swagger:operation GET /admin/orgs admin adminGetAllOrgs
  73. // ---
  74. // summary: List all organizations
  75. // produces:
  76. // - application/json
  77. // parameters:
  78. // - name: page
  79. // in: query
  80. // description: page number of results to return (1-based)
  81. // type: integer
  82. // - name: limit
  83. // in: query
  84. // description: page size of results, maximum page size is 50
  85. // type: integer
  86. // responses:
  87. // "200":
  88. // "$ref": "#/responses/OrganizationList"
  89. // "403":
  90. // "$ref": "#/responses/forbidden"
  91. users, _, err := models.SearchUsers(&models.SearchUserOptions{
  92. Type: models.UserTypeOrganization,
  93. OrderBy: models.SearchOrderByAlphabetically,
  94. Page: ctx.QueryInt("page"),
  95. PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
  96. Private: true,
  97. })
  98. if err != nil {
  99. ctx.Error(http.StatusInternalServerError, "SearchOrganizations", err)
  100. return
  101. }
  102. orgs := make([]*api.Organization, len(users))
  103. for i := range users {
  104. orgs[i] = convert.ToOrganization(users[i])
  105. }
  106. ctx.JSON(http.StatusOK, &orgs)
  107. }