feat(rbac): lift org membership from per-user to per-tenant (Plan 3, #1303)
Before this change, the `organization_members` table keyed on user_id — joining an org as a Viewer in tenant T meant only that one user got visibility on shared KBs/agents, while the rest of T (including its Owner) saw nothing. The mismatch surfaced as confusing UX bugs: "my colleague shared this with us, why can't I see it?" This change replaces `organization_members` with `organization_tenant_members`. Membership and Org-RBAC role now key on (org_id, tenant_id); `representative_user_id` stays as a display-only label of the user who first brought the tenant in. Permission resolution gains a 3-D cap so tenant-RBAC stays honest under cross-tenant sharing: effective = min(share.Permission, tenant_org_role, tenant_role_cap) where tenant_role_cap pins tenant Viewers to OrgRoleViewer regardless of the org-level grant. A Viewer in their own tenant cannot edit a shared KB even if the org granted Editor — preserves the "Viewer = read-only everywhere" promise. Migration 000045 backfills `organization_tenant_members` from the old table by collapsing each (org, tenant) group to its max role, then renames the old table to `organization_members_pre_plan3` for safe rollback. Sqlite mirror (Lite mode) is fresh-bootstrap: the init schema replaces the old table directly. Routes: member-management URLs change from `:user_id` to `:tenant_id`. Org create / join / leave now require Admin+ in the caller's tenant — joining an Org affects everyone in T, so the decision can't sit with a Viewer. Frontend: OrganizationSettingsModal + store update to address members by tenant_id.
This commit is contained in:
@@ -34,6 +34,7 @@ func (h *ChunkHandler) effectiveCtxForKnowledge(c *gin.Context, knowledgeID stri
|
||||
return nil, errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
|
||||
knowledge, err := h.kgService.GetKnowledgeByIDOnly(ctx, knowledgeID)
|
||||
if err != nil {
|
||||
@@ -46,7 +47,7 @@ func (h *ChunkHandler) effectiveCtxForKnowledge(c *gin.Context, knowledgeID stri
|
||||
return nil, errors.NewForbiddenError("Permission denied to access this knowledge")
|
||||
}
|
||||
if h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, knowledge.KnowledgeBaseID, userID.(string))
|
||||
permission, isShared, permErr := h.kbShareService.CheckTenantKBPermission(ctx, knowledge.KnowledgeBaseID, tenantID, callerTenantRole)
|
||||
if permErr == nil && isShared {
|
||||
if !permission.HasPermission(requiredPermission) {
|
||||
return nil, errors.NewForbiddenError("Insufficient permission for this operation")
|
||||
@@ -56,11 +57,12 @@ func (h *ChunkHandler) effectiveCtxForKnowledge(c *gin.Context, knowledgeID stri
|
||||
}
|
||||
if requiredPermission == types.OrgRoleViewer && h.agentShareService != nil {
|
||||
kbRef := &types.KnowledgeBase{ID: knowledge.KnowledgeBaseID, TenantID: knowledge.TenantID}
|
||||
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), tenantID, kbRef)
|
||||
can, err := h.agentShareService.TenantCanAccessKBViaSomeSharedAgent(ctx, tenantID, callerTenantRole, kbRef)
|
||||
if err == nil && can {
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, knowledge.TenantID), nil
|
||||
}
|
||||
}
|
||||
_ = userID
|
||||
return nil, errors.NewForbiddenError("Permission denied to access this knowledge")
|
||||
}
|
||||
|
||||
|
||||
+10
-7
@@ -47,6 +47,7 @@ func (h *FAQHandler) effectiveCtxForKB(c *gin.Context, kbID string, requiredPerm
|
||||
return nil, errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
kbID = secutils.SanitizeForLog(kbID)
|
||||
if kbID == "" {
|
||||
return nil, errors.NewBadRequestError("Knowledge base ID cannot be empty")
|
||||
@@ -67,24 +68,26 @@ func (h *FAQHandler) effectiveCtxForKB(c *gin.Context, kbID string, requiredPerm
|
||||
if kb.TenantID == tenantID {
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, tenantID), nil
|
||||
}
|
||||
if userExists && h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, kbID, userID.(string))
|
||||
if h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckTenantKBPermission(ctx, kbID, tenantID, callerTenantRole)
|
||||
if permErr == nil && isShared && permission.HasPermission(requiredPermission) {
|
||||
sourceTenantID, srcErr := h.kbShareService.GetKBSourceTenant(ctx, kbID)
|
||||
if srcErr == nil {
|
||||
logger.Infof(ctx, "User %s accessing shared KB %s with permission %s, source tenant: %d",
|
||||
userID.(string), kbID, permission, sourceTenantID)
|
||||
logger.Infof(ctx, "Tenant %d accessing shared KB %s with permission %s, source tenant: %d",
|
||||
tenantID, kbID, permission, sourceTenantID)
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, sourceTenantID), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if requiredPermission == types.OrgRoleViewer && userExists && h.agentShareService != nil {
|
||||
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), tenantID, kb)
|
||||
if requiredPermission == types.OrgRoleViewer && h.agentShareService != nil {
|
||||
can, err := h.agentShareService.TenantCanAccessKBViaSomeSharedAgent(ctx, tenantID, callerTenantRole, kb)
|
||||
if err == nil && can {
|
||||
logger.Infof(ctx, "User %s accessing KB %s via some shared agent", userID.(string), kbID)
|
||||
logger.Infof(ctx, "Tenant %d accessing KB %s via some shared agent", tenantID, kbID)
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, kb.TenantID), nil
|
||||
}
|
||||
}
|
||||
_ = userID
|
||||
_ = userExists
|
||||
logger.Warnf(ctx, "Permission denied to access KB %s", kbID)
|
||||
return nil, errors.NewForbiddenError("Permission denied to access this knowledge base")
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ func (h *KnowledgeHandler) validateKnowledgeBaseAccessWithKBID(c *gin.Context, k
|
||||
return nil, "", 0, "", errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
kbID = secutils.SanitizeForLog(kbID)
|
||||
if kbID == "" {
|
||||
return nil, "", 0, "", errors.NewBadRequestError("Knowledge base ID cannot be empty")
|
||||
@@ -89,24 +90,26 @@ func (h *KnowledgeHandler) validateKnowledgeBaseAccessWithKBID(c *gin.Context, k
|
||||
if kb.TenantID == tenantID {
|
||||
return kb, kbID, tenantID, types.OrgRoleAdmin, nil
|
||||
}
|
||||
if userExists && h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, kbID, userID.(string))
|
||||
if h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckTenantKBPermission(ctx, kbID, tenantID, callerTenantRole)
|
||||
if permErr == nil && isShared {
|
||||
sourceTenantID, srcErr := h.kbShareService.GetKBSourceTenant(ctx, kbID)
|
||||
if srcErr == nil {
|
||||
logger.Infof(ctx, "User %s accessing shared KB %s with permission %s, source tenant: %d",
|
||||
userID.(string), kbID, permission, sourceTenantID)
|
||||
logger.Infof(ctx, "Tenant %d accessing shared KB %s with permission %s, source tenant: %d",
|
||||
tenantID, kbID, permission, sourceTenantID)
|
||||
return kb, kbID, sourceTenantID, permission, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if userExists && h.agentShareService != nil {
|
||||
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), tenantID, kb)
|
||||
if h.agentShareService != nil {
|
||||
can, err := h.agentShareService.TenantCanAccessKBViaSomeSharedAgent(ctx, tenantID, callerTenantRole, kb)
|
||||
if err == nil && can {
|
||||
logger.Infof(ctx, "User %s accessing KB %s via some shared agent", userID.(string), kbID)
|
||||
logger.Infof(ctx, "Tenant %d accessing KB %s via some shared agent", tenantID, kbID)
|
||||
return kb, kbID, kb.TenantID, types.OrgRoleViewer, nil
|
||||
}
|
||||
}
|
||||
_ = userID
|
||||
_ = userExists
|
||||
logger.Warnf(ctx, "Permission denied to access KB %s, tenant ID: %d, KB tenant: %d", kbID, tenantID, kb.TenantID)
|
||||
return nil, kbID, 0, "", errors.NewForbiddenError("Permission denied to access this knowledge base")
|
||||
}
|
||||
@@ -120,6 +123,7 @@ func (h *KnowledgeHandler) resolveKnowledgeAndValidateKBAccess(c *gin.Context, k
|
||||
return nil, ctx, errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
|
||||
knowledge, err := h.kgService.GetKnowledgeByIDOnly(ctx, knowledgeID)
|
||||
if err != nil {
|
||||
@@ -132,18 +136,18 @@ func (h *KnowledgeHandler) resolveKnowledgeAndValidateKBAccess(c *gin.Context, k
|
||||
}
|
||||
|
||||
// Shared KB: check organization permission
|
||||
if userExists && h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, knowledge.KnowledgeBaseID, userID.(string))
|
||||
if h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckTenantKBPermission(ctx, knowledge.KnowledgeBaseID, tenantID, callerTenantRole)
|
||||
if permErr == nil && isShared && permission.HasPermission(requiredPermission) {
|
||||
effectiveTenantID := knowledge.TenantID
|
||||
return knowledge, context.WithValue(ctx, types.TenantIDContextKey, effectiveTenantID), nil
|
||||
}
|
||||
}
|
||||
// Shared agent: request passes agent_id, or user has any shared agent that can access this KB
|
||||
if userExists && h.agentShareService != nil && requiredPermission == types.OrgRoleViewer {
|
||||
if h.agentShareService != nil && requiredPermission == types.OrgRoleViewer {
|
||||
agentID := c.Query("agent_id")
|
||||
if agentID != "" {
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID.(string), tenantID, agentID)
|
||||
agent, err := h.agentShareService.GetSharedAgentForTenant(ctx, tenantID, callerTenantRole, agentID)
|
||||
if err == nil && agent != nil {
|
||||
if knowledge.TenantID != agent.TenantID {
|
||||
return nil, ctx, errors.NewForbiddenError("Permission denied to access this knowledge")
|
||||
@@ -166,12 +170,14 @@ func (h *KnowledgeHandler) resolveKnowledgeAndValidateKBAccess(c *gin.Context, k
|
||||
}
|
||||
} else {
|
||||
kbRef := &types.KnowledgeBase{ID: knowledge.KnowledgeBaseID, TenantID: knowledge.TenantID}
|
||||
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), tenantID, kbRef)
|
||||
can, err := h.agentShareService.TenantCanAccessKBViaSomeSharedAgent(ctx, tenantID, callerTenantRole, kbRef)
|
||||
if err == nil && can {
|
||||
return knowledge, context.WithValue(ctx, types.TenantIDContextKey, knowledge.TenantID), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = userID
|
||||
_ = userExists
|
||||
return nil, ctx, errors.NewForbiddenError("Permission denied to access this knowledge")
|
||||
}
|
||||
|
||||
@@ -1083,12 +1089,14 @@ func (h *KnowledgeHandler) GetKnowledgeBatch(c *gin.Context) {
|
||||
c.Error(errors.NewUnauthorizedError("Unauthorized"))
|
||||
return
|
||||
}
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID, currentTenantID, agentID)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
agent, err := h.agentShareService.GetSharedAgentForTenant(ctx, currentTenantID, callerTenantRole, agentID)
|
||||
if err != nil || agent == nil {
|
||||
logger.Warnf(ctx, "GetKnowledgeBatch: invalid or inaccessible shared agent %s: %v", agentID, err)
|
||||
c.Error(errors.NewForbiddenError("Invalid or inaccessible shared agent").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
_ = userID
|
||||
effectiveTenantID = agent.TenantID
|
||||
agentAllowedKBIDs = resolveAgentAllowedKBIDs(agent)
|
||||
|
||||
@@ -1509,13 +1517,14 @@ func (h *KnowledgeHandler) SearchKnowledge(c *gin.Context) {
|
||||
c.Error(errors.NewUnauthorizedError("user ID not found"))
|
||||
return
|
||||
}
|
||||
userID, _ := userIDVal.(string)
|
||||
_ = userIDVal
|
||||
currentTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if currentTenantID == 0 {
|
||||
c.Error(errors.NewUnauthorizedError("tenant ID not found"))
|
||||
return
|
||||
}
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID, currentTenantID, agentID)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
agent, err := h.agentShareService.GetSharedAgentForTenant(ctx, currentTenantID, callerTenantRole, agentID)
|
||||
if err != nil {
|
||||
if goerrors.Is(err, service.ErrAgentShareNotFound) || goerrors.Is(err, service.ErrAgentSharePermission) || goerrors.Is(err, service.ErrAgentNotFoundForShare) {
|
||||
c.Error(errors.NewForbiddenError("no permission for this shared agent"))
|
||||
|
||||
@@ -270,6 +270,7 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
|
||||
|
||||
// Get user ID from context (needed for shared KB permission check)
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
|
||||
// Get knowledge base ID from URL parameter
|
||||
id := secutils.SanitizeForLog(c.Param("id"))
|
||||
@@ -299,26 +300,26 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
|
||||
}
|
||||
|
||||
// Check 2: If not owner, check organization shared access
|
||||
if userExists && h.kbShareService != nil {
|
||||
// Check if user has shared access through organization
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, id, userID.(string))
|
||||
if h.kbShareService != nil {
|
||||
// Check if caller's tenant has shared access through organization
|
||||
permission, isShared, permErr := h.kbShareService.CheckTenantKBPermission(ctx, id, tenantID.(uint64), callerTenantRole)
|
||||
if permErr == nil && isShared {
|
||||
// User has shared access, get the source tenant ID for embedding queries
|
||||
// Tenant has shared access, get the source tenant ID for embedding queries
|
||||
sourceTenantID, srcErr := h.kbShareService.GetKBSourceTenant(ctx, id)
|
||||
if srcErr == nil {
|
||||
logger.Infof(ctx, "User %s accessing shared KB %s with permission %s, source tenant: %d",
|
||||
userID.(string), id, permission, sourceTenantID)
|
||||
logger.Infof(ctx, "Tenant %d accessing shared KB %s with permission %s, source tenant: %d",
|
||||
tenantID.(uint64), id, permission, sourceTenantID)
|
||||
return kb, id, sourceTenantID, permission, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check 3: Shared agent — allow if request has agent_id (and agent can access this KB) OR user has any shared agent that can access this KB (e.g. opened from "通过智能体可见" list without agent_id)
|
||||
if userExists && h.agentShareService != nil {
|
||||
// Check 3: Shared agent — allow if request has agent_id (and agent can access this KB) OR caller's tenant has any shared agent that can access this KB (e.g. opened from "通过智能体可见" list without agent_id)
|
||||
if h.agentShareService != nil {
|
||||
currentTenantID := tenantID.(uint64)
|
||||
agentID := c.Query("agent_id")
|
||||
if agentID != "" {
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID.(string), currentTenantID, agentID)
|
||||
agent, err := h.agentShareService.GetSharedAgentForTenant(ctx, currentTenantID, callerTenantRole, agentID)
|
||||
if err == nil && agent != nil {
|
||||
if kb.TenantID != agent.TenantID {
|
||||
logger.Warnf(ctx, "Shared agent tenant mismatch, KB %s tenant: %d, agent tenant: %d", id, kb.TenantID, agent.TenantID)
|
||||
@@ -327,12 +328,12 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
|
||||
if mode == "none" {
|
||||
// no-op, fall through
|
||||
} else if mode == "all" {
|
||||
logger.Infof(ctx, "User %s accessing KB %s via shared agent %s (mode=all)", userID.(string), id, agentID)
|
||||
logger.Infof(ctx, "Tenant %d accessing KB %s via shared agent %s (mode=all)", currentTenantID, id, agentID)
|
||||
return kb, id, kb.TenantID, types.OrgRoleViewer, nil
|
||||
} else if mode == "selected" {
|
||||
for _, allowedID := range agent.Config.KnowledgeBases {
|
||||
if allowedID == id {
|
||||
logger.Infof(ctx, "User %s accessing KB %s via shared agent %s (mode=selected)", userID.(string), id, agentID)
|
||||
logger.Infof(ctx, "Tenant %d accessing KB %s via shared agent %s (mode=selected)", currentTenantID, id, agentID)
|
||||
return kb, id, kb.TenantID, types.OrgRoleViewer, nil
|
||||
}
|
||||
}
|
||||
@@ -340,14 +341,16 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No agent_id in query: allow if user has any shared agent that can access this KB (e.g. from space list "通过智能体可见")
|
||||
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), currentTenantID, kb)
|
||||
// No agent_id in query: allow if caller's tenant has any shared agent that can access this KB (e.g. from space list "通过智能体可见")
|
||||
can, err := h.agentShareService.TenantCanAccessKBViaSomeSharedAgent(ctx, currentTenantID, callerTenantRole, kb)
|
||||
if err == nil && can {
|
||||
logger.Infof(ctx, "User %s accessing KB %s via some shared agent (no agent_id in query)", userID.(string), id)
|
||||
logger.Infof(ctx, "Tenant %d accessing KB %s via some shared agent (no agent_id in query)", currentTenantID, id)
|
||||
return kb, id, kb.TenantID, types.OrgRoleViewer, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = userID
|
||||
_ = userExists
|
||||
|
||||
// No permission: not owner and no shared access
|
||||
logger.Warnf(
|
||||
@@ -416,13 +419,14 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
|
||||
c.Error(apperrors.NewUnauthorizedError("user ID not found"))
|
||||
return
|
||||
}
|
||||
userID, _ := userIDVal.(string)
|
||||
_ = userIDVal
|
||||
currentTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if currentTenantID == 0 {
|
||||
c.Error(apperrors.NewUnauthorizedError("tenant ID not found"))
|
||||
return
|
||||
}
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID, currentTenantID, agentID)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
agent, err := h.agentShareService.GetSharedAgentForTenant(ctx, currentTenantID, callerTenantRole, agentID)
|
||||
if err != nil {
|
||||
if stderrors.Is(err, service.ErrAgentShareNotFound) || stderrors.Is(err, service.ErrAgentSharePermission) || stderrors.Is(err, service.ErrAgentNotFoundForShare) {
|
||||
c.Error(apperrors.NewForbiddenError("no permission for this shared agent"))
|
||||
|
||||
@@ -122,10 +122,10 @@ func (h *OrganizationHandler) GetOrganization(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// ListMyOrganizations lists organizations that the current user belongs to.
|
||||
// ListMyOrganizations lists organizations that the current tenant belongs to.
|
||||
// Response includes resource_counts (per-org KB/agent counts) for list sidebar so frontend does not need a separate GET /me/resource-counts.
|
||||
// @Summary 获取我的组织列表
|
||||
// @Description 获取当前用户所属的所有组织,并附带各空间内知识库/智能体数量
|
||||
// @Description 获取当前租户所属的所有组织,并附带各空间内知识库/智能体数量
|
||||
// @Tags 组织管理
|
||||
// @Produce json
|
||||
// @Success 200 {object} types.ListOrganizationsResponse
|
||||
@@ -136,7 +136,7 @@ func (h *OrganizationHandler) ListMyOrganizations(c *gin.Context) {
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
orgs, err := h.orgService.ListUserOrganizations(ctx, userID)
|
||||
orgs, err := h.orgService.ListTenantOrganizations(ctx, tenantID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to list organizations: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to list organizations").WithDetails(err.Error()))
|
||||
@@ -184,16 +184,18 @@ func (h *OrganizationHandler) buildResourceCountsByOrg(ctx context.Context, orgs
|
||||
logger.Warnf(ctx, "buildResourceCountsByOrg CountByOrganizations: %v", err)
|
||||
return nil
|
||||
}
|
||||
directKBIDsByOrg, err := h.shareService.ListSharedKnowledgeBaseIDsByOrganizations(ctx, orgIDs, userID)
|
||||
directKBIDsByOrg, err := h.shareService.ListSharedKnowledgeBaseIDsByOrganizations(ctx, orgIDs, tenantID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "buildResourceCountsByOrg ListSharedKnowledgeBaseIDsByOrganizations: %v", err)
|
||||
return nil
|
||||
}
|
||||
agentListByOrg, err := h.agentShareService.ListSharedAgentsInOrganizations(ctx, orgIDs, userID, tenantID)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
agentListByOrg, err := h.agentShareService.ListSharedAgentsInOrganizations(ctx, orgIDs, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "buildResourceCountsByOrg ListSharedAgentsInOrganizations: %v", err)
|
||||
return nil
|
||||
}
|
||||
_ = userID
|
||||
byOrgKB := make(map[string]int)
|
||||
tenantKBCache := make(map[uint64][]string) // cache ListKnowledgeBasesByTenantID by tenantID
|
||||
for _, o := range orgs {
|
||||
@@ -286,6 +288,7 @@ func (h *OrganizationHandler) UpdateOrganization(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
var req types.UpdateOrganizationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -293,7 +296,7 @@ func (h *OrganizationHandler) UpdateOrganization(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
org, err := h.orgService.UpdateOrganization(ctx, orgID, userID, &req)
|
||||
org, err := h.orgService.UpdateOrganization(ctx, orgID, userID, tenantID, &req)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to update organization: %v", err)
|
||||
if errors.Is(err, service.ErrInvalidValidityDays) {
|
||||
@@ -328,8 +331,9 @@ func (h *OrganizationHandler) DeleteOrganization(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
if err := h.orgService.DeleteOrganization(ctx, orgID, userID); err != nil {
|
||||
if err := h.orgService.DeleteOrganization(ctx, orgID, userID, tenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to delete organization: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied or organization not found"))
|
||||
return
|
||||
@@ -341,9 +345,9 @@ func (h *OrganizationHandler) DeleteOrganization(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// ListMembers lists all members of an organization
|
||||
// ListMembers lists all tenant-members of an organization
|
||||
// @Summary 获取组织成员列表
|
||||
// @Description 获取组织的所有成员
|
||||
// @Description 获取组织的所有成员(按租户)
|
||||
// @Tags 组织管理
|
||||
// @Produce json
|
||||
// @Param id path string true "组织ID"
|
||||
@@ -355,7 +359,7 @@ func (h *OrganizationHandler) ListMembers(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
|
||||
members, err := h.orgService.ListMembers(ctx, orgID)
|
||||
members, err := h.orgService.ListTenantMembers(ctx, orgID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to list members: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to list members").WithDetails(err.Error()))
|
||||
@@ -366,15 +370,15 @@ func (h *OrganizationHandler) ListMembers(c *gin.Context) {
|
||||
for _, m := range members {
|
||||
resp := types.OrganizationMemberResponse{
|
||||
ID: m.ID,
|
||||
UserID: m.UserID,
|
||||
UserID: m.RepresentativeUserID,
|
||||
Role: string(m.Role),
|
||||
TenantID: m.TenantID,
|
||||
JoinedAt: m.CreatedAt,
|
||||
}
|
||||
if m.User != nil {
|
||||
resp.Username = m.User.Username
|
||||
resp.Email = m.User.Email
|
||||
resp.Avatar = m.User.Avatar
|
||||
if m.RepresentativeUser != nil {
|
||||
resp.Username = m.RepresentativeUser.Username
|
||||
resp.Email = m.RepresentativeUser.Email
|
||||
resp.Avatar = m.RepresentativeUser.Avatar
|
||||
}
|
||||
response = append(response, resp)
|
||||
}
|
||||
@@ -388,25 +392,31 @@ func (h *OrganizationHandler) ListMembers(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateMemberRole updates a member's role
|
||||
// UpdateMemberRole updates a tenant-member's role
|
||||
// @Summary 更新成员角色
|
||||
// @Description 更新组织成员的角色(需要管理员权限)
|
||||
// @Description 更新组织成员(租户)的角色(需要管理员权限)
|
||||
// @Tags 组织管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "组织ID"
|
||||
// @Param user_id path string true "用户ID"
|
||||
// @Param request body types.UpdateMemberRoleRequest true "角色信息"
|
||||
// @Param id path string true "组织ID"
|
||||
// @Param tenant_id path string true "成员租户ID"
|
||||
// @Param request body types.UpdateMemberRoleRequest true "角色信息"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 403 {object} apperrors.AppError
|
||||
// @Security Bearer
|
||||
// @Router /organizations/{id}/members/{user_id} [put]
|
||||
// @Router /organizations/{id}/members/{tenant_id} [put]
|
||||
func (h *OrganizationHandler) UpdateMemberRole(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
orgID := c.Param("id")
|
||||
memberUserID := c.Param("user_id")
|
||||
memberTenantIDStr := c.Param("tenant_id")
|
||||
memberTenantID, err := strconv.ParseUint(memberTenantIDStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Error(apperrors.NewValidationError("Invalid tenant ID"))
|
||||
return
|
||||
}
|
||||
operatorUserID := c.GetString(types.UserIDContextKey.String())
|
||||
operatorTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
var req types.UpdateMemberRoleRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -414,7 +424,7 @@ func (h *OrganizationHandler) UpdateMemberRole(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.orgService.UpdateMemberRole(ctx, orgID, memberUserID, req.Role, operatorUserID); err != nil {
|
||||
if err := h.orgService.UpdateTenantMemberRole(ctx, orgID, memberTenantID, req.Role, operatorUserID, operatorTenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to update member role: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied or invalid operation"))
|
||||
return
|
||||
@@ -426,24 +436,30 @@ func (h *OrganizationHandler) UpdateMemberRole(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// RemoveMember removes a member from an organization
|
||||
// RemoveMember removes a tenant-member from an organization
|
||||
// @Summary 移除成员
|
||||
// @Description 从组织中移除成员(需要管理员权限)
|
||||
// @Description 从组织中移除成员租户(需要管理员权限)
|
||||
// @Tags 组织管理
|
||||
// @Param id path string true "组织ID"
|
||||
// @Param user_id path string true "用户ID"
|
||||
// @Param id path string true "组织ID"
|
||||
// @Param tenant_id path string true "成员租户ID"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 403 {object} apperrors.AppError
|
||||
// @Security Bearer
|
||||
// @Router /organizations/{id}/members/{user_id} [delete]
|
||||
// @Router /organizations/{id}/members/{tenant_id} [delete]
|
||||
func (h *OrganizationHandler) RemoveMember(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
orgID := c.Param("id")
|
||||
memberUserID := c.Param("user_id")
|
||||
memberTenantIDStr := c.Param("tenant_id")
|
||||
memberTenantID, err := strconv.ParseUint(memberTenantIDStr, 10, 64)
|
||||
if err != nil {
|
||||
c.Error(apperrors.NewValidationError("Invalid tenant ID"))
|
||||
return
|
||||
}
|
||||
operatorUserID := c.GetString(types.UserIDContextKey.String())
|
||||
operatorTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
if err := h.orgService.RemoveMember(ctx, orgID, memberUserID, operatorUserID); err != nil {
|
||||
if err := h.orgService.RemoveTenantMember(ctx, orgID, memberTenantID, operatorUserID, operatorTenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to remove member: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied or invalid operation"))
|
||||
return
|
||||
@@ -470,8 +486,9 @@ func (h *OrganizationHandler) GenerateInviteCode(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
code, err := h.orgService.GenerateInviteCode(ctx, orgID, userID)
|
||||
code, err := h.orgService.GenerateInviteCode(ctx, orgID, userID, tenantID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to generate invite code: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied"))
|
||||
@@ -498,7 +515,7 @@ func (h *OrganizationHandler) PreviewByInviteCode(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
inviteCode := c.Param("code")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Get organization by invite code
|
||||
org, err := h.orgService.GetOrganizationByInviteCode(ctx, inviteCode)
|
||||
@@ -508,7 +525,7 @@ func (h *OrganizationHandler) PreviewByInviteCode(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Get member count
|
||||
members, _ := h.orgService.ListMembers(ctx, org.ID)
|
||||
members, _ := h.orgService.ListTenantMembers(ctx, org.ID)
|
||||
memberCount := len(members)
|
||||
|
||||
// Get shared knowledge bases count
|
||||
@@ -518,8 +535,8 @@ func (h *OrganizationHandler) PreviewByInviteCode(c *gin.Context) {
|
||||
agentShares, _ := h.agentShareService.ListSharesByOrganization(ctx, org.ID)
|
||||
agentShareCount := len(agentShares)
|
||||
|
||||
// Check if user is already a member
|
||||
_, memberErr := h.orgService.GetMember(ctx, org.ID, userID)
|
||||
// Check if caller's tenant is already a member
|
||||
_, memberErr := h.orgService.GetTenantMember(ctx, org.ID, tenantID)
|
||||
isAlreadyMember := memberErr == nil
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -616,10 +633,10 @@ func (h *OrganizationHandler) SubmitJoinRequest(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if user is already a member
|
||||
_, memberErr := h.orgService.GetMember(ctx, org.ID, userID)
|
||||
// Check if caller's tenant is already a member
|
||||
_, memberErr := h.orgService.GetTenantMember(ctx, org.ID, tenantID)
|
||||
if memberErr == nil {
|
||||
c.Error(apperrors.NewValidationError("You are already a member of this organization"))
|
||||
c.Error(apperrors.NewValidationError("Your tenant is already a member of this organization"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -665,7 +682,7 @@ func (h *OrganizationHandler) SubmitJoinRequest(c *gin.Context) {
|
||||
// @Router /organizations/search [get]
|
||||
func (h *OrganizationHandler) SearchOrganizations(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
query := c.Query("q")
|
||||
limit := 20
|
||||
if l := c.Query("limit"); l != "" {
|
||||
@@ -673,7 +690,7 @@ func (h *OrganizationHandler) SearchOrganizations(c *gin.Context) {
|
||||
limit = n
|
||||
}
|
||||
}
|
||||
resp, err := h.orgService.SearchSearchableOrganizations(ctx, userID, query, limit)
|
||||
resp, err := h.orgService.SearchSearchableOrganizations(ctx, tenantID, query, limit)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to search organizations: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to search organizations"))
|
||||
@@ -816,6 +833,7 @@ func (h *OrganizationHandler) LeaveOrganization(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Check if user is the owner
|
||||
org, err := h.orgService.GetOrganization(ctx, orgID)
|
||||
@@ -829,8 +847,8 @@ func (h *OrganizationHandler) LeaveOrganization(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove the user from the organization
|
||||
if err := h.orgService.RemoveMember(ctx, orgID, userID, userID); err != nil {
|
||||
// Remove the caller's tenant from the organization (self-leave)
|
||||
if err := h.orgService.RemoveTenantMember(ctx, orgID, tenantID, userID, tenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to leave organization: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to leave organization"))
|
||||
return
|
||||
@@ -856,10 +874,10 @@ func (h *OrganizationHandler) ListJoinRequests(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Check admin
|
||||
isAdmin, err := h.orgService.IsOrgAdmin(ctx, orgID, userID)
|
||||
// Check admin: caller's tenant must be admin in the org
|
||||
isAdmin, err := h.orgService.IsTenantOrgAdmin(ctx, orgID, tenantID)
|
||||
if err != nil || !isAdmin {
|
||||
c.Error(apperrors.NewForbiddenError("Only organization admins can view join requests"))
|
||||
return
|
||||
@@ -928,9 +946,10 @@ func (h *OrganizationHandler) ReviewJoinRequest(c *gin.Context) {
|
||||
orgID := c.Param("id")
|
||||
requestID := c.Param("request_id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Check admin
|
||||
isAdmin, err := h.orgService.IsOrgAdmin(ctx, orgID, userID)
|
||||
// Check admin: caller's tenant must be admin in the org
|
||||
isAdmin, err := h.orgService.IsTenantOrgAdmin(ctx, orgID, tenantID)
|
||||
if err != nil || !isAdmin {
|
||||
c.Error(apperrors.NewForbiddenError("Only organization admins can review join requests"))
|
||||
return
|
||||
@@ -950,7 +969,7 @@ func (h *OrganizationHandler) ReviewJoinRequest(c *gin.Context) {
|
||||
assignRole = &req.Role
|
||||
}
|
||||
|
||||
if err := h.orgService.ReviewJoinRequest(ctx, orgID, requestID, req.Approved, userID, req.Message, assignRole); err != nil {
|
||||
if err := h.orgService.ReviewJoinRequest(ctx, orgID, requestID, req.Approved, userID, tenantID, req.Message, assignRole); err != nil {
|
||||
logger.Errorf(ctx, "Failed to review join request: %v", err)
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("空间成员已满,无法通过该加入申请"))
|
||||
@@ -1090,6 +1109,7 @@ func (h *OrganizationHandler) UpdateSharePermission(c *gin.Context) {
|
||||
|
||||
shareID := c.Param("share_id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
var req types.UpdateSharePermissionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -1097,7 +1117,7 @@ func (h *OrganizationHandler) UpdateSharePermission(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.shareService.UpdateSharePermission(ctx, shareID, req.Permission, userID); err != nil {
|
||||
if err := h.shareService.UpdateSharePermission(ctx, shareID, req.Permission, userID, tenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to update share permission: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied"))
|
||||
return
|
||||
@@ -1124,8 +1144,9 @@ func (h *OrganizationHandler) RemoveShare(c *gin.Context) {
|
||||
|
||||
shareID := c.Param("share_id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
if err := h.shareService.RemoveShare(ctx, shareID, userID); err != nil {
|
||||
if err := h.shareService.RemoveShare(ctx, shareID, userID, tenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to remove share: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied"))
|
||||
return
|
||||
@@ -1150,12 +1171,12 @@ func (h *OrganizationHandler) ListOrgShares(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Check if user is a member and get their role for effective-permission calculation
|
||||
member, err := h.orgService.GetMember(ctx, orgID, userID)
|
||||
// Check if caller's tenant is a member and get its role for effective-permission calculation
|
||||
member, err := h.orgService.GetTenantMember(ctx, orgID, tenantID)
|
||||
if err != nil {
|
||||
c.Error(apperrors.NewForbiddenError("You are not a member of this organization"))
|
||||
c.Error(apperrors.NewForbiddenError("Your tenant is not a member of this organization"))
|
||||
return
|
||||
}
|
||||
myRoleInOrg := member.Role
|
||||
@@ -1224,10 +1245,10 @@ func (h *OrganizationHandler) ListOrgShares(c *gin.Context) {
|
||||
func (h *OrganizationHandler) ListSharedKnowledgeBases(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := types.MustTenantIDFromContext(ctx)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
|
||||
sharedKBs, err := h.shareService.ListSharedKnowledgeBases(ctx, userID, tenantID)
|
||||
sharedKBs, err := h.shareService.ListSharedKnowledgeBases(ctx, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to list shared knowledge bases: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to list shared knowledge bases"))
|
||||
@@ -1314,7 +1335,8 @@ func (h *OrganizationHandler) RemoveAgentShare(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
shareID := c.Param("share_id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
if err := h.agentShareService.RemoveShare(ctx, shareID, userID); err != nil {
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if err := h.agentShareService.RemoveShare(ctx, shareID, userID, tenantID); err != nil {
|
||||
logger.Errorf(ctx, "Failed to remove agent share: %v", err)
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied"))
|
||||
return
|
||||
@@ -1338,10 +1360,10 @@ func (h *OrganizationHandler) RemoveAgentShare(c *gin.Context) {
|
||||
func (h *OrganizationHandler) ListOrgAgentShares(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
member, err := h.orgService.GetMember(ctx, orgID, userID)
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
member, err := h.orgService.GetTenantMember(ctx, orgID, tenantID)
|
||||
if err != nil {
|
||||
c.Error(apperrors.NewForbiddenError("You are not a member of this organization"))
|
||||
c.Error(apperrors.NewForbiddenError("Your tenant is not a member of this organization"))
|
||||
return
|
||||
}
|
||||
myRoleInOrg := member.Role
|
||||
@@ -1409,9 +1431,9 @@ func (h *OrganizationHandler) ListOrgAgentShares(c *gin.Context) {
|
||||
// @Router /shared-agents [get]
|
||||
func (h *OrganizationHandler) ListSharedAgents(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
list, err := h.agentShareService.ListSharedAgents(ctx, userID, tenantID)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
list, err := h.agentShareService.ListSharedAgents(ctx, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to list shared agents: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to list shared agents"))
|
||||
@@ -1421,8 +1443,8 @@ func (h *OrganizationHandler) ListSharedAgents(c *gin.Context) {
|
||||
}
|
||||
|
||||
// listSpaceKnowledgeBasesInOrganization returns merged list of direct shared KBs and agent-carried KBs in the org (for list and count).
|
||||
func (h *OrganizationHandler) listSpaceKnowledgeBasesInOrganization(ctx context.Context, orgID string, userID string, tenantID uint64) ([]*types.OrganizationSharedKnowledgeBaseItem, error) {
|
||||
directList, err := h.shareService.ListSharedKnowledgeBasesInOrganization(ctx, orgID, userID, tenantID)
|
||||
func (h *OrganizationHandler) listSpaceKnowledgeBasesInOrganization(ctx context.Context, orgID string, tenantID uint64, callerTenantRole types.TenantRole) ([]*types.OrganizationSharedKnowledgeBaseItem, error) {
|
||||
directList, err := h.shareService.ListSharedKnowledgeBasesInOrganization(ctx, orgID, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1434,7 +1456,7 @@ func (h *OrganizationHandler) listSpaceKnowledgeBasesInOrganization(ctx context.
|
||||
}
|
||||
}
|
||||
|
||||
agentList, err := h.agentShareService.ListSharedAgentsInOrganization(ctx, orgID, userID, tenantID)
|
||||
agentList, err := h.agentShareService.ListSharedAgentsInOrganization(ctx, orgID, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
return directList, nil
|
||||
}
|
||||
@@ -1552,13 +1574,13 @@ func (h *OrganizationHandler) listSpaceKnowledgeBasesInOrganization(ctx context.
|
||||
func (h *OrganizationHandler) ListOrganizationSharedKnowledgeBases(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
|
||||
list, err := h.listSpaceKnowledgeBasesInOrganization(ctx, orgID, userID, tenantID)
|
||||
list, err := h.listSpaceKnowledgeBasesInOrganization(ctx, orgID, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrUserNotInOrg) {
|
||||
c.Error(apperrors.NewForbiddenError("You are not a member of this organization"))
|
||||
if errors.Is(err, service.ErrTenantNotInOrg) {
|
||||
c.Error(apperrors.NewForbiddenError("Your tenant is not a member of this organization"))
|
||||
return
|
||||
}
|
||||
logger.Errorf(ctx, "Failed to list organization shared knowledge bases: %v", err)
|
||||
@@ -1580,13 +1602,13 @@ func (h *OrganizationHandler) ListOrganizationSharedKnowledgeBases(c *gin.Contex
|
||||
func (h *OrganizationHandler) ListOrganizationSharedAgents(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
|
||||
list, err := h.agentShareService.ListSharedAgentsInOrganization(ctx, orgID, userID, tenantID)
|
||||
list, err := h.agentShareService.ListSharedAgentsInOrganization(ctx, orgID, tenantID, callerTenantRole)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrUserNotInOrg) {
|
||||
c.Error(apperrors.NewForbiddenError("You are not a member of this organization"))
|
||||
if errors.Is(err, service.ErrTenantNotInOrg) {
|
||||
c.Error(apperrors.NewForbiddenError("Your tenant is not a member of this organization"))
|
||||
return
|
||||
}
|
||||
logger.Errorf(ctx, "Failed to list organization shared agents: %v", err)
|
||||
@@ -1621,13 +1643,14 @@ func (h *OrganizationHandler) SetSharedAgentDisabledByMe(c *gin.Context) {
|
||||
if err == nil && agent != nil && agent.TenantID == tid {
|
||||
sourceTenantID = tid
|
||||
} else {
|
||||
share, err := h.agentShareService.GetShareByAgentIDForUser(ctx, uid, req.AgentID, tid)
|
||||
share, err := h.agentShareService.GetShareByAgentIDForTenant(ctx, tid, req.AgentID, tid)
|
||||
if err != nil || share == nil {
|
||||
c.Error(apperrors.NewForbiddenError("No access to this agent"))
|
||||
return
|
||||
}
|
||||
sourceTenantID = share.SourceTenantID
|
||||
}
|
||||
_ = uid
|
||||
if err := h.agentShareService.SetSharedAgentDisabledByMe(ctx, tid, req.AgentID, sourceTenantID, req.Disabled); err != nil {
|
||||
logger.Errorf(ctx, "SetSharedAgentDisabledByMe failed: %v", err)
|
||||
c.Error(apperrors.NewInternalServerError("Failed to update preference"))
|
||||
@@ -1638,6 +1661,7 @@ func (h *OrganizationHandler) SetSharedAgentDisabledByMe(c *gin.Context) {
|
||||
|
||||
// toOrgResponse converts an organization to response format
|
||||
func (h *OrganizationHandler) toOrgResponse(ctx context.Context, org *types.Organization, currentUserID string) types.OrganizationResponse {
|
||||
currentTenantID := types.MustTenantIDFromContext(ctx)
|
||||
resp := types.OrganizationResponse{
|
||||
ID: org.ID,
|
||||
Name: org.Name,
|
||||
@@ -1653,8 +1677,8 @@ func (h *OrganizationHandler) toOrgResponse(ctx context.Context, org *types.Orga
|
||||
UpdatedAt: org.UpdatedAt,
|
||||
}
|
||||
|
||||
// Get member count
|
||||
if members, err := h.orgService.ListMembers(ctx, org.ID); err == nil {
|
||||
// Get member count (per-tenant)
|
||||
if members, err := h.orgService.ListTenantMembers(ctx, org.ID); err == nil {
|
||||
resp.MemberCount = len(members)
|
||||
}
|
||||
|
||||
@@ -1667,9 +1691,9 @@ func (h *OrganizationHandler) toOrgResponse(ctx context.Context, org *types.Orga
|
||||
resp.AgentShareCount = len(agentShares)
|
||||
}
|
||||
|
||||
// Get current user's role in this organization
|
||||
// Get current tenant's role in this organization
|
||||
isAdmin := false
|
||||
if role, err := h.orgService.GetUserRoleInOrg(ctx, org.ID, currentUserID); err == nil {
|
||||
if role, err := h.orgService.GetTenantRoleInOrg(ctx, org.ID, currentTenantID); err == nil {
|
||||
resp.MyRole = string(role)
|
||||
isAdmin = (role == types.OrgRoleAdmin)
|
||||
}
|
||||
@@ -1681,8 +1705,8 @@ func (h *OrganizationHandler) toOrgResponse(ctx context.Context, org *types.Orga
|
||||
}
|
||||
}
|
||||
|
||||
// Check if current user has pending upgrade request
|
||||
if _, err := h.orgService.GetPendingUpgradeRequest(ctx, org.ID, currentUserID); err == nil {
|
||||
// Check if current tenant has pending upgrade request
|
||||
if _, err := h.orgService.GetPendingUpgradeRequest(ctx, org.ID, currentTenantID); err == nil {
|
||||
resp.HasPendingUpgrade = true
|
||||
}
|
||||
|
||||
@@ -1706,10 +1730,10 @@ func (h *OrganizationHandler) SearchUsersForInvite(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
query := c.Query("q")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Check admin permission
|
||||
isAdmin, err := h.orgService.IsOrgAdmin(ctx, orgID, userID)
|
||||
// Check admin permission: caller's tenant must be org admin
|
||||
isAdmin, err := h.orgService.IsTenantOrgAdmin(ctx, orgID, tenantID)
|
||||
if err != nil || !isAdmin {
|
||||
c.Error(apperrors.NewForbiddenError("Only organization admins can invite members"))
|
||||
return
|
||||
@@ -1739,17 +1763,17 @@ func (h *OrganizationHandler) SearchUsersForInvite(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get existing members
|
||||
existingMembers, _ := h.orgService.ListMembers(ctx, orgID)
|
||||
existingMemberIDs := make(map[string]bool)
|
||||
// Get existing tenant members; exclude users whose tenant is already a member
|
||||
existingMembers, _ := h.orgService.ListTenantMembers(ctx, orgID)
|
||||
existingTenantIDs := make(map[uint64]bool)
|
||||
for _, m := range existingMembers {
|
||||
existingMemberIDs[m.UserID] = true
|
||||
existingTenantIDs[m.TenantID] = true
|
||||
}
|
||||
|
||||
// Filter out existing members and build response
|
||||
// Filter out users whose tenant is already a member
|
||||
var result []gin.H
|
||||
for _, u := range users {
|
||||
if existingMemberIDs[u.ID] {
|
||||
if existingTenantIDs[u.TenantID] {
|
||||
continue
|
||||
}
|
||||
result = append(result, gin.H{
|
||||
@@ -1787,9 +1811,10 @@ func (h *OrganizationHandler) InviteMember(c *gin.Context) {
|
||||
|
||||
orgID := c.Param("id")
|
||||
userID := c.GetString(types.UserIDContextKey.String())
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
|
||||
// Check admin permission
|
||||
isAdmin, err := h.orgService.IsOrgAdmin(ctx, orgID, userID)
|
||||
// Check admin permission: caller's tenant must be org admin
|
||||
isAdmin, err := h.orgService.IsTenantOrgAdmin(ctx, orgID, tenantID)
|
||||
if err != nil || !isAdmin {
|
||||
c.Error(apperrors.NewForbiddenError("Only organization admins can invite members"))
|
||||
return
|
||||
@@ -1814,15 +1839,15 @@ func (h *OrganizationHandler) InviteMember(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if already a member
|
||||
_, memberErr := h.orgService.GetMember(ctx, orgID, req.UserID)
|
||||
// Check if invitee's tenant is already a member of this org
|
||||
_, memberErr := h.orgService.GetTenantMember(ctx, orgID, invitedUser.TenantID)
|
||||
if memberErr == nil {
|
||||
c.Error(apperrors.NewValidationError("User is already a member of this organization"))
|
||||
c.Error(apperrors.NewValidationError("User's tenant is already a member of this organization"))
|
||||
return
|
||||
}
|
||||
|
||||
// Add member
|
||||
if err := h.orgService.AddMember(ctx, orgID, req.UserID, invitedUser.TenantID, req.Role); err != nil {
|
||||
// Add tenant member with the invited user as representative
|
||||
if err := h.orgService.AddTenantMember(ctx, orgID, invitedUser.TenantID, req.UserID, req.Role); err != nil {
|
||||
logger.Errorf(ctx, "Failed to add member: %v", err)
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("该空间成员已满,无法添加新成员"))
|
||||
|
||||
@@ -250,8 +250,8 @@ func (h *Handler) resolveAgent(ctx context.Context, c *gin.Context, agentID stri
|
||||
userIDVal, _ := c.Get(types.UserIDContextKey.String())
|
||||
currentTenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if h.agentShareService != nil && userIDVal != nil && currentTenantID != 0 {
|
||||
userID, _ := userIDVal.(string)
|
||||
agent, err := h.agentShareService.GetSharedAgentForUser(ctx, userID, currentTenantID, agentID)
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
agent, err := h.agentShareService.GetSharedAgentForTenant(ctx, currentTenantID, callerTenantRole, agentID)
|
||||
if err == nil && agent != nil {
|
||||
effectiveTenantID = agent.TenantID
|
||||
customAgent = agent
|
||||
|
||||
+10
-7
@@ -51,6 +51,7 @@ func (h *TagHandler) effectiveCtxForKB(c *gin.Context, kbID string) (context.Con
|
||||
return nil, errors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
userID, userExists := c.Get(types.UserIDContextKey.String())
|
||||
callerTenantRole := types.TenantRoleFromContext(ctx)
|
||||
kbID = secutils.SanitizeForLog(kbID)
|
||||
if kbID == "" {
|
||||
return nil, errors.NewBadRequestError("Knowledge base ID cannot be empty")
|
||||
@@ -69,24 +70,26 @@ func (h *TagHandler) effectiveCtxForKB(c *gin.Context, kbID string) (context.Con
|
||||
if kb.TenantID == tenantID {
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, tenantID), nil
|
||||
}
|
||||
if userExists && h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, kbID, userID.(string))
|
||||
if h.kbShareService != nil {
|
||||
permission, isShared, permErr := h.kbShareService.CheckTenantKBPermission(ctx, kbID, tenantID, callerTenantRole)
|
||||
if permErr == nil && isShared {
|
||||
sourceTenantID, srcErr := h.kbShareService.GetKBSourceTenant(ctx, kbID)
|
||||
if srcErr == nil {
|
||||
logger.Infof(ctx, "User %s accessing shared KB %s with permission %s, source tenant: %d",
|
||||
userID.(string), kbID, permission, sourceTenantID)
|
||||
logger.Infof(ctx, "Tenant %d accessing shared KB %s with permission %s, source tenant: %d",
|
||||
tenantID, kbID, permission, sourceTenantID)
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, sourceTenantID), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if userExists && h.agentShareService != nil {
|
||||
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), tenantID, kb)
|
||||
if h.agentShareService != nil {
|
||||
can, err := h.agentShareService.TenantCanAccessKBViaSomeSharedAgent(ctx, tenantID, callerTenantRole, kb)
|
||||
if err == nil && can {
|
||||
logger.Infof(ctx, "User %s accessing KB %s via some shared agent", userID.(string), kbID)
|
||||
logger.Infof(ctx, "Tenant %d accessing KB %s via some shared agent", tenantID, kbID)
|
||||
return context.WithValue(ctx, types.TenantIDContextKey, kb.TenantID), nil
|
||||
}
|
||||
}
|
||||
_ = userID
|
||||
_ = userExists
|
||||
logger.Warnf(ctx, "Permission denied to access KB %s", kbID)
|
||||
return nil, errors.NewForbiddenError("Permission denied to access this knowledge base")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user