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 2.6 kB

9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. api "code.gitea.io/sdk/gitea"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  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. org := &models.User{
  44. Name: form.UserName,
  45. FullName: form.FullName,
  46. Description: form.Description,
  47. Website: form.Website,
  48. Location: form.Location,
  49. IsActive: true,
  50. Type: models.UserTypeOrganization,
  51. }
  52. if err := models.CreateOrganization(org, u); err != nil {
  53. if models.IsErrUserAlreadyExist(err) ||
  54. models.IsErrNameReserved(err) ||
  55. models.IsErrNamePatternNotAllowed(err) {
  56. ctx.Error(422, "", err)
  57. } else {
  58. ctx.Error(500, "CreateOrganization", err)
  59. }
  60. return
  61. }
  62. ctx.JSON(201, convert.ToOrganization(org))
  63. }
  64. //GetAllOrgs API for getting information of all the organizations
  65. func GetAllOrgs(ctx *context.APIContext) {
  66. // swagger:operation GET /admin/orgs admin adminGetAllOrgs
  67. // ---
  68. // summary: List all organizations
  69. // produces:
  70. // - application/json
  71. // responses:
  72. // "200":
  73. // "$ref": "#/responses/OrganizationList"
  74. // "403":
  75. // "$ref": "#/responses/forbidden"
  76. users, _, err := models.SearchUsers(&models.SearchUserOptions{
  77. Type: models.UserTypeOrganization,
  78. OrderBy: models.SearchOrderByAlphabetically,
  79. PageSize: -1,
  80. })
  81. if err != nil {
  82. ctx.Error(500, "SearchOrganizations", err)
  83. return
  84. }
  85. orgs := make([]*api.Organization, len(users))
  86. for i := range users {
  87. orgs[i] = convert.ToOrganization(users[i])
  88. }
  89. ctx.JSON(200, &orgs)
  90. }