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.

64 lines
1.6 KiB

8 years ago
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. "code.gitea.io/gitea/routers/api/v1/user"
  11. )
  12. // CreateOrg api for create organization
  13. func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
  14. // swagger:operation POST /admin/users/{username}/orgs admin adminCreateOrg
  15. // ---
  16. // summary: Create an organization
  17. // consumes:
  18. // - application/json
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: username
  23. // in: path
  24. // description: username of the user that will own the created organization
  25. // type: string
  26. // required: true
  27. // responses:
  28. // "201":
  29. // "$ref": "#/responses/Organization"
  30. // "403":
  31. // "$ref": "#/responses/forbidden"
  32. // "422":
  33. // "$ref": "#/responses/validationError"
  34. u := user.GetUserByParams(ctx)
  35. if ctx.Written() {
  36. return
  37. }
  38. org := &models.User{
  39. Name: form.UserName,
  40. FullName: form.FullName,
  41. Description: form.Description,
  42. Website: form.Website,
  43. Location: form.Location,
  44. IsActive: true,
  45. Type: models.UserTypeOrganization,
  46. }
  47. if err := models.CreateOrganization(org, u); err != nil {
  48. if models.IsErrUserAlreadyExist(err) ||
  49. models.IsErrNameReserved(err) ||
  50. models.IsErrNamePatternNotAllowed(err) {
  51. ctx.Error(422, "", err)
  52. } else {
  53. ctx.Error(500, "CreateOrganization", err)
  54. }
  55. return
  56. }
  57. ctx.JSON(201, convert.ToOrganization(org))
  58. }