refactor: Update tenant ID handling across the application

This commit is contained in:
wizardchen
2025-11-25 16:30:54 +08:00
parent 9f6f5f5773
commit 64c44c9e27
78 changed files with 437 additions and 601 deletions
+3 -3
View File
@@ -58,7 +58,7 @@ func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
}
// Validate tenant ID
if chunk.TenantID != tenantID.(uint) {
if chunk.TenantID != tenantID.(uint64) {
logger.Warnf(
ctx,
"Tenant has no permission to access chunk, chunk ID: %s, req tenant: %d, chunk tenant: %d",
@@ -184,11 +184,11 @@ func (h *ChunkHandler) validateAndGetChunk(c *gin.Context) (*types.Chunk, string
}
// Validate tenant ID
if chunk.TenantID != tenantID.(uint) || chunk.KnowledgeID != knowledgeID {
if chunk.TenantID != tenantID.(uint64) || chunk.KnowledgeID != knowledgeID {
logger.Warnf(
ctx,
"Tenant has no permission to access chunk, knowledge ID: %s, chunk ID: %s, req tenant: %d, chunk tenant: %d",
knowledgeID, id, tenantID.(uint), chunk.TenantID,
knowledgeID, id, tenantID, chunk.TenantID,
)
return nil, knowledgeID, errors.NewForbiddenError("No permission to access this chunk")
}
+1 -4
View File
@@ -87,9 +87,6 @@ func (e *EvaluationHandler) GetEvaluationResult(c *gin.Context) {
c.Error(errors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error()))
return
}
logger.Infof(ctx, "Retrieving evaluation result, task ID: %s", request.TaskID)
result, err := e.evaluationService.EvaluationResult(ctx, request.TaskID)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
@@ -97,7 +94,7 @@ func (e *EvaluationHandler) GetEvaluationResult(c *gin.Context) {
return
}
logger.Infof(ctx, "Retrieved evaluation result successfully, task ID: %s", request.TaskID)
logger.Info(ctx, "Retrieved evaluation result successfully")
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": result,
+6 -5
View File
@@ -205,7 +205,6 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
c.Error(errors.NewBadRequestError(err.Error()))
return
}
logger.Infof(ctx, "Starting KB configuration update with model IDs, kbId: %s", kbIdStr)
// 获取知识库信息
kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbIdStr)
@@ -233,14 +232,14 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
// 从数据库获取模型详情并验证
llmModel, err := h.modelService.GetModelByID(ctx, req.LLMModelID)
if err != nil || llmModel == nil {
logger.Error(ctx, "LLM model not found", "modelId", req.LLMModelID)
logger.Error(ctx, "LLM model not found")
c.Error(errors.NewBadRequestError("LLM模型不存在"))
return
}
embeddingModel, err := h.modelService.GetModelByID(ctx, req.EmbeddingModelID)
if err != nil || embeddingModel == nil {
logger.Error(ctx, "Embedding model not found", "modelId", req.EmbeddingModelID)
logger.Error(ctx, "Embedding model not found")
c.Error(errors.NewBadRequestError("Embedding模型不存在"))
return
}
@@ -254,7 +253,7 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
if req.VLMConfig != nil && req.Multimodal.Enabled && req.VLMConfig.ModelID != "" {
vllmModel, err := h.modelService.GetModelByID(ctx, req.VLMConfig.ModelID)
if err != nil || vllmModel == nil {
logger.Warn(ctx, "VLM model not found", "modelId", req.VLMConfig.ModelID)
logger.Warn(ctx, "VLM model not found")
} else {
kb.VLMConfig.Enabled = req.VLMConfig.Enabled
kb.VLMConfig.ModelID = req.VLMConfig.ModelID
@@ -335,7 +334,6 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
return
}
logger.Info(ctx, "KB configuration updated successfully", "kbId", kbIdStr)
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "配置更新成功",
@@ -1133,6 +1131,9 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
// 按类型分组模型
for _, model := range models {
if model == nil {
continue
}
// Hide sensitive information for builtin models
baseURL := model.Parameters.BaseURL
apiKey := model.Parameters.APIKey
+4 -4
View File
@@ -48,12 +48,12 @@ func (h *KnowledgeHandler) validateKnowledgeBaseAccess(c *gin.Context) (*types.K
}
// Verify tenant permissions
if kb.TenantID != c.GetUint(types.TenantIDContextKey.String()) {
if kb.TenantID != c.GetUint64(types.TenantIDContextKey.String()) {
logger.Warnf(
ctx,
"Permission denied to access this knowledge base, tenant ID mismatch, "+
"requested tenant ID: %d, knowledge base tenant ID: %d",
c.GetUint(types.TenantIDContextKey.String()),
c.GetUint64(types.TenantIDContextKey.String()),
kb.TenantID,
)
return nil, kbID, errors.NewForbiddenError("Permission denied to access this knowledge base")
@@ -422,11 +422,11 @@ func (h *KnowledgeHandler) GetKnowledgeBatch(c *gin.Context) {
logger.Infof(
ctx,
"Batch retrieving knowledge, tenant ID: %d, number of knowledge IDs: %d",
tenantID.(uint), len(req.IDs),
tenantID, len(req.IDs),
)
// Retrieve knowledge entries in batch
knowledges, err := h.kgService.GetKnowledgeBatch(ctx, tenantID.(uint), req.IDs)
knowledges, err := h.kgService.GetKnowledgeBatch(ctx, tenantID.(uint64), req.IDs)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError("Failed to retrieve knowledge list").WithDetails(err.Error()))
+1 -1
View File
@@ -121,7 +121,7 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
}
// Verify tenant ownership
if kb.TenantID != tenantID.(uint) {
if kb.TenantID != tenantID.(uint64) {
logger.Warnf(
ctx,
"Tenant has no permission to access this knowledge base, knowledge base ID: %s, "+
+8 -8
View File
@@ -34,7 +34,7 @@ func (h *MCPServiceHandler) CreateMCPService(c *gin.Context) {
return
}
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -60,7 +60,7 @@ func (h *MCPServiceHandler) CreateMCPService(c *gin.Context) {
func (h *MCPServiceHandler) ListMCPServices(c *gin.Context) {
ctx := c.Request.Context()
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -86,7 +86,7 @@ func (h *MCPServiceHandler) GetMCPService(c *gin.Context) {
ctx := c.Request.Context()
serviceID := c.Param("id")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -112,7 +112,7 @@ func (h *MCPServiceHandler) UpdateMCPService(c *gin.Context) {
ctx := c.Request.Context()
serviceID := c.Param("id")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -229,7 +229,7 @@ func (h *MCPServiceHandler) DeleteMCPService(c *gin.Context) {
ctx := c.Request.Context()
serviceID := c.Param("id")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -255,7 +255,7 @@ func (h *MCPServiceHandler) TestMCPService(c *gin.Context) {
ctx := c.Request.Context()
serviceID := c.Param("id")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -290,7 +290,7 @@ func (h *MCPServiceHandler) GetMCPServiceTools(c *gin.Context) {
ctx := c.Request.Context()
serviceID := c.Param("id")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -316,7 +316,7 @@ func (h *MCPServiceHandler) GetMCPServiceResources(c *gin.Context) {
ctx := c.Request.Context()
serviceID := c.Param("id")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
+2 -2
View File
@@ -84,7 +84,7 @@ func (h *ModelHandler) CreateModel(c *gin.Context) {
return
}
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
@@ -173,7 +173,7 @@ func (h *ModelHandler) ListModels(c *gin.Context) {
logger.Info(ctx, "Start retrieving model list")
tenantID := c.GetUint(types.TenantIDContextKey.String())
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Tenant ID is empty")
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
+3 -3
View File
@@ -69,7 +69,7 @@ func (h *Handler) CreateSession(c *gin.Context) {
logger.Infof(
ctx,
"Processing session creation request, tenant ID: %d, knowledge base ID: %s, agent mode: %v, agent KBs: %v",
tenantID.(uint),
tenantID,
request.KnowledgeBaseID,
isAgentMode,
hasAgentKnowledgeBases,
@@ -77,7 +77,7 @@ func (h *Handler) CreateSession(c *gin.Context) {
// Create session object with base properties
createdSession := &types.Session{
TenantID: tenantID.(uint),
TenantID: tenantID.(uint64),
KnowledgeBaseID: request.KnowledgeBaseID,
AgentConfig: request.AgentConfig, // Set agent config if provided
}
@@ -281,7 +281,7 @@ func (h *Handler) UpdateSession(c *gin.Context) {
}
session.ID = id
session.TenantID = tenantID.(uint)
session.TenantID = tenantID.(uint64)
// Call service to update session
if err := h.sessionService.UpdateSession(ctx, &session); err != nil {
+1 -1
View File
@@ -192,7 +192,7 @@ func (h *Handler) StopSession(c *gin.Context) {
c.JSON(401, gin.H{"error": "Unauthorized"})
return
}
tenantIDUint := tenantID.(uint)
tenantIDUint := tenantID.(uint64)
// Verify message ownership and status
message, err := h.messageService.GetMessage(ctx, sessionID, assistantMessageID)
+9 -6
View File
@@ -91,7 +91,7 @@ func (h *TenantHandler) GetTenant(c *gin.Context) {
return
}
tenant, err := h.service.GetTenantByID(ctx, uint(id))
tenant, err := h.service.GetTenantByID(ctx, id)
if err != nil {
// Check if this is an application-specific error
if appErr, ok := errors.IsAppError(err); ok {
@@ -136,7 +136,7 @@ func (h *TenantHandler) UpdateTenant(c *gin.Context) {
logger.Infof(ctx, "Updating tenant, ID: %d, Name: %s", id, tenantData.Name)
tenantData.ID = uint(id)
tenantData.ID = id
updatedTenant, err := h.service.UpdateTenant(ctx, &tenantData)
if err != nil {
// Check if this is an application-specific error
@@ -176,7 +176,7 @@ func (h *TenantHandler) DeleteTenant(c *gin.Context) {
logger.Infof(ctx, "Deleting tenant, ID: %d", id)
if err := h.service.DeleteTenant(ctx, uint(id)); err != nil {
if err := h.service.DeleteTenant(ctx, id); err != nil {
// Check if this is an application-specific error
if appErr, ok := errors.IsAppError(err); ok {
logger.Error(ctx, "Failed to delete tenant: application error", appErr)
@@ -337,6 +337,11 @@ func (h *TenantHandler) updateTenantAgentConfigInternal(c *gin.Context) {
// Get existing tenant
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
// Update agent configuration
useCustomPrompt := false
if tenant.AgentConfig != nil {
@@ -557,15 +562,13 @@ func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
// If tenant has no conversation config, return defaults from config.yaml
var response *types.ConversationConfig
if tenant.ConversationConfig == nil {
if tc := tenant.ConversationConfig; tc == nil {
logger.Info(ctx, "Tenant has no conversation config, returning defaults")
response = h.buildDefaultConversationConfig()
} else {
logger.Infof(ctx, "Tenant has conversation config, merging with defaults, Tenant ID: %d", tenant.ID)
// Merge tenant config with defaults, so that newly added fields always have valid values
defaultCfg := h.buildDefaultConversationConfig()
tc := tenant.ConversationConfig
// Prompt related
defaultCfg.UseCustomSystemPrompt = tc.UseCustomSystemPrompt
if !defaultCfg.UseCustomSystemPrompt && tc.Prompt != "" {