org.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 "github.com/gogits/go-gogs-client"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/context"
  9. "github.com/gogits/gogs/routers/api/v1/convert"
  10. "github.com/gogits/gogs/routers/api/v1/user"
  11. )
  12. // https://github.com/gogits/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization
  13. func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) {
  14. u := user.GetUserByParams(ctx)
  15. if ctx.Written() {
  16. return
  17. }
  18. org := &models.User{
  19. Name: form.UserName,
  20. FullName: form.FullName,
  21. Description: form.Description,
  22. Website: form.Website,
  23. Location: form.Location,
  24. IsActive: true,
  25. Type: models.USER_TYPE_ORGANIZATION,
  26. }
  27. if err := models.CreateOrganization(org, u); err != nil {
  28. if models.IsErrUserAlreadyExist(err) ||
  29. models.IsErrNameReserved(err) ||
  30. models.IsErrNamePatternNotAllowed(err) {
  31. ctx.Error(422, "", err)
  32. } else {
  33. ctx.Error(500, "CreateOrganization", err)
  34. }
  35. return
  36. }
  37. ctx.JSON(201, convert.ToOrganization(org))
  38. }