org.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2014 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 org
  5. import (
  6. log "unknwon.dev/clog/v2"
  7. "gogs.io/gogs/internal/context"
  8. "gogs.io/gogs/internal/db"
  9. "gogs.io/gogs/internal/form"
  10. )
  11. const (
  12. CREATE = "org/create"
  13. )
  14. func Create(c *context.Context) {
  15. c.Title("new_org")
  16. c.Success(CREATE)
  17. }
  18. func CreatePost(c *context.Context, f form.CreateOrg) {
  19. c.Title("new_org")
  20. if c.HasError() {
  21. c.Success(CREATE)
  22. return
  23. }
  24. org := &db.User{
  25. Name: f.OrgName,
  26. IsActive: true,
  27. Type: db.USER_TYPE_ORGANIZATION,
  28. }
  29. if err := db.CreateOrganization(org, c.User); err != nil {
  30. c.Data["Err_OrgName"] = true
  31. switch {
  32. case db.IsErrUserAlreadyExist(err):
  33. c.RenderWithErr(c.Tr("form.org_name_been_taken"), CREATE, &f)
  34. case db.IsErrNameReserved(err):
  35. c.RenderWithErr(c.Tr("org.form.name_reserved", err.(db.ErrNameReserved).Name), CREATE, &f)
  36. case db.IsErrNamePatternNotAllowed(err):
  37. c.RenderWithErr(c.Tr("org.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), CREATE, &f)
  38. default:
  39. c.Error(err, "create organization")
  40. }
  41. return
  42. }
  43. log.Trace("Organization created: %s", org.Name)
  44. c.RedirectSubpath("/org/" + f.OrgName + "/dashboard")
  45. }