feat: add organization management features
- Introduced a new API for organization management, including creation, updating, and deletion of organizations. - Implemented organization sharing functionality for knowledge bases, allowing users to share knowledge bases with specific organizations and set permissions. - Added UI components for organization selection and sharing dialogs. - Enhanced existing components to support organization-related features, including shared knowledge base handling and organization role management. This update enhances collaboration capabilities within the application by allowing users to manage and share knowledge bases across different organizations.
This commit is contained in:
+80
-78
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/application/service"
|
||||
@@ -14,17 +15,48 @@ import (
|
||||
|
||||
// ChunkHandler defines HTTP handlers for chunk operations
|
||||
type ChunkHandler struct {
|
||||
service interfaces.ChunkService
|
||||
service interfaces.ChunkService
|
||||
kgService interfaces.KnowledgeService
|
||||
kbShareService interfaces.KBShareService
|
||||
}
|
||||
|
||||
// NewChunkHandler creates a new chunk handler
|
||||
func NewChunkHandler(service interfaces.ChunkService) *ChunkHandler {
|
||||
return &ChunkHandler{service: service}
|
||||
func NewChunkHandler(service interfaces.ChunkService, kgService interfaces.KnowledgeService, kbShareService interfaces.KBShareService) *ChunkHandler {
|
||||
return &ChunkHandler{service: service, kgService: kgService, kbShareService: kbShareService}
|
||||
}
|
||||
|
||||
// effectiveCtxForKnowledge resolves knowledge by ID, validates KB access (owner or shared with required role), and returns context with effectiveTenantID for downstream service calls.
|
||||
func (h *ChunkHandler) effectiveCtxForKnowledge(c *gin.Context, knowledgeID string, requiredPermission types.OrgMemberRole) (context.Context, error) {
|
||||
ctx := c.Request.Context()
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
return nil, errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
|
||||
knowledge, err := h.kgService.GetKnowledgeByIDOnly(ctx, knowledgeID)
|
||||
if err != nil {
|
||||
return nil, errors.NewNotFoundError("Knowledge not found")
|
||||
}
|
||||
if knowledge.TenantID == tenantID {
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, tenantID), nil
|
||||
}
|
||||
if !userExists || h.kbShareService == nil {
|
||||
return nil, errors.NewForbiddenError("Permission denied to access this knowledge")
|
||||
}
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, knowledge.KnowledgeBaseID, userID.(string))
|
||||
if permErr != nil || !isShared {
|
||||
return nil, errors.NewForbiddenError("Permission denied to access this knowledge")
|
||||
}
|
||||
if !permission.HasPermission(requiredPermission) {
|
||||
return nil, errors.NewForbiddenError("Insufficient permission for this operation")
|
||||
}
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, knowledge.TenantID), nil
|
||||
}
|
||||
|
||||
// GetChunkByIDOnly godoc
|
||||
// @Summary 通过ID获取分块
|
||||
// @Description 仅通过分块ID获取分块详情(不需要knowledge_id)
|
||||
// @Description 仅通过分块ID获取分块详情(不需要knowledge_id);支持共享知识库下的分块访问
|
||||
// @Tags 分块管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -46,18 +78,8 @@ func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID, exists := c.Get(types.TenantIDContextKey.String())
|
||||
if !exists {
|
||||
logger.Error(ctx, "Failed to get tenant ID")
|
||||
c.Error(errors.NewUnauthorizedError("Unauthorized"))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving chunk by ID, chunk ID: %s, tenant ID: %d", chunkID, tenantID)
|
||||
|
||||
// Get chunk by ID
|
||||
chunk, err := h.service.GetChunkByID(ctx, chunkID)
|
||||
// Get chunk by ID without tenant filter (chunk may belong to shared KB)
|
||||
chunk, err := h.service.GetChunkByIDOnly(ctx, chunkID)
|
||||
if err != nil {
|
||||
if err == service.ErrChunkNotFound {
|
||||
logger.Warnf(ctx, "Chunk not found, chunk ID: %s", chunkID)
|
||||
@@ -69,14 +91,9 @@ func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Validate tenant ID
|
||||
if chunk.TenantID != tenantID.(uint64) {
|
||||
logger.Warnf(
|
||||
ctx,
|
||||
"Tenant has no permission to access chunk, chunk ID: %s, req tenant: %d, chunk tenant: %d",
|
||||
chunkID, tenantID.(uint64), chunk.TenantID,
|
||||
)
|
||||
c.Error(errors.NewForbiddenError("No permission to access this chunk"))
|
||||
_, err = h.effectiveCtxForKnowledge(c, chunk.KnowledgeID, types.OrgRoleViewer)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,6 +133,12 @@ func (h *ChunkHandler) ListKnowledgeChunks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
effCtx, err := h.effectiveCtxForKnowledge(c, knowledgeID, types.OrgRoleViewer)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse pagination parameters
|
||||
var pagination types.Pagination
|
||||
if err := c.ShouldBindQuery(&pagination); err != nil {
|
||||
@@ -135,8 +158,8 @@ func (h *ChunkHandler) ListKnowledgeChunks(c *gin.Context) {
|
||||
|
||||
chunkType := []types.ChunkType{types.ChunkTypeText}
|
||||
|
||||
// Use pagination for query
|
||||
result, err := h.service.ListPagedChunksByKnowledgeID(ctx, knowledgeID, &pagination, chunkType)
|
||||
// Use pagination for query (effCtx has effectiveTenantID for shared KB)
|
||||
result, err := h.service.ListPagedChunksByKnowledgeID(effCtx, knowledgeID, &pagination, chunkType)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
@@ -170,59 +193,46 @@ type UpdateChunkRequest struct {
|
||||
ImageInfo string `json:"image_info"`
|
||||
}
|
||||
|
||||
// validateAndGetChunk validates request parameters and retrieves the chunk
|
||||
// Returns chunk information, knowledge ID, and error
|
||||
func (h *ChunkHandler) validateAndGetChunk(c *gin.Context) (*types.Chunk, string, error) {
|
||||
// validateAndGetChunk validates request parameters and retrieves the chunk (supports shared KB via effectiveTenantID).
|
||||
// Returns chunk, knowledge ID, context with effectiveTenantID for downstream calls, and error.
|
||||
func (h *ChunkHandler) validateAndGetChunk(c *gin.Context) (*types.Chunk, string, context.Context, error) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
// Validate knowledge ID
|
||||
knowledgeID := secutils.SanitizeForLog(c.Param("knowledge_id"))
|
||||
if knowledgeID == "" {
|
||||
logger.Error(ctx, "Knowledge ID is empty")
|
||||
return nil, "", errors.NewBadRequestError("Knowledge ID cannot be empty")
|
||||
return nil, "", nil, errors.NewBadRequestError("Knowledge ID cannot be empty")
|
||||
}
|
||||
|
||||
// Validate chunk ID
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
if id == "" {
|
||||
logger.Error(ctx, "Chunk ID is empty")
|
||||
return nil, knowledgeID, errors.NewBadRequestError("Chunk ID cannot be empty")
|
||||
return nil, knowledgeID, nil, errors.NewBadRequestError("Chunk ID cannot be empty")
|
||||
}
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID, exists := c.Get(types.TenantIDContextKey.String())
|
||||
if !exists {
|
||||
logger.Error(ctx, "Failed to get tenant ID")
|
||||
return nil, knowledgeID, errors.NewUnauthorizedError("Unauthorized")
|
||||
effCtx, err := h.effectiveCtxForKnowledge(c, knowledgeID, types.OrgRoleEditor)
|
||||
if err != nil {
|
||||
return nil, knowledgeID, nil, err
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Retrieving knowledge chunk information, knowledge ID: %s, chunk ID: %s", knowledgeID, id)
|
||||
|
||||
// Get existing chunk
|
||||
chunk, err := h.service.GetChunkByID(ctx, id)
|
||||
chunk, err := h.service.GetChunkByID(effCtx, id)
|
||||
if err != nil {
|
||||
if err == service.ErrChunkNotFound {
|
||||
logger.Warnf(ctx, "Chunk not found, knowledge ID: %s, chunk ID: %s", knowledgeID, id)
|
||||
return nil, knowledgeID, errors.NewNotFoundError("Chunk not found")
|
||||
return nil, knowledgeID, nil, errors.NewNotFoundError("Chunk not found")
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
return nil, knowledgeID, errors.NewInternalServerError(err.Error())
|
||||
return nil, knowledgeID, nil, errors.NewInternalServerError(err.Error())
|
||||
}
|
||||
|
||||
// Validate tenant ID
|
||||
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,
|
||||
chunk.TenantID,
|
||||
)
|
||||
return nil, knowledgeID, errors.NewForbiddenError("No permission to access this chunk")
|
||||
if chunk.KnowledgeID != knowledgeID {
|
||||
logger.Warnf(ctx, "Chunk does not belong to knowledge, knowledge ID: %s, chunk ID: %s", knowledgeID, id)
|
||||
return nil, knowledgeID, nil, errors.NewForbiddenError("No permission to access this chunk")
|
||||
}
|
||||
|
||||
return chunk, knowledgeID, nil
|
||||
return chunk, knowledgeID, effCtx, nil
|
||||
}
|
||||
|
||||
// UpdateChunk godoc
|
||||
@@ -244,8 +254,7 @@ func (h *ChunkHandler) UpdateChunk(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start updating knowledge chunk")
|
||||
|
||||
// Validate parameters and get chunk
|
||||
chunk, knowledgeID, err := h.validateAndGetChunk(c)
|
||||
chunk, knowledgeID, effCtx, err := h.validateAndGetChunk(c)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
@@ -257,14 +266,13 @@ func (h *ChunkHandler) UpdateChunk(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Update chunk properties
|
||||
if req.Content != "" {
|
||||
chunk.Content = req.Content
|
||||
}
|
||||
|
||||
chunk.IsEnabled = req.IsEnabled
|
||||
|
||||
if err := h.service.UpdateChunk(ctx, chunk); err != nil {
|
||||
if err := h.service.UpdateChunk(effCtx, chunk); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
@@ -296,14 +304,13 @@ func (h *ChunkHandler) DeleteChunk(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
logger.Info(ctx, "Start deleting knowledge chunk")
|
||||
|
||||
// Validate parameters and get chunk
|
||||
chunk, _, err := h.validateAndGetChunk(c)
|
||||
chunk, _, effCtx, err := h.validateAndGetChunk(c)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.service.DeleteChunk(ctx, chunk.ID); err != nil {
|
||||
if err := h.service.DeleteChunk(effCtx, chunk.ID); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
@@ -338,8 +345,13 @@ func (h *ChunkHandler) DeleteChunksByKnowledgeID(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Delete all chunks under the knowledge
|
||||
err := h.service.DeleteChunksByKnowledgeID(ctx, knowledgeID)
|
||||
effCtx, err := h.effectiveCtxForKnowledge(c, knowledgeID, types.OrgRoleEditor)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.service.DeleteChunksByKnowledgeID(effCtx, knowledgeID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
@@ -386,16 +398,7 @@ func (h *ChunkHandler) DeleteGeneratedQuestion(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantID, exists := c.Get(types.TenantIDContextKey.String())
|
||||
if !exists {
|
||||
logger.Error(ctx, "Failed to get tenant ID")
|
||||
c.Error(errors.NewUnauthorizedError("Unauthorized"))
|
||||
return
|
||||
}
|
||||
|
||||
// Verify chunk exists and belongs to tenant
|
||||
chunk, err := h.service.GetChunkByID(ctx, chunkID)
|
||||
chunk, err := h.service.GetChunkByIDOnly(ctx, chunkID)
|
||||
if err != nil {
|
||||
if err == service.ErrChunkNotFound {
|
||||
logger.Warnf(ctx, "Chunk not found, chunk ID: %s", chunkID)
|
||||
@@ -407,14 +410,13 @@ func (h *ChunkHandler) DeleteGeneratedQuestion(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if chunk.TenantID != tenantID.(uint64) {
|
||||
logger.Warnf(ctx, "Tenant has no permission to access chunk, chunk ID: %s", chunkID)
|
||||
c.Error(errors.NewForbiddenError("No permission to access this chunk"))
|
||||
effCtx, err := h.effectiveCtxForKnowledge(c, chunk.KnowledgeID, types.OrgRoleEditor)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Delete the generated question by ID
|
||||
if err := h.service.DeleteGeneratedQuestion(ctx, chunkID, req.QuestionID); err != nil {
|
||||
if err := h.service.DeleteGeneratedQuestion(effCtx, chunkID, req.QuestionID); err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewBadRequestError(err.Error()))
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user