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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package org
  5. import (
  6. "errors"
  7. "net/http"
  8. "code.gitea.io/gitea/models/db"
  9. "code.gitea.io/gitea/models/organization"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/web"
  16. "code.gitea.io/gitea/services/forms"
  17. )
  18. const (
  19. // tplCreateOrg template path for create organization
  20. tplCreateOrg base.TplName = "org/create"
  21. )
  22. // Create render the page for create organization
  23. func Create(ctx *context.Context) {
  24. ctx.Data["Title"] = ctx.Tr("new_org")
  25. ctx.Data["DefaultOrgVisibilityMode"] = setting.Service.DefaultOrgVisibilityMode
  26. if !ctx.Doer.CanCreateOrganization() {
  27. ctx.ServerError("Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
  28. return
  29. }
  30. ctx.HTML(http.StatusOK, tplCreateOrg)
  31. }
  32. // CreatePost response for create organization
  33. func CreatePost(ctx *context.Context) {
  34. form := *web.GetForm(ctx).(*forms.CreateOrgForm)
  35. ctx.Data["Title"] = ctx.Tr("new_org")
  36. if !ctx.Doer.CanCreateOrganization() {
  37. ctx.ServerError("Not allowed", errors.New(ctx.Tr("org.form.create_org_not_allowed")))
  38. return
  39. }
  40. if ctx.HasError() {
  41. ctx.HTML(http.StatusOK, tplCreateOrg)
  42. return
  43. }
  44. org := &organization.Organization{
  45. Name: form.OrgName,
  46. IsActive: true,
  47. Type: user_model.UserTypeOrganization,
  48. Visibility: form.Visibility,
  49. RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
  50. }
  51. if err := organization.CreateOrganization(org, ctx.Doer); err != nil {
  52. ctx.Data["Err_OrgName"] = true
  53. switch {
  54. case user_model.IsErrUserAlreadyExist(err):
  55. ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form)
  56. case db.IsErrNameReserved(err):
  57. ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(db.ErrNameReserved).Name), tplCreateOrg, &form)
  58. case db.IsErrNamePatternNotAllowed(err):
  59. ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form)
  60. case organization.IsErrUserNotAllowedCreateOrg(err):
  61. ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
  62. default:
  63. ctx.ServerError("CreateOrganization", err)
  64. }
  65. return
  66. }
  67. log.Trace("Organization created: %s", org.Name)
  68. ctx.Redirect(org.AsUser().DashboardLink())
  69. }