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.4KB

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