Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(kbshare): enhance sharing functionality with soft-delete handling and shared access

wizardchenb3d743a0a9ba
diff --git a/internal/application/repository/kbshare.go b/internal/application/repository/kbshare.go
index f3fce1442..4323fa735 100644
--- a/internal/application/repository/kbshare.go
+++ b/internal/application/repository/kbshare.go
@@ -108,13 +108,15 @@ func (r *kbShareRepository) ListByKnowledgeBase(ctx context.Context, kbID string
return shares, nil
}
-// ListByOrganization lists all share records for an organization
+// ListByOrganization lists all share records for an organization.
+// Excludes shares whose knowledge base has been soft-deleted.
func (r *kbShareRepository) ListByOrganization(ctx context.Context, orgID string) ([]*types.KnowledgeBaseShare, error) {
var shares []*types.KnowledgeBaseShare
err := r.db.WithContext(ctx).
+ Joins("JOIN knowledge_bases ON knowledge_bases.id = kb_shares.knowledge_base_id AND knowledge_bases.deleted_at IS NULL").
Preload("KnowledgeBase").
- Where("organization_id = ?", orgID).
- Order("created_at DESC").
+ Where("kb_shares.organization_id = ? AND kb_shares.deleted_at IS NULL", orgID).
+ Order("kb_shares.created_at DESC").
Find(&shares).Error
if err != nil {
@@ -124,12 +126,13 @@ func (r *kbShareRepository) ListByOrganization(ctx context.Context, orgID string
}
// ListSharedKBsForUser lists all knowledge bases shared to organizations that the user belongs to.
-// Excludes shares for soft-deleted organizations so users no longer see KBs after the space is deleted.
+// Excludes shares for soft-deleted organizations and soft-deleted knowledge bases.
func (r *kbShareRepository) ListSharedKBsForUser(ctx context.Context, userID string) ([]*types.KnowledgeBaseShare, error) {
var shares []*types.KnowledgeBaseShare
- // Get shares for organizations that the user is a member of; exclude deleted organizations
+ // Get shares for organizations that the user is a member of; exclude deleted orgs and deleted KBs
err := r.db.WithContext(ctx).
+ Joins("JOIN knowledge_bases ON knowledge_bases.id = kb_shares.knowledge_base_id AND knowledge_bases.deleted_at IS NULL").
Preload("KnowledgeBase").
Preload("Organization").
Joins("JOIN organization_members ON organization_members.organization_id = kb_shares.organization_id").
diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go
index 0d621f968..4cb3248bb 100644
--- a/internal/application/service/agent_service.go
+++ b/internal/application/service/agent_service.go
@@ -551,8 +551,8 @@ func (s *agentService) getSelectedDocumentInfos(ctx context.Context, knowledgeID
tenantID = tid
}
- // Fetch knowledge metadata
- knowledges, err := s.knowledgeService.GetKnowledgeBatch(ctx, tenantID, knowledgeIDs)
+ // Fetch knowledge metadata (include docs from shared KBs the user has access to)
+ knowledges, err := s.knowledgeService.GetKnowledgeBatchWithSharedAccess(ctx, tenantID, knowledgeIDs)
if err != nil {
return nil, fmt.Errorf("failed to get knowledge batch: %w", err)
}
diff --git a/internal/application/service/chat_pipline/extract_entity.go b/internal/application/service/chat_pipline/extract_entity.go
index e1f975712..2652e0bad 100644
--- a/internal/application/service/chat_pipline/extract_entity.go
+++ b/internal/application/service/chat_pipline/extract_entity.go
@@ -22,15 +22,17 @@ type PluginExtractEntity struct {
modelService interfaces.ModelService // Model service for calling large language models
template *types.PromptTemplateStructured // Template for generating prompts
knowledgeBaseRepo interfaces.KnowledgeBaseRepository
+ knowledgeService interfaces.KnowledgeService // For shared KB document resolution
knowledgeRepo interfaces.KnowledgeRepository
}
-// NewPluginRewrite creates a new query rewriting plugin instance
+// NewPluginExtractEntity creates a new extract-entity plugin instance
// Also registers the plugin with the event manager
func NewPluginExtractEntity(
eventManager *EventManager,
modelService interfaces.ModelService,
knowledgeBaseRepo interfaces.KnowledgeBaseRepository,
+ knowledgeService interfaces.KnowledgeService,
knowledgeRepo interfaces.KnowledgeRepository,
config *config.Config,
) *PluginExtractEntity {
@@ -38,6 +40,7 @@ func NewPluginExtractEntity(
modelService: modelService,
template: config.ExtractManager.ExtractEntity,
knowledgeBaseRepo: knowledgeBaseRepo,
+ knowledgeService: knowledgeService,
knowledgeRepo: knowledgeRepo,
}
eventManager.Register(res)
@@ -74,11 +77,11 @@ func (p *PluginExtractEntity) OnEvent(ctx context.Context,
kbIDSet[id] = struct{}{}
}
- // If KnowledgeIDs is specified, retrieve them and collect their knowledge base IDs
+ // If KnowledgeIDs is specified, retrieve them and collect their knowledge base IDs (include shared KB docs)
// Also build a mapping from KnowledgeID to KnowledgeBaseID
knowledgeToKBMap := make(map[string]string)
if len(chatManage.KnowledgeIDs) > 0 {
- knowledges, err := p.knowledgeRepo.GetKnowledgeBatch(ctx, chatManage.TenantID, chatManage.KnowledgeIDs)
+ knowledges, err := p.knowledgeService.GetKnowledgeBatchWithSharedAccess(ctx, chatManage.TenantID, chatManage.KnowledgeIDs)
if err != nil {
logger.Errorf(ctx, "failed to get knowledges: %v", err)
return next()
diff --git a/internal/application/service/chat_pipline/search.go b/internal/application/service/chat_pipline/search.go
index cebcf84b2..f3a43b105 100644
--- a/internal/application/service/chat_pipline/search.go
+++ b/internal/application/service/chat_pipline/search.go
@@ -448,7 +448,7 @@ func (p *PluginSearch) tryDirectChunkLoading(ctx context.Context, tenantID uint6
knowledgeMap := make(map[string]*types.Knowledge)
if len(uniqueKIDs) > 0 {
- knowledges, err := p.knowledgeService.GetKnowledgeBatch(ctx, tenantID, uniqueKIDs)
+ knowledges, err := p.knowledgeService.GetKnowledgeBatchWithSharedAccess(ctx, tenantID, uniqueKIDs)
if err != nil {
logger.Warnf(ctx, "DirectLoad: Failed to fetch knowledge batch: %v", err)
// Continue without metadata
diff --git a/internal/application/service/kbshare.go b/internal/application/service/kbshare.go
index 55713e0df..589333b14 100644
--- a/internal/application/service/kbshare.go
+++ b/internal/application/service/kbshare.go
@@ -119,7 +119,8 @@ func (s *kbShareService) ShareKnowledgeBase(ctx context.Context, kbID string, or
return share, nil
}
-// UpdateSharePermission updates the permission of a share
+// UpdateSharePermission updates the permission of a share.
+// Allowed if: (1) current user is the sharer, or (2) current user is admin of the target organization.
func (s *kbShareService) UpdateSharePermission(ctx context.Context, shareID string, permission types.OrgMemberRole, userID string) error {
share, err := s.shareRepo.GetByID(ctx, shareID)
if err != nil {
@@ -129,9 +130,12 @@ func (s *kbShareService) UpdateSharePermission(ctx context.Context, shareID stri
return err
}
- // Only the user who shared can update
+ // Sharer can always update; org admin can also update (e.g. when sharer left)
if share.SharedByUserID != userID {
- return ErrSharePermissionDenied
+ member, err := s.orgRepo.GetMember(ctx, share.OrganizationID, userID)
+ if err != nil || member.Role != types.OrgRoleAdmin {
+ return ErrSharePermissionDenied
+ }
}
if !permission.IsValid() {