From f6c63bb9785eec8ac1e20b78c883fb2b2d99a2fb Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sat, 25 Jun 2022 19:21:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BF=AE=E6=94=B9=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=89=80=E5=9C=A8=E5=88=86=E7=BB=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/const/const.go | 2 +- internal/db/models/server_dao.go | 21 ++++++++- internal/db/models/server_group_dao.go | 11 +++++ internal/rpc/services/service_server.go | 57 +++++++++++++++++++++++-- 4 files changed, 86 insertions(+), 5 deletions(-) diff --git a/internal/const/const.go b/internal/const/const.go index 213ab282..03f3a50c 100644 --- a/internal/const/const.go +++ b/internal/const/const.go @@ -19,7 +19,7 @@ const ( // 其他节点版本号,用来检测是否有需要升级的节点 NodeVersion = "0.4.9" - UserNodeVersion = "0.3.4" + UserNodeVersion = "0.3.5" AuthorityNodeVersion = "0.0.2" MonitorNodeVersion = "0.0.4" DNSNodeVersion = "0.2.3" diff --git a/internal/db/models/server_dao.go b/internal/db/models/server_dao.go index a599668d..33e033c9 100644 --- a/internal/db/models/server_dao.go +++ b/internal/db/models/server_dao.go @@ -338,12 +338,31 @@ func (this *ServerDAO) UpdateServerBasic(tx *dbs.Tx, serverId int64, name string return nil } +// UpdateServerGroupIds 修改服务所在分组 +func (this *ServerDAO) UpdateServerGroupIds(tx *dbs.Tx, serverId int64, groupIds []int64) error { + if groupIds == nil { + groupIds = []int64{} + } + groupIdsJSON, err := json.Marshal(groupIds) + if err != nil { + return err + } + err = this.Query(tx). + Pk(serverId). + Set("groupIds", groupIdsJSON). + UpdateQuickly() + if err != nil { + return err + } + return this.NotifyUpdate(tx, serverId) +} + // UpdateUserServerBasic 设置用户相关的基本信息 func (this *ServerDAO) UpdateUserServerBasic(tx *dbs.Tx, serverId int64, name string) error { if serverId <= 0 { return errors.New("serverId should not be smaller than 0") } - op := NewServerOperator() + var op = NewServerOperator() op.Id = serverId op.Name = name diff --git a/internal/db/models/server_group_dao.go b/internal/db/models/server_group_dao.go index 939cf886..415212ae 100644 --- a/internal/db/models/server_group_dao.go +++ b/internal/db/models/server_group_dao.go @@ -410,6 +410,17 @@ func (this *ServerGroupDAO) CheckUserGroup(tx *dbs.Tx, userId int64, groupId int return nil } +// ExistsGroup 检查分组ID是否存在 +func (this *ServerGroupDAO) ExistsGroup(tx *dbs.Tx, groupId int64) (bool, error) { + if groupId <= 0 { + return false, nil + } + return this.Query(tx). + Pk(groupId). + State(ServerGroupStateEnabled). + Exist() +} + // NotifyUpdate 通知更新 func (this *ServerGroupDAO) NotifyUpdate(tx *dbs.Tx, groupId int64) error { serverIds, err := SharedServerDAO.FindAllEnabledServerIdsWithGroupId(tx, groupId) diff --git a/internal/rpc/services/service_server.go b/internal/rpc/services/service_server.go index ecc9e0c6..fed22f2f 100644 --- a/internal/rpc/services/service_server.go +++ b/internal/rpc/services/service_server.go @@ -192,6 +192,56 @@ func (this *ServerService) UpdateServerBasic(ctx context.Context, req *pb.Update return this.Success() } +// UpdateServerGroupIds 修改服务所在分组 +func (this *ServerService) UpdateServerGroupIds(ctx context.Context, req *pb.UpdateServerGroupIdsRequest) (*pb.RPCSuccess, error) { + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + if userId > 0 { + err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId) + if err != nil { + return nil, err + } + } + + // 检查分组IDs + for _, groupId := range req.ServerGroupIds { + if userId > 0 { + err = models.SharedServerGroupDAO.CheckUserGroup(tx, userId, groupId) + if err != nil { + return nil, err + } + } else { + b, err := models.SharedServerGroupDAO.ExistsGroup(tx, groupId) + if err != nil { + return nil, err + } + if !b { + continue + } + } + } + + // 增加默认分组 + if userId > 0 { + config, err := models.SharedSysSettingDAO.ReadUserServerConfig(tx) + if err == nil && config.GroupId > 0 && !lists.ContainsInt64(req.ServerGroupIds, config.GroupId) { + req.ServerGroupIds = append(req.ServerGroupIds, config.GroupId) + } + } + + // 修改 + err = models.SharedServerDAO.UpdateServerGroupIds(tx, req.ServerId, req.ServerGroupIds) + if err != nil { + return nil, err + } + + return this.Success() +} + // UpdateServerIsOn 修改服务是否启用 func (this *ServerService) UpdateServerIsOn(ctx context.Context, req *pb.UpdateServerIsOnRequest) (*pb.RPCSuccess, error) { _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -199,7 +249,7 @@ func (this *ServerService) UpdateServerIsOn(ctx context.Context, req *pb.UpdateS return nil, err } - tx := this.NullTx() + var tx = this.NullTx() if userId > 0 { err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId) @@ -832,8 +882,9 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn continue } pbGroups = append(pbGroups, &pb.ServerGroup{ - Id: int64(group.Id), - Name: group.Name, + Id: int64(group.Id), + Name: group.Name, + UserId: int64(group.UserId), }) } }