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.

org.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }
  46. if err := models.CreateOrganization(org, ctx.User); err != nil {
  47. ctx.Data["Err_OrgName"] = true
  48. switch {
  49. case models.IsErrUserAlreadyExist(err):
  50. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form)
  51. case models.IsErrNameReserved(err):
  52. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), tplCreateOrg, &form)
  53. case models.IsErrNamePatternNotAllowed(err):
  54. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form)
  55. case models.IsErrUserNotAllowedCreateOrg(err):
  56. ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
  57. default:
  58. ctx.ServerError("CreateOrganization", err)
  59. }
  60. return
  61. }
  62. log.Trace("Organization created: %s", org.Name)
  63. ctx.Redirect(setting.AppSubURL + "/org/" + form.OrgName + "/dashboard")
  64. }