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:
wizardchen
2026-05-15 21:49:54 +08:00
committed by lyingbug
parent c89563a273
commit 928f5abe3f
29 changed files with 955 additions and 643 deletions
+5 -5
View File
@@ -145,19 +145,19 @@ func (r *kbShareRepository) ListByOrganizations(ctx context.Context, orgIDs []st
return shares, nil
}
// ListSharedKBsForUser lists all knowledge bases shared to organizations that the user belongs to.
// ListSharedKBsForTenant lists all knowledge bases shared to organizations that the tenant participates in.
// Excludes shares for soft-deleted organizations and soft-deleted knowledge bases.
func (r *kbShareRepository) ListSharedKBsForUser(ctx context.Context, userID string) ([]*types.KnowledgeBaseShare, error) {
func (r *kbShareRepository) ListSharedKBsForTenant(ctx context.Context, tenantID uint64) ([]*types.KnowledgeBaseShare, error) {
var shares []*types.KnowledgeBaseShare
// Get shares for organizations that the user is a member of; exclude deleted orgs and deleted KBs
// Get shares for organizations the tenant 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").
Joins("JOIN organization_tenant_members otm ON otm.organization_id = kb_shares.organization_id").
Joins("JOIN organizations ON organizations.id = kb_shares.organization_id AND organizations.deleted_at IS NULL").
Where("organization_members.user_id = ?", userID).
Where("otm.tenant_id = ?", tenantID).
Where("kb_shares.deleted_at IS NULL").
Order("kb_shares.created_at DESC").
Find(&shares).Error