Files

84 lines
2.6 KiB
Go
Raw Permalink Normal View History

2014-06-25 00:44:48 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2014-06-25 00:44:48 -04:00
2014-06-07 13:27:24 +08:00
package org
import (
"errors"
"net/http"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/forms"
2014-06-25 00:44:48 -04:00
)
const (
// tplCreateOrg template path for create organization
tplCreateOrg templates.TplName = "org/create"
2014-06-07 13:27:24 +08:00
)
// Create render the page for create organization
2016-03-11 11:56:52 -05:00
func Create(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("new_org")
2022-03-22 08:03:22 +01:00
if !ctx.Doer.CanCreateOrganization() {
ctx.ServerError("Not allowed", errors.New(ctx.Locale.TrString("org.form.create_org_not_allowed")))
return
}
ctx.Data["visibility"] = setting.Service.DefaultOrgVisibilityMode
ctx.Data["repo_admin_change_team_access"] = true
ctx.HTML(http.StatusOK, tplCreateOrg)
2014-06-25 00:44:48 -04:00
}
// CreatePost response for create organization
2021-01-26 23:36:53 +08:00
func CreatePost(ctx *context.Context) {
form := *web.GetForm(ctx).(*forms.CreateOrgForm)
ctx.Data["Title"] = ctx.Tr("new_org")
2022-03-22 08:03:22 +01:00
if !ctx.Doer.CanCreateOrganization() {
ctx.ServerError("Not allowed", errors.New(ctx.Locale.TrString("org.form.create_org_not_allowed")))
return
2018-01-10 22:34:17 +01:00
}
2014-06-25 00:44:48 -04:00
if ctx.HasError() {
ctx.HTML(http.StatusOK, tplCreateOrg)
2014-06-25 00:44:48 -04:00
return
}
org := &organization.Organization{
Name: form.OrgName,
IsActive: true,
Type: user_model.UserTypeOrganization,
Visibility: form.Visibility,
RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
2014-06-25 00:44:48 -04:00
}
if err := organization.CreateOrganization(ctx, org, ctx.Doer); err != nil {
ctx.Data["Err_OrgName"] = true
switch {
case user_model.IsErrUserAlreadyExist(err):
2026-02-27 20:38:44 +08:00
ctx.RenderWithErrDeprecated(ctx.Tr("form.org_name_been_taken"), tplCreateOrg, &form)
case db.IsErrNameReserved(err):
2026-02-27 20:38:44 +08:00
ctx.RenderWithErrDeprecated(ctx.Tr("org.form.name_reserved", err.(db.ErrNameReserved).Name), tplCreateOrg, &form)
case db.IsErrNamePatternNotAllowed(err):
2026-02-27 20:38:44 +08:00
ctx.RenderWithErrDeprecated(ctx.Tr("org.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form)
case organization.IsErrUserNotAllowedCreateOrg(err):
2026-02-27 20:38:44 +08:00
ctx.RenderWithErrDeprecated(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form)
2014-06-25 00:44:48 -04:00
default:
2018-01-10 22:34:17 +01:00
ctx.ServerError("CreateOrganization", err)
2014-06-25 00:44:48 -04:00
}
return
}
log.Trace("Organization created: %s", org.Name)
2014-06-25 00:44:48 -04:00
ctx.Redirect(org.AsUser().DashboardLink())
2014-06-23 11:40:49 +08:00
}