1.4.5.2
This commit is contained in:
187
EdgeUser/internal/web/actions/default/ns/domains/create.go
Normal file
187
EdgeUser/internal/web/actions/default/ns/domains/create.go
Normal file
@@ -0,0 +1,187 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package domains
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/dns/domains/domainutils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/ns/nsutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CreateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CreateAction) Init() {
|
||||
this.Nav("", "", "create")
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunGet(params struct{}) {
|
||||
userConfigResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.UserContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeNSUserConfig})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["configIsValid"] = false
|
||||
if len(userConfigResp.ValueJSON) > 0 {
|
||||
var config = &dnsconfigs.NSUserConfig{}
|
||||
err = json.Unmarshal(userConfigResp.ValueJSON, config)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if config.DefaultClusterId > 0 {
|
||||
this.Data["configIsValid"] = true
|
||||
}
|
||||
}
|
||||
|
||||
// 套餐限制
|
||||
maxDomains, canCreate, err := nsutils.CheckDomainsQuote(this.UserContext(), 1)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
this.Data["quotaMaxDomains"] = maxDomains
|
||||
this.Data["quotaCanCreate"] = canCreate
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunPost(params struct {
|
||||
AddingType string
|
||||
|
||||
Name string
|
||||
Names string // 如果批量的话
|
||||
UserId int64
|
||||
GroupId int64
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
// 检查套餐限制
|
||||
_, canCreate, err := nsutils.CheckDomainsQuote(this.UserContext(), 1)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !canCreate {
|
||||
this.Fail("域名个数已超出最大限额")
|
||||
}
|
||||
|
||||
var groupIds = []int64{}
|
||||
if params.GroupId > 0 {
|
||||
groupIds = append(groupIds, params.GroupId)
|
||||
}
|
||||
|
||||
if params.AddingType == "batch" { // 批量添加
|
||||
defer this.CreateLogInfo(codes.NS_LogCreateNSDomainsBatch)
|
||||
|
||||
params.Must.
|
||||
Field("names", params.Names).
|
||||
Require("请输入域名")
|
||||
|
||||
var names = []string{}
|
||||
for _, name := range strings.Split(params.Names, "\n") {
|
||||
name = strings.TrimSpace(name)
|
||||
if len(name) == 0 {
|
||||
continue
|
||||
}
|
||||
if !domainutils.ValidateDomainFormat(name) {
|
||||
this.Fail("域名 '" + name + "' 格式不正确")
|
||||
}
|
||||
if !lists.ContainsString(names, name) {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
|
||||
if len(names) == 0 {
|
||||
this.FailField("names", "请输入域名")
|
||||
return
|
||||
}
|
||||
|
||||
// 检查域名是否已经存在
|
||||
{
|
||||
existResp, err := this.RPC().NSDomainRPC().ExistNSDomains(this.UserContext(), &pb.ExistNSDomainsRequest{
|
||||
Names: names,
|
||||
UserId: params.UserId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(existResp.ExistingNames) > 0 {
|
||||
this.Fail("域名 " + strings.Join(existResp.ExistingNames, ", ") + " 已经存在,无法重复添加")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, err := this.RPC().NSDomainRPC().CreateNSDomains(this.UserContext(), &pb.CreateNSDomainsRequest{
|
||||
UserId: params.UserId,
|
||||
Names: names,
|
||||
NsDomainGroupIds: groupIds,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
} else { // 单个添加
|
||||
var domainId int64
|
||||
defer func() {
|
||||
this.CreateLogInfo(codes.NSDomain_LogCreateNSDomain, domainId)
|
||||
}()
|
||||
|
||||
params.Name = strings.ToLower(params.Name)
|
||||
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入域名").
|
||||
Expect(func() (message string, success bool) {
|
||||
success = domainutils.ValidateDomainFormat(params.Name)
|
||||
if !success {
|
||||
message = "请输入正确的域名"
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
// 检查域名是否已经存在
|
||||
{
|
||||
existResp, err := this.RPC().NSDomainRPC().ExistNSDomains(this.UserContext(), &pb.ExistNSDomainsRequest{
|
||||
Names: []string{params.Name},
|
||||
UserId: params.UserId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if len(existResp.ExistingNames) > 0 {
|
||||
this.Fail("域名 " + strings.Join(existResp.ExistingNames, ", ") + " 已经存在,无法重复添加")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
createResp, err := this.RPC().NSDomainRPC().CreateNSDomain(this.UserContext(), &pb.CreateNSDomainRequest{
|
||||
UserId: params.UserId,
|
||||
Name: params.Name,
|
||||
NsDomainGroupIds: groupIds,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
domainId = createResp.NsDomainId
|
||||
|
||||
this.Success()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user