You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

8 years ago
  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. "errors"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. const (
  15. // tplCreateOrg template path for create organization
  16. tplCreateOrg base.TplName = "org/create"
  17. )
  18. // Create render the page for create organization
  19. func Create(ctx *context.Context) {
  20. ctx.Data["Title"] = ctx.Tr("new_org")
  21. if !ctx.User.CanCreateOrganization() {
  22. ctx.Handle(500, "Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
  23. return
  24. }
  25. ctx.HTML(200, tplCreateOrg)
  26. }
  27. // CreatePost response for create organization
  28. func CreatePost(ctx *context.Context, form auth.CreateOrgForm) {
  29. ctx.Data["Title"] = ctx.Tr("new_org")
  30. if ctx.HasError() {
  31. ctx.HTML(200, tplCreateOrg)
  32. return
  33. }
  34. org := &models.User{
  35. Name: form.OrgName,
  36. IsActive: true,
  37. Type: models.UserTypeOrganization,
  38. }
  39. if err := models.CreateOrganization(org, ctx.User); err != nil {
  40. ctx.Data["Err_OrgName"] = true
  41. switch {
  42. case models.IsErrUserAlreadyExist(err):
  43. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form)
  44. case models.IsErrNameReserved(err):
  45. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), tplCreateOrg, &form)
  46. case models.IsErrNamePatternNotAllowed(err):
  47. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form)
  48. case models.IsErrUserNotAllowedCreateOrg(err):
  49. ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
  50. default:
  51. ctx.Handle(500, "CreateOrganization", err)
  52. }
  53. return
  54. }
  55. log.Trace("Organization created: %s", org.Name)
  56. ctx.Redirect(setting.AppSubURL + "/org/" + form.OrgName + "/dashboard")
  57. }