org.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package org
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "gitote/gitote/pkg/form"
  11. "gitote/gitote/pkg/setting"
  12. log "gopkg.in/clog.v1"
  13. )
  14. const (
  15. // CreateTPL organization page template
  16. CreateTPL = "org/create"
  17. )
  18. // Create renders the page to create an organization
  19. func Create(c *context.Context) {
  20. c.Data["Title"] = c.Tr("new_org")
  21. c.HTML(200, CreateTPL)
  22. }
  23. // CreatePost creates an organization with the body fields
  24. func CreatePost(c *context.Context, f form.CreateOrg) {
  25. c.Data["Title"] = c.Tr("new_org")
  26. if c.HasError() {
  27. c.HTML(200, CreateTPL)
  28. return
  29. }
  30. org := &models.User{
  31. Name: f.OrgName,
  32. IsActive: true,
  33. Type: models.UserTypeOrganization,
  34. }
  35. // Create organization
  36. if err := models.CreateOrganization(org, c.User); err != nil {
  37. c.Data["Err_OrgName"] = true
  38. switch {
  39. case models.IsErrUserAlreadyExist(err):
  40. c.RenderWithErr(c.Tr("form.org_name_been_taken"), CreateTPL, &f)
  41. case models.IsErrNameReserved(err):
  42. c.RenderWithErr(c.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), CreateTPL, &f)
  43. case models.IsErrNamePatternNotAllowed(err):
  44. c.RenderWithErr(c.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CreateTPL, &f)
  45. default:
  46. c.Handle(500, "CreateOrganization", err)
  47. }
  48. return
  49. }
  50. log.Trace("Organization created: %s", org.Name)
  51. c.Redirect(setting.AppSubURL + "/org/" + f.OrgName + "/dashboard")
  52. }