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.8 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/gitea/modules/structs"
  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. 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. // responses:
  77. // "200":
  78. // "$ref": "#/responses/OrganizationList"
  79. // "403":
  80. // "$ref": "#/responses/forbidden"
  81. users, _, err := models.SearchUsers(&models.SearchUserOptions{
  82. Type: models.UserTypeOrganization,
  83. OrderBy: models.SearchOrderByAlphabetically,
  84. PageSize: -1,
  85. })
  86. if err != nil {
  87. ctx.Error(500, "SearchOrganizations", err)
  88. return
  89. }
  90. orgs := make([]*api.Organization, len(users))
  91. for i := range users {
  92. orgs[i] = convert.ToOrganization(users[i])
  93. }
  94. ctx.JSON(200, &orgs)
  95. }