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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if !ctx.User.CanCreateOrganization() {
  21. ctx.NotFound("CanCreateOrganization", nil)
  22. }
  23. ctx.Data["Title"] = ctx.Tr("new_org")
  24. if !ctx.User.CanCreateOrganization() {
  25. ctx.ServerError("Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
  26. return
  27. }
  28. ctx.HTML(200, tplCreateOrg)
  29. }
  30. // CreatePost response for create organization
  31. func CreatePost(ctx *context.Context, form auth.CreateOrgForm) {
  32. if !ctx.User.CanCreateOrganization() {
  33. ctx.NotFound("CanCreateOrganization", nil)
  34. }
  35. ctx.Data["Title"] = ctx.Tr("new_org")
  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. }
  45. if err := models.CreateOrganization(org, ctx.User); err != nil {
  46. ctx.Data["Err_OrgName"] = true
  47. switch {
  48. case models.IsErrUserAlreadyExist(err):
  49. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form)
  50. case models.IsErrNameReserved(err):
  51. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(models.ErrNameReserved).Name), tplCreateOrg, &form)
  52. case models.IsErrNamePatternNotAllowed(err):
  53. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form)
  54. case models.IsErrUserNotAllowedCreateOrg(err):
  55. ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
  56. default:
  57. ctx.ServerError("CreateOrganization", err)
  58. }
  59. return
  60. }
  61. log.Trace("Organization created: %s", org.Name)
  62. ctx.Redirect(setting.AppSubURL + "/org/" + form.OrgName + "/dashboard")
  63. }