refactor: Update 删拷贝分支,引用行锁定配置,系统改名传播引用
- 引用行只改 apiKey/enabled/chatModel,配置锁定跟随系统模型 - 系统模型改名同步引用行 model_name(保列表去重正确) - 非创建者操作他人行 → 无权限(引用改走 referenceSystemModel) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -121,6 +121,9 @@ func (s *modelManageService) ReferenceSystemModel(ctx context.Context, req *dto.
|
||||
}
|
||||
|
||||
// Update 更新模型配置
|
||||
// 引用行:只改 apiKey/enabled/chatModel,配置锁定跟随系统模型(其余字段忽略);
|
||||
// 系统模型:仅创建者(超管)可改配置,改名时同步引用行 model_name;
|
||||
// 用户自有模型:创建者可改全配置。非创建者操作他人行 → 无权限(引用不再靠"编辑系统模型"触发拷贝,改走 referenceSystemModel)。
|
||||
func (s *modelManageService) Update(ctx context.Context, req *dto.UpdateModelManageReq) (res *dto.GetModelManageRes, err error) {
|
||||
err = gfdb.DB(ctx, public.DbNameModelGateway).Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
var get *entity.ModelManage
|
||||
@@ -130,48 +133,31 @@ func (s *modelManageService) Update(ctx context.Context, req *dto.UpdateModelMan
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if get == nil {
|
||||
return fmt.Errorf("模型不存在")
|
||||
}
|
||||
var user *beans.User
|
||||
user, err = utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 1)如果不是创建者,且是系统模型,则需要拷贝
|
||||
if get.Creator != user.UserName {
|
||||
if get.SystemModel != nil && *get.SystemModel {
|
||||
// 拷贝前先查同名:用户已存在同名模型则直接返回用户自己的模型,避免重复拷贝
|
||||
copyName := req.ModelName
|
||||
if g.IsEmpty(copyName) {
|
||||
copyName = get.ModelName
|
||||
}
|
||||
if !g.IsEmpty(copyName) {
|
||||
var exist *entity.ModelManage
|
||||
exist, err = dao.ModelManage.GetByCreatorAndName(ctx, user.UserName, copyName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if exist != nil {
|
||||
res, err = s.Get(ctx, &dto.GetModelManageReq{Id: exist.Id})
|
||||
return
|
||||
}
|
||||
}
|
||||
if g.IsEmpty(req.ApiKey) {
|
||||
return fmt.Errorf("模型apiKey不能为空")
|
||||
}
|
||||
d := new(dto.CreateModelManageReq)
|
||||
err = gconv.Struct(req, d)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var r *dto.CreateModelManageRes
|
||||
r, err = s.Create(ctx, d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err = s.Get(ctx, &dto.GetModelManageReq{
|
||||
Id: r.Id,
|
||||
})
|
||||
return
|
||||
|
||||
// 引用行:只应用个人字段,配置忽略(跟随系统模型;isSuperAdmin=false=个人会话模型开关)
|
||||
if get.RefSystemModelId > 0 {
|
||||
if err = s.CancelChatModel(ctx, get.ModelType, req.ChatModel, false); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = dao.ModelManage.Update(ctx, &dto.UpdateModelManageReq{
|
||||
Id: req.Id,
|
||||
ApiKey: req.ApiKey,
|
||||
Enabled: req.Enabled,
|
||||
ChatModel: req.ChatModel,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 非创建者 → 无权限(引用已由 referenceSystemModel 端点管理,不再拷贝)
|
||||
if get.Creator != user.UserName {
|
||||
return fmt.Errorf("无权限操作")
|
||||
}
|
||||
|
||||
@@ -201,7 +187,13 @@ func (s *modelManageService) Update(ctx context.Context, req *dto.UpdateModelMan
|
||||
return fmt.Errorf("模型名称 [%s] 已存在,同一用户下不能重复添加同名模型", req.ModelName)
|
||||
}
|
||||
}
|
||||
// 3)更新数据
|
||||
// 3)系统模型改名 → 同步引用行 model_name(保列表 DISTINCT ON 去重正确)
|
||||
if get.SystemModel != nil && *get.SystemModel && req.ModelName != "" && req.ModelName != get.ModelName {
|
||||
if _, err = dao.ModelManage.UpdateReferencesName(ctx, get.Id, req.ModelName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 4)更新数据
|
||||
_, err = dao.ModelManage.Update(ctx, req)
|
||||
return
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user