org.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "github.com/gogits/gogs/models"
  7. "github.com/gogits/gogs/modules/auth"
  8. "github.com/gogits/gogs/modules/base"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/middleware"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. const (
  14. CREATE base.TplName = "org/create"
  15. )
  16. func Create(ctx *middleware.Context) {
  17. ctx.Data["Title"] = ctx.Tr("new_org")
  18. ctx.HTML(200, CREATE)
  19. }
  20. func CreatePost(ctx *middleware.Context, form auth.CreateOrgForm) {
  21. ctx.Data["Title"] = ctx.Tr("new_org")
  22. if ctx.HasError() {
  23. ctx.HTML(200, CREATE)
  24. return
  25. }
  26. org := &models.User{
  27. Name: form.OrgName,
  28. IsActive: true,
  29. Type: models.ORGANIZATION,
  30. }
  31. if err := models.CreateOrganization(org, ctx.User); err != nil {
  32. ctx.Data["Err_OrgName"] = true
  33. switch {
  34. case models.IsErrUserAlreadyExist(err):
  35. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form)
  36. case models.IsErrNameReserved(err):
  37. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form)
  38. case models.IsErrNamePatternNotAllowed(err):
  39. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form)
  40. default:
  41. ctx.Handle(500, "CreateOrganization", err)
  42. }
  43. return
  44. }
  45. log.Trace("Organization created: %s", org.Name)
  46. ctx.Redirect(setting.AppSubUrl + "/org/" + form.OrgName + "/dashboard")
  47. }