feat(knowledge): enhance knowledge tag batch update with authorization checks

- Updated UpdateKnowledgeTagBatch method to include an authorizedKBID parameter for restricting updates to specific knowledge items.
- Implemented validation to ensure that all requested knowledge IDs belong to the authorized knowledge base.
- Enhanced GetKnowledgeBatch and UpdateKnowledgeTagBatch methods to incorporate agent-specific knowledge base access restrictions.
- Added utility functions to resolve allowed knowledge base IDs for agents based on their configuration.
This commit is contained in:
wizardchen
2026-04-01 21:45:35 +08:00
committed by lyingbug
parent cb504e35b8
commit b1fe7abbe1
3 changed files with 96 additions and 7 deletions
+16 -1
View File
@@ -5269,7 +5269,9 @@ func (s *knowledgeService) UpdateKnowledgeTag(ctx context.Context, knowledgeID s
}
// UpdateKnowledgeTagBatch updates tags for document knowledge items in batch.
func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, updates map[string]*string) error {
// authorizedKBID restricts all updates to knowledge items belonging to this KB;
// pass empty string to skip the check (caller must ensure authorization by other means).
func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, authorizedKBID string, updates map[string]*string) error {
if len(updates) == 0 {
return nil
}
@@ -5292,6 +5294,19 @@ func (s *knowledgeService) UpdateKnowledgeTagBatch(ctx context.Context, updates
return err
}
// Validate all requested IDs were found and belong to the authorized KB
if authorizedKBID != "" {
if len(knowledgeList) != len(updates) {
return werrors.NewForbiddenError("some knowledge IDs are not accessible in the authorized scope")
}
for _, k := range knowledgeList {
if k.KnowledgeBaseID != authorizedKBID {
return werrors.NewForbiddenError(
fmt.Sprintf("knowledge %s does not belong to authorized knowledge base", k.ID))
}
}
}
// Build tag ID map for validation
tagIDSet := make(map[string]bool)
for _, tagID := range updates {