服务没有所属用户时,可以修改所属用户

This commit is contained in:
GoEdgeLab
2022-12-21 17:07:02 +08:00
parent 48da717baf
commit 800dbdc77c
6 changed files with 175 additions and 9 deletions

View File

@@ -2697,6 +2697,72 @@ func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTim
}
}
// UpdateServerUserId 修改服务所属用户
func (this *ServerDAO) UpdateServerUserId(tx *dbs.Tx, serverId int64, userId int64) error {
if serverId <= 0 {
return nil
}
serverOne, err := this.Query(tx).
Result("https", "tls").
Pk(serverId).
State(ServerStateEnabled).
Find()
if err != nil || serverOne == nil {
return err
}
var server = serverOne.(*Server)
// 修改服务
err = this.Query(tx).
Pk(serverId).
Set("userId", userId).
UpdateQuickly()
if err != nil {
return err
}
// 修改证书相关数据
var sslPolicyIds = []int64{}
var httpsConfig = server.DecodeHTTPS()
if httpsConfig != nil && httpsConfig.SSLPolicyRef != nil && httpsConfig.SSLPolicyRef.SSLPolicyId > 0 {
sslPolicyIds = append(sslPolicyIds, httpsConfig.SSLPolicyRef.SSLPolicyId)
}
var tlsConfig = server.DecodeTLS()
if tlsConfig != nil && tlsConfig.SSLPolicyRef != nil && tlsConfig.SSLPolicyRef.SSLPolicyId > 0 {
sslPolicyIds = append(sslPolicyIds, tlsConfig.SSLPolicyRef.SSLPolicyId)
}
if len(sslPolicyIds) > 0 {
for _, sslPolicyId := range sslPolicyIds {
policy, err := SharedSSLPolicyDAO.FindEnabledSSLPolicy(tx, sslPolicyId)
if err != nil {
return err
}
if policy != nil {
// 修改策略
err = SharedSSLPolicyDAO.UpdatePolicyUser(tx, sslPolicyId, userId)
if err != nil {
return err
}
var certRefs = policy.DecodeCerts()
for _, certRef := range certRefs {
if certRef.CertId > 0 {
// 修改证书
err = SharedSSLCertDAO.UpdateCertUser(tx, certRef.CertId, userId)
if err != nil {
return err
}
}
}
}
}
}
return this.NotifyUpdate(tx, serverId)
}
// NotifyUpdate 同步服务所在的集群
func (this *ServerDAO) NotifyUpdate(tx *dbs.Tx, serverId int64) error {
// 创建任务

View File

@@ -42,10 +42,38 @@ func (this *Server) DecodeHTTPPorts() (ports []int) {
return
}
// DecodeHTTPS 解析HTTPS设置
func (this *Server) DecodeHTTPS() *serverconfigs.HTTPSProtocolConfig {
if len(this.Https) == 0 {
return nil
}
var config = &serverconfigs.HTTPSProtocolConfig{}
err := json.Unmarshal(this.Https, config)
if err != nil {
remotelogs.Error("Server_DecodeHTTPS", err.Error())
}
return config
}
// DecodeTLS 解析TLS设置
func (this *Server) DecodeTLS() *serverconfigs.TLSProtocolConfig {
if len(this.Tls) == 0 {
return nil
}
var config = &serverconfigs.TLSProtocolConfig{}
err := json.Unmarshal(this.Tls, config)
if err != nil {
remotelogs.Error("Server_DecodeTLS", err.Error())
}
return config
}
// DecodeHTTPSPorts 获取HTTPS所有端口
func (this *Server) DecodeHTTPSPorts() (ports []int) {
if len(this.Https) > 0 {
config := &serverconfigs.HTTPSProtocolConfig{}
var config = &serverconfigs.HTTPSProtocolConfig{}
err := json.Unmarshal(this.Https, config)
if err != nil {
return nil

View File

@@ -399,6 +399,17 @@ func (this *SSLCertDAO) CheckUserCert(tx *dbs.Tx, certId int64, userId int64) er
return nil
}
// UpdateCertUser 修改证书所属用户
func (this *SSLCertDAO) UpdateCertUser(tx *dbs.Tx, certId int64, userId int64) error {
if certId <= 0 || userId <= 0 {
return nil
}
return this.Query(tx).
Pk(certId).
Set("userId", userId).
UpdateQuickly()
}
// ListCertsToUpdateOCSP 查找需要更新OCSP的证书
func (this *SSLCertDAO) ListCertsToUpdateOCSP(tx *dbs.Tx, maxTries int, size int64) (result []*SSLCert, err error) {
var nowTime = time.Now().Unix()

View File

@@ -306,6 +306,18 @@ func (this *SSLPolicyDAO) CheckUserPolicy(tx *dbs.Tx, userId int64, policyId int
return nil
}
// UpdatePolicyUser 修改策略所属用户
func (this *SSLPolicyDAO) UpdatePolicyUser(tx *dbs.Tx, policyId int64, userId int64) error {
if policyId <= 0 || userId <= 0 {
return nil
}
return this.Query(tx).
Pk(policyId).
Set("userId", userId).
UpdateQuickly()
}
// NotifyUpdate 通知更新
func (this *SSLPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
serverIds, err := SharedServerDAO.FindAllEnabledServerIdsWithSSLPolicyIds(tx, []int64{policyId})

View File

@@ -1 +1,20 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
)
func (this *SSLPolicy) DecodeCerts() []*sslconfigs.SSLCertRef {
if len(this.Certs) == 0 {
return nil
}
var refs = []*sslconfigs.SSLCertRef{}
err := json.Unmarshal(this.Certs, &refs)
if err != nil {
remotelogs.Error("SSLPolicy_DecodeCerts", err.Error())
}
return refs
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
@@ -770,7 +771,9 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
var tx = this.NullTx()
var fromUser = false
if userId > 0 {
fromUser = true
req.UserId = userId
}
@@ -817,17 +820,19 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
}
// 用户
var pbUser *pb.User = nil
if !fromUser {
user, err := models.SharedUserDAO.FindEnabledBasicUser(tx, int64(server.UserId))
if err != nil {
return nil, err
}
var pbUser *pb.User = nil
if user != nil {
pbUser = &pb.User{
Id: int64(user.Id),
Fullname: user.Fullname,
}
}
}
// 审核结果
var auditingResult = &pb.ServerNameAuditingResult{}
@@ -2239,3 +2244,28 @@ func (this *ServerService) ComposeServerConfig(ctx context.Context, req *pb.Comp
}
return &pb.ComposeServerConfigResponse{ServerConfigJSON: configJSON}, nil
}
// UpdateServerUser 修改服务所属用户
func (this *ServerService) UpdateServerUser(ctx context.Context, req *pb.UpdateServerUserRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx)
if err != nil {
return nil, err
}
if req.ServerId <= 0 {
return nil, errors.New("invalid serverId")
}
if req.UserId <= 0 {
return nil, errors.New("invalid userId")
}
err = this.RunTx(func(tx *dbs.Tx) error {
return models.SharedServerDAO.UpdateServerUserId(tx, req.ServerId, req.UserId)
})
if err != nil {
return nil, err
}
return this.Success()
}