org.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package org
  2. import (
  3. "net/http"
  4. log "unknwon.dev/clog/v2"
  5. "gogs.io/gogs/internal/context"
  6. "gogs.io/gogs/internal/database"
  7. "gogs.io/gogs/internal/form"
  8. )
  9. const (
  10. CREATE = "org/create"
  11. )
  12. func Create(c *context.Context) {
  13. c.Title("new_org")
  14. c.Success(CREATE)
  15. }
  16. func CreatePost(c *context.Context, f form.CreateOrg) {
  17. c.Title("new_org")
  18. if c.HasError() {
  19. c.HTML(http.StatusBadRequest, CREATE)
  20. return
  21. }
  22. org := &database.User{
  23. Name: f.OrgName,
  24. IsActive: true,
  25. Type: database.UserTypeOrganization,
  26. }
  27. if err := database.CreateOrganization(org, c.User); err != nil {
  28. c.Data["Err_OrgName"] = true
  29. switch {
  30. case database.IsErrUserAlreadyExist(err):
  31. c.RenderWithErr(c.Tr("form.org_name_been_taken"), http.StatusUnprocessableEntity, CREATE, &f)
  32. case database.IsErrNameNotAllowed(err):
  33. c.RenderWithErr(c.Tr("org.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), http.StatusBadRequest, CREATE, &f)
  34. default:
  35. c.Error(err, "create organization")
  36. }
  37. return
  38. }
  39. log.Trace("Organization created: %s", org.Name)
  40. c.RedirectSubpath("/org/" + f.OrgName + "/dashboard")
  41. }