fix(rbac): role-aware UI gates leaked write affordances after tenant switch
When an Owner of tenant A invited user B as a Viewer, B could log in,
switch the active tenant from B's home to A via the user menu, and
still see every write button — Create KB, Upload Document, Edit/Delete
agent — because the role-gate plumbing on the client was wired to the
wrong tenant. The server's RBAC enforcement (PR 2 / PR 5) still blocked
the actual mutations with 403, so no data leaked, but the UI presented
authority the user did not have, which is a confusing and embarrassing
defect.
Five distinct root causes, all on the client / server boundary:
1. `currentTenantRole` (frontend/src/stores/auth.ts) read
`tenant.value.id` — the user's *home* tenant — instead of the active
tenant id. After switching to A, the helper still returned B's home
role (Owner), so every `hasRole(...)` gate in the codebase silently
short-circuited. Fix: read `selectedTenantId` first, fall back to
`tenant.id`.
2. `setMemberships` was only called once, on /auth/login. Hard reloads
(which the tenant switcher triggers) re-ran `getCurrentUser()` via
`App.vue` and `router/index.ts`, but those callers discarded the
`memberships` field of the response, so the auth store kept the
login-time snapshot forever. Role changes between sessions stayed
invisible. Fix: extract memberships in both /auth/me consumers and
feed them into `setMemberships`.
3. `KnowledgeBase.vue`'s `isOwner` compared `kb.tenant_id` to the
active tenant id, so any KB inside the current tenant was treated
as "owned by the caller" — bypassing every role gate below. Fix:
compare against `kb.creator_id` (the field PR 5 introduced) and
require an actual creator match. `canEdit` / `canManage` now layer
`hasRole('admin')` on top so a non-creator with sufficient role can
still manage.
4. `KnowledgeBaseList.vue` and `AgentList.vue` rendered the per-card
more-menu (Settings, Delete, Edit) unconditionally. With the
ownership column from PR 5 this is straightforward to gate on the
client: Settings/Delete now require creator-match or `hasRole('admin')`
per the server's `OwnedKBOrAdmin` / `OwnedAgentOrAdmin` matrix.
5. The axios interceptor (frontend/src/utils/request.ts) had a
"selectedTenantId === defaultTenantId → don't attach X-Tenant-ID"
short-circuit. Any code that wrote the active tenant into
`weknora_tenant` (UserMenu's loadUserInfo, OIDC sync, router
hydrate) made the two values equal, after which the header silently
stopped attaching and subsequent navigation requests landed on the
home tenant. Fix: drop the short-circuit; always attach when
selectedTenantId is set.
Server-side, /auth/me read `user.TenantID` (the immutable home tenant
id stamped at signup) when fetching the tenant for the response, so
the `tenant` field always pointed at home even after the auth
middleware had already resolved an active tenant from X-Tenant-ID.
The frontend then re-keyed `authStore.tenant` to the home tenant on
every refresh, undoing the switch. Fix: read the active tenant id from
context (`types.TenantIDFromContext(ctx)`) and only fall back to
user.TenantID when context is unset.
The server-side enforcement was correct throughout — every fix here is
purely about not lying to the user with the rendered UI.
This commit is contained in:
@@ -103,13 +103,13 @@
|
||||
</div>
|
||||
<template #content>
|
||||
<div class="popup-menu">
|
||||
<div class="popup-menu-item" @click="handleEdit(agent)"><t-icon class="menu-icon" name="edit" /><span>{{ $t('common.edit') }}</span></div>
|
||||
<div class="popup-menu-item" @click="handleCopy(agent)"><t-icon class="menu-icon" name="file-copy" /><span>{{ $t('common.copy') }}</span></div>
|
||||
<div v-if="canManageAgent(agent)" class="popup-menu-item" @click="handleEdit(agent)"><t-icon class="menu-icon" name="edit" /><span>{{ $t('common.edit') }}</span></div>
|
||||
<div v-if="authStore.hasRole('contributor')" class="popup-menu-item" @click="handleCopy(agent)"><t-icon class="menu-icon" name="file-copy" /><span>{{ $t('common.copy') }}</span></div>
|
||||
<div v-if="!agent.is_builtin" class="popup-menu-item" @click="handleToggleDisabled(agent)">
|
||||
<t-icon class="menu-icon" name="poweroff" />
|
||||
<span>{{ agent.disabled_by_me ? $t('agent.enable') : $t('agent.disable') }}</span>
|
||||
</div>
|
||||
<div v-if="!agent.is_builtin" class="popup-menu-item delete" @click="handleDelete(agent)"><t-icon class="menu-icon" name="delete" /><span>{{ $t('common.delete') }}</span></div>
|
||||
<div v-if="!agent.is_builtin && canManageAgent(agent)" class="popup-menu-item delete" @click="handleDelete(agent)"><t-icon class="menu-icon" name="delete" /><span>{{ $t('common.delete') }}</span></div>
|
||||
</div>
|
||||
</template>
|
||||
</t-popup>
|
||||
@@ -243,11 +243,11 @@
|
||||
</div>
|
||||
<template #content>
|
||||
<div class="popup-menu">
|
||||
<div class="popup-menu-item" @click="handleEdit(agent)">
|
||||
<div v-if="canManageAgent(agent)" class="popup-menu-item" @click="handleEdit(agent)">
|
||||
<t-icon class="menu-icon" name="edit" />
|
||||
<span>{{ $t('common.edit') }}</span>
|
||||
</div>
|
||||
<div class="popup-menu-item" @click="handleCopy(agent)">
|
||||
<div v-if="authStore.hasRole('contributor')" class="popup-menu-item" @click="handleCopy(agent)">
|
||||
<t-icon class="menu-icon" name="file-copy" />
|
||||
<span>{{ $t('common.copy') }}</span>
|
||||
</div>
|
||||
@@ -255,7 +255,7 @@
|
||||
<t-icon class="menu-icon" name="poweroff" />
|
||||
<span>{{ agent.disabled_by_me ? $t('agent.enable') : $t('agent.disable') }}</span>
|
||||
</div>
|
||||
<div v-if="!agent.is_builtin" class="popup-menu-item delete" @click="handleDelete(agent)">
|
||||
<div v-if="!agent.is_builtin && canManageAgent(agent)" class="popup-menu-item delete" @click="handleDelete(agent)">
|
||||
<t-icon class="menu-icon" name="delete" />
|
||||
<span>{{ $t('common.delete') }}</span>
|
||||
</div>
|
||||
@@ -845,6 +845,20 @@ const handleEdit = (agent: AgentWithUI) => {
|
||||
editorVisible.value = true
|
||||
}
|
||||
|
||||
// canManageAgent mirrors the server-side OwnedAgentOrAdmin guard
|
||||
// (PR 5 #1303): the agent's creator may always edit / delete; otherwise
|
||||
// Admin+ is required. Built-in agents have created_by="" → only Admin+
|
||||
// matches, which lines up with the "Admin can mutate tenant-owned
|
||||
// agents" rule. The server still enforces the same matrix on every
|
||||
// mutation; this gate just hides buttons the user has no authority
|
||||
// to use.
|
||||
function canManageAgent(agent: AgentWithUI): boolean {
|
||||
const userId = authStore.user?.id || ''
|
||||
const creatorId = (agent as any).created_by || ''
|
||||
if (creatorId && userId && creatorId === userId) return true
|
||||
return authStore.hasRole('admin')
|
||||
}
|
||||
|
||||
const handleDelete = (agent: AgentWithUI) => {
|
||||
openMoreAgentId.value = null
|
||||
deletingAgent.value = agent
|
||||
|
||||
@@ -193,21 +193,42 @@ const goToParserSettings = () => {
|
||||
}
|
||||
|
||||
// Permission control: check if current user owns this KB or has edit/manage permission
|
||||
//
|
||||
// "Owner" here is "the original creator of this KB" (PR 5 introduced
|
||||
// CreatorID). The previous version compared kb.tenant_id to the active
|
||||
// tenant id, which only answers "is this KB inside our tenant" — that
|
||||
// is true even for a Viewer in someone else's tenant, so the gate
|
||||
// silently bypassed every role check below. Now we require an explicit
|
||||
// creator match, and the role-aware fallbacks below decide whether a
|
||||
// non-creator may edit / manage.
|
||||
const isOwner = computed(() => {
|
||||
if (!kbInfo.value) return false;
|
||||
// Check if the current user's tenant ID matches the KB's tenant ID
|
||||
const userTenantId = authStore.effectiveTenantId;
|
||||
return kbInfo.value.tenant_id === userTenantId;
|
||||
const creatorId = (kbInfo.value as any).creator_id || '';
|
||||
const userId = authStore.user?.id || '';
|
||||
// creator_id may be empty for legacy KBs created before PR 5; treat
|
||||
// those as tenant-owned so the role gate applies (Admin+ can manage,
|
||||
// Viewer cannot).
|
||||
if (!creatorId) return false;
|
||||
return creatorId === userId;
|
||||
});
|
||||
|
||||
// Can edit: owner, admin, or editor
|
||||
// Can edit: KB creator (any role), or tenant Admin+, or org-share-grant.
|
||||
// The hasRole('contributor') branch is intentionally NOT here — being a
|
||||
// Contributor in a tenant does not by itself grant edit on someone
|
||||
// else's KB. Org shares (orgStore.canEditKB) carry their own grant.
|
||||
const canEdit = computed(() => {
|
||||
return orgStore.canEditKB(kbId.value, isOwner.value);
|
||||
if (isOwner.value) return true;
|
||||
if (authStore.hasRole('admin')) return true;
|
||||
return orgStore.canEditKB(kbId.value, false);
|
||||
});
|
||||
|
||||
// Can manage (delete, settings, etc.): owner or admin
|
||||
// Can manage (delete, settings, etc.): KB creator or tenant Admin+.
|
||||
// Org shares are intentionally excluded here — sharing a KB grants
|
||||
// read/edit, never delete or settings access.
|
||||
const canManage = computed(() => {
|
||||
return orgStore.canManageKB(kbId.value, isOwner.value);
|
||||
if (isOwner.value) return true;
|
||||
if (authStore.hasRole('admin')) return true;
|
||||
return orgStore.canManageKB(kbId.value, false);
|
||||
});
|
||||
|
||||
// Current KB's shared record (when accessed via organization share)
|
||||
|
||||
@@ -328,22 +328,22 @@
|
||||
>
|
||||
<img class="more-icon" src="@/assets/img/more.png" alt="" />
|
||||
</div>
|
||||
<template #content>
|
||||
<div class="popup-menu" @click.stop>
|
||||
<div class="popup-menu-item" @click.stop="handleTogglePin(kb)">
|
||||
<t-icon class="menu-icon" :name="kb.is_pinned ? 'pin-filled' : 'pin'" />
|
||||
<span>{{ kb.is_pinned ? $t('knowledgeList.pin.unpin') : $t('knowledgeList.pin.pin') }}</span>
|
||||
<template #content>
|
||||
<div class="popup-menu" @click.stop>
|
||||
<div class="popup-menu-item" @click.stop="handleTogglePin(kb)">
|
||||
<t-icon class="menu-icon" :name="kb.is_pinned ? 'pin-filled' : 'pin'" />
|
||||
<span>{{ kb.is_pinned ? $t('knowledgeList.pin.unpin') : $t('knowledgeList.pin.pin') }}</span>
|
||||
</div>
|
||||
<div v-if="canManageKBCard(kb)" class="popup-menu-item" @click.stop="handleSettings(kb)">
|
||||
<t-icon class="menu-icon" name="setting" />
|
||||
<span>{{ $t('knowledgeBase.settings') }}</span>
|
||||
</div>
|
||||
<div v-if="canManageKBCard(kb)" class="popup-menu-item delete" @click.stop="handleDelete(kb)">
|
||||
<t-icon class="menu-icon" name="delete" />
|
||||
<span>{{ $t('common.delete') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="popup-menu-item" @click.stop="handleSettings(kb)">
|
||||
<t-icon class="menu-icon" name="setting" />
|
||||
<span>{{ $t('knowledgeBase.settings') }}</span>
|
||||
</div>
|
||||
<div class="popup-menu-item delete" @click.stop="handleDelete(kb)">
|
||||
<t-icon class="menu-icon" name="delete" />
|
||||
<span>{{ $t('common.delete') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
</t-popup>
|
||||
</div>
|
||||
|
||||
@@ -670,10 +670,10 @@ const { t } = useI18n()
|
||||
// 左侧空间选择:我的 / 空间 ID(已去掉「全部」)
|
||||
const spaceSelection = ref<'all' | 'mine' | 'shared' | string>('mine')
|
||||
|
||||
interface KB {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
interface KB {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
updated_at?: string;
|
||||
embedding_model_id?: string;
|
||||
summary_model_id?: string;
|
||||
@@ -690,6 +690,10 @@ interface KB {
|
||||
processing_count?: number;
|
||||
share_count?: number;
|
||||
is_pinned?: boolean;
|
||||
// creator_id is the owner-id matched against authStore.user.id when
|
||||
// gating the per-card more-menu (Settings / Delete). Empty for legacy
|
||||
// KBs created before PR 5; those fall back to the role gate.
|
||||
creator_id?: string;
|
||||
}
|
||||
|
||||
const kbs = ref<KB[]>([])
|
||||
@@ -903,6 +907,21 @@ const handleSettings = (kb: KB) => {
|
||||
goSettings(kb.id)
|
||||
}
|
||||
|
||||
// canManageKBCard mirrors KnowledgeBase.vue's `canManage` for the
|
||||
// per-card more-menu so a Viewer cannot click into Settings or Delete
|
||||
// for a KB they don't own. The server still rejects the call (PR 5
|
||||
// guards every mutation with OwnedKBOrAdmin) but the UI shouldn't
|
||||
// surface buttons the user has no authority to use.
|
||||
//
|
||||
// Legacy KBs created before PR 5 have an empty creator_id; treat
|
||||
// those as tenant-owned (Admin+ may manage) so existing KBs aren't
|
||||
// suddenly unmanageable for everyone.
|
||||
function canManageKBCard(kb: KB): boolean {
|
||||
const userId = authStore.user?.id || ''
|
||||
if (kb.creator_id && userId && kb.creator_id === userId) return true
|
||||
return authStore.hasRole('admin')
|
||||
}
|
||||
|
||||
// 通过 ID 处理设置(用于全部 Tab 下的知识库)
|
||||
const handleSettingsById = (id: string) => {
|
||||
goSettings(id)
|
||||
|
||||
Reference in New Issue
Block a user