本站源代码
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.

115 lines
3.1KB

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