本站源代码
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.

77 lines
2.4KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package org
  6. import (
  7. "errors"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. const (
  16. // tplCreateOrg template path for create organization
  17. tplCreateOrg base.TplName = "org/create"
  18. )
  19. // Create render the page for create organization
  20. func Create(ctx *context.Context) {
  21. ctx.Data["Title"] = ctx.Tr("new_org")
  22. ctx.Data["DefaultOrgVisibilityMode"] = setting.Service.DefaultOrgVisibilityMode
  23. if !ctx.User.CanCreateOrganization() {
  24. ctx.ServerError("Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
  25. return
  26. }
  27. ctx.HTML(200, tplCreateOrg)
  28. }
  29. // CreatePost response for create organization
  30. func CreatePost(ctx *context.Context, form auth.CreateOrgForm) {
  31. ctx.Data["Title"] = ctx.Tr("new_org")
  32. if !ctx.User.CanCreateOrganization() {
  33. ctx.ServerError("Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
  34. return
  35. }
  36. if ctx.HasError() {
  37. ctx.HTML(200, tplCreateOrg)
  38. return
  39. }
  40. org := &models.User{
  41. Name: form.OrgName,
  42. IsActive: true,
  43. Type: models.UserTypeOrganization,
  44. Visibility: form.Visibility,
  45. RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
  46. }
  47. if err := models.CreateOrganization(org, ctx.User); err != nil {
  48. ctx.Data["Err_OrgName"] = true
  49. switch {
  50. case models.IsErrUserAlreadyExist(err):
  51. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form)
  52. case models.IsErrNameReserved(err):
  53. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), tplCreateOrg, &form)
  54. case models.IsErrNamePatternNotAllowed(err):
  55. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form)
  56. case models.IsErrUserNotAllowedCreateOrg(err):
  57. ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
  58. default:
  59. ctx.ServerError("CreateOrganization", err)
  60. }
  61. return
  62. }
  63. log.Trace("Organization created: %s", org.Name)
  64. ctx.Redirect(setting.AppSubURL + "/org/" + form.OrgName + "/dashboard")
  65. }
上海开阖软件有限公司 沪ICP备12045867号-1