refactor(api-keys): use role field and route-level API key guards

Replace scopes JSON with a viewer/contributor/admin role column, move
unsafe-method authorization from path-prefix middleware to declarative
APIKey* guards on routes, and close remaining KB-scope gaps in handlers.
This commit is contained in:
wizardchen
2026-07-06 13:23:55 +08:00
committed by lyingbug
parent 3ace6e657f
commit 9a76a24ce6
28 changed files with 678 additions and 438 deletions
@@ -462,6 +462,13 @@ func (s *customAgentService) GetSuggestedQuestions(
limit = 6
}
if err := types.AuthorizeTenantAPIKeyKnowledgeTargets(ctx, kbIDs, knowledgeIDs); err != nil {
return nil, err
}
if err := types.AuthorizeTenantAPIKeyOptionalTagIDs(ctx, tagIDs); err != nil {
return nil, err
}
// Get tenant ID from context
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
@@ -541,6 +548,12 @@ func (s *customAgentService) GetSuggestedQuestions(
}
}
filteredKBIDs, err := types.FilterKnowledgeBasesForTenantAPIKeyScope(ctx, kbIDs, effectiveKBIDs)
if err != nil {
return nil, err
}
effectiveKBIDs = filteredKBIDs
if len(effectiveKBIDs) == 0 && len(knowledgeIDs) == 0 {
return s.truncateQuestions(result, limit), nil
}