From 4f44c12c4196aae2d0bd6f93350a75ed7a93ca8e Mon Sep 17 00:00:00 2001 From: wizardchen Date: Fri, 6 Feb 2026 17:54:40 +0800 Subject: [PATCH] feat(kbshare): enhance knowledge base share listing with tenant ownership --- internal/application/service/kbshare.go | 11 +++++++++-- internal/handler/organization.go | 15 ++++++++++++++- internal/types/interfaces/organization.go | 3 ++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/application/service/kbshare.go b/internal/application/service/kbshare.go index 589333b1..68aa4e0f 100644 --- a/internal/application/service/kbshare.go +++ b/internal/application/service/kbshare.go @@ -174,8 +174,15 @@ func (s *kbShareService) RemoveShare(ctx context.Context, shareID string, userID return ErrSharePermissionDenied } -// ListSharesByKnowledgeBase lists all shares for a knowledge base -func (s *kbShareService) ListSharesByKnowledgeBase(ctx context.Context, kbID string) ([]*types.KnowledgeBaseShare, error) { +// ListSharesByKnowledgeBase lists shares for a knowledge base; caller's tenant must own the KB. +func (s *kbShareService) ListSharesByKnowledgeBase(ctx context.Context, kbID string, tenantID uint64) ([]*types.KnowledgeBaseShare, error) { + kb, err := s.kbRepo.GetKnowledgeBaseByID(ctx, kbID) + if err != nil { + return nil, ErrKBNotFound + } + if kb.TenantID != tenantID { + return nil, ErrNotKBOwner + } return s.shareRepo.ListByKnowledgeBase(ctx, kbID) } diff --git a/internal/handler/organization.go b/internal/handler/organization.go index 7ecbcd76..d71f6968 100644 --- a/internal/handler/organization.go +++ b/internal/handler/organization.go @@ -909,9 +909,22 @@ func (h *OrganizationHandler) ListKBShares(c *gin.Context) { ctx := c.Request.Context() kbID := c.Param("id") + tenantID, _ := c.Get(types.TenantIDContextKey.String()) + if tenantID == nil { + c.Error(apperrors.NewUnauthorizedError("Unauthorized")) + return + } - shares, err := h.shareService.ListSharesByKnowledgeBase(ctx, kbID) + shares, err := h.shareService.ListSharesByKnowledgeBase(ctx, kbID, tenantID.(uint64)) if err != nil { + if errors.Is(err, service.ErrKBNotFound) { + c.Error(apperrors.NewNotFoundError("Knowledge base not found")) + return + } + if errors.Is(err, service.ErrNotKBOwner) { + c.Error(apperrors.NewForbiddenError("Only the knowledge base owner can list its shares")) + return + } logger.Errorf(ctx, "Failed to list shares: %v", err) c.Error(apperrors.NewInternalServerError("Failed to list shares")) return diff --git a/internal/types/interfaces/organization.go b/internal/types/interfaces/organization.go index 0df2731d..822ae4d2 100644 --- a/internal/types/interfaces/organization.go +++ b/internal/types/interfaces/organization.go @@ -86,7 +86,8 @@ type KBShareService interface { RemoveShare(ctx context.Context, shareID string, userID string) error // Query - ListSharesByKnowledgeBase(ctx context.Context, kbID string) ([]*types.KnowledgeBaseShare, error) + // ListSharesByKnowledgeBase lists shares for a KB; tenantID must own the KB (authz check). + ListSharesByKnowledgeBase(ctx context.Context, kbID string, tenantID uint64) ([]*types.KnowledgeBaseShare, error) ListSharesByOrganization(ctx context.Context, orgID string) ([]*types.KnowledgeBaseShare, error) ListSharedKnowledgeBases(ctx context.Context, userID string, currentTenantID uint64) ([]*types.SharedKnowledgeBaseInfo, error) GetShare(ctx context.Context, shareID string) (*types.KnowledgeBaseShare, error)