feat: 系统模型被引用拒删,系统 apiKey 对非创建者脱敏
- Delete:系统模型被引用 → 拒绝;引用行/自有行仅创建者可删 - List/Get:系统模型 apiKey 对非创建者返回空 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -238,9 +238,36 @@ func (s *modelManageService) CancelChatModel(ctx context.Context, modelType mode
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除模型
|
||||
// Delete 删除模型:系统模型被引用 → 拒绝;引用行/自有行仅创建者可删
|
||||
func (s *modelManageService) Delete(ctx context.Context, req *dto.DeleteModelManageReq) error {
|
||||
_, err := dao.ModelManage.Delete(ctx, req)
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
get, err := dao.ModelManage.GetNotTenantId(ctx, &dto.GetModelManageReq{Id: req.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if get == nil {
|
||||
return nil
|
||||
}
|
||||
// 系统模型被引用 → 拒绝删除(防悬挂)
|
||||
if get.SystemModel != nil && *get.SystemModel {
|
||||
n, err := dao.ModelManage.CountReferences(ctx, get.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n > 0 {
|
||||
return fmt.Errorf("系统模型已被 %d 个用户引用,不能删除", n)
|
||||
}
|
||||
_, err = dao.ModelManage.Delete(ctx, req)
|
||||
return err
|
||||
}
|
||||
// 引用行/自有行:仅创建者可删(删引用行即解除引用)
|
||||
if get.Creator != user.UserName {
|
||||
return fmt.Errorf("无权限操作")
|
||||
}
|
||||
_, err = dao.ModelManage.Delete(ctx, req)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -249,6 +276,17 @@ func (s *modelManageService) Get(ctx context.Context, req *dto.GetModelManageReq
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if get == nil {
|
||||
return new(dto.GetModelManageRes), nil
|
||||
}
|
||||
// 系统模型 apiKey 对非创建者脱敏(引用行/自有行仅本人可见)
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if get.SystemModel != nil && *get.SystemModel && get.Creator != user.UserName {
|
||||
get.ApiKey = ""
|
||||
}
|
||||
res = new(dto.GetModelManageRes)
|
||||
err = gconv.Struct(get, &res.ModelManage)
|
||||
return
|
||||
@@ -294,6 +332,12 @@ func (s *modelManageService) List(ctx context.Context, req *dto.ListModelManageR
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 系统模型 apiKey 对非创建者脱敏(引用行/自有行仅本人可见)
|
||||
for _, row := range list {
|
||||
if row.SystemModel != nil && *row.SystemModel && row.Creator != user.UserName {
|
||||
row.ApiKey = ""
|
||||
}
|
||||
}
|
||||
res = &dto.ListModelManageRes{
|
||||
Total: total,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user