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
+6 -6
View File
@@ -470,11 +470,11 @@ export async function listMembers(orgId: string): Promise<ApiResponse<ListMember
}
/**
* Update member role
* Update member role (member is identified by tenant_id)
*/
export async function updateMemberRole(orgId: string, userId: string, req: UpdateMemberRoleRequest): Promise<ApiResponse<void>> {
export async function updateMemberRole(orgId: string, tenantId: number, req: UpdateMemberRoleRequest): Promise<ApiResponse<void>> {
try {
const response = await put(`/api/v1/organizations/${orgId}/members/${userId}`, req)
const response = await put(`/api/v1/organizations/${orgId}/members/${tenantId}`, req)
return response as unknown as ApiResponse<void>
} catch (error: any) {
return { success: false, message: error.message || 'Failed to update member role' }
@@ -482,11 +482,11 @@ export async function updateMemberRole(orgId: string, userId: string, req: Updat
}
/**
* Remove member
* Remove member (member is identified by tenant_id)
*/
export async function removeMember(orgId: string, userId: string): Promise<ApiResponse<void>> {
export async function removeMember(orgId: string, tenantId: number): Promise<ApiResponse<void>> {
try {
const response = await del(`/api/v1/organizations/${orgId}/members/${userId}`)
const response = await del(`/api/v1/organizations/${orgId}/members/${tenantId}`)
return response as unknown as ApiResponse<void>
} catch (error: any) {
return { success: false, message: error.message || 'Failed to remove member' }
+8 -8
View File
@@ -294,15 +294,15 @@ export const useOrganizationStore = defineStore('organization', () => {
}
/**
* Update a member's role
* Update a member's role (member identified by tenant_id)
*/
async function changeMemberRole(orgId: string, userId: string, role: 'admin' | 'editor' | 'viewer') {
async function changeMemberRole(orgId: string, tenantId: number, role: 'admin' | 'editor' | 'viewer') {
loading.value = true
error.value = null
try {
const response = await updateMemberRole(orgId, userId, { role })
const response = await updateMemberRole(orgId, tenantId, { role })
if (response.success) {
const member = currentMembers.value.find(m => m.user_id === userId)
const member = currentMembers.value.find(m => m.tenant_id === tenantId)
if (member) {
member.role = role
}
@@ -320,15 +320,15 @@ export const useOrganizationStore = defineStore('organization', () => {
}
/**
* Remove a member from organization
* Remove a member from organization (member identified by tenant_id)
*/
async function kickMember(orgId: string, userId: string) {
async function kickMember(orgId: string, tenantId: number) {
loading.value = true
error.value = null
try {
const response = await removeMember(orgId, userId)
const response = await removeMember(orgId, tenantId)
if (response.success) {
currentMembers.value = currentMembers.value.filter(m => m.user_id !== userId)
currentMembers.value = currentMembers.value.filter(m => m.tenant_id !== tenantId)
return true
} else {
error.value = response.message || 'Failed to remove member'
@@ -1172,8 +1172,8 @@ const handleSave = async () => {
const handleRoleChange = async (member: OrganizationMember, newRole: string) => {
if (!props.orgId) return
try {
const res = await updateMemberRole(props.orgId, member.user_id, {
role: newRole as 'admin' | 'editor' | 'viewer'
const res = await updateMemberRole(props.orgId, member.tenant_id, {
role: newRole as 'admin' | 'editor' | 'viewer'
})
if (res.success) {
MessagePlugin.success(t('organization.roleUpdated'))
@@ -1196,7 +1196,7 @@ const confirmRemoveMember = async () => {
if (!removingMember.value || !props.orgId) return
try {
const res = await removeMember(props.orgId, removingMember.value.user_id)
const res = await removeMember(props.orgId, removingMember.value.tenant_id)
if (res.success) {
MessagePlugin.success(t('organization.memberRemoved'))
showRemoveDialog.value = false