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

9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // - name: organization
  28. // in: body
  29. // required: true
  30. // schema: { "$ref": "#/definitions/CreateOrgOption" }
  31. // responses:
  32. // "201":
  33. // "$ref": "#/responses/Organization"
  34. // "403":
  35. // "$ref": "#/responses/forbidden"
  36. // "422":
  37. // "$ref": "#/responses/validationError"
  38. u := user.GetUserByParams(ctx)
  39. if ctx.Written() {
  40. return
  41. }
  42. org := &models.User{
  43. Name: form.UserName,
  44. FullName: form.FullName,
  45. Description: form.Description,
  46. Website: form.Website,
  47. Location: form.Location,
  48. IsActive: true,
  49. Type: models.UserTypeOrganization,
  50. }
  51. if err := models.CreateOrganization(org, u); err != nil {
  52. if models.IsErrUserAlreadyExist(err) ||
  53. models.IsErrNameReserved(err) ||
  54. models.IsErrNamePatternNotAllowed(err) {
  55. ctx.Error(422, "", err)
  56. } else {
  57. ctx.Error(500, "CreateOrganization", err)
  58. }
  59. return
  60. }
  61. ctx.JSON(201, convert.ToOrganization(org))
  62. }