feat(frontend): tenant switcher in user menu + role-aware UI gates

PR 4 of #1303 wired the backend X-Tenant-ID gate to accept active
tenant_members rows (not just superusers), but the frontend never
exposed a switcher to ordinary multi-tenant members. The only
existing component, the sidebar TenantSelector, is gated to
canAccessAllTenants and queries /tenants/search — both wrong for a
Contributor that an Owner just invited to a second tenant.

This PR closes both gaps:

1. Switcher entry inside the user-avatar dropdown
   - New "Switch Tenant" submenu in UserMenu.vue, hover-triggered, with
     a teleported floating panel (same pattern as the existing IM
     overview submenu so the layout/animation read consistently).
   - Data source is authStore.memberships, populated by /auth/login.
     No new API call: the JWT-issued claim already enumerates every
     tenant the user is an active member of plus their role.
   - Visibility: only shown when memberships.length > 1. Cluster
     superusers keep using the sidebar TenantSelector for "any tenant
     in the system" — the avatar entry is the curated "tenants I'm a
     member of" list.
   - Click writes via authStore.setSelectedTenant(...), then
     window.location.reload() so every cached store / open SSE
     stream / in-flight request is re-keyed under the new tenant.
     Same approach as the existing TenantSelector.

2. Role-aware UI gates on high-friction surfaces
   - authStore exposes a new hasRole(min: 'viewer'|'contributor'|
     'admin'|'owner') helper (numeric ordering matches
     middleware/rbac.go) so v-if gates line up with the server-side
     matrix instead of duplicating ['admin','owner'].includes(...).
   - Gates applied per the PR 4 audit:
     * KB list "Create" button — Contributor+
     * Agent list "Create" button — Contributor+
     * Model / VectorStore / WebSearch / MCP — Add+Edit+Delete+Test
       are Admin+ (and the per-card action menus return [] for
       non-Admins so the dropdowns disappear, not just disable).
     * Parser / Storage engine drawers — Save button Admin+
     * Retrieval settings — slider/selector inputs disabled for
       non-Admins (the form auto-saves on change, no dedicated Save).
     * API key reset — Owner+ (Reset is the highest-impact tenant op).
     * IM channel toggle/edit/delete + Add — Admin+. Same in the
       avatar-menu IM submenu (toggle disabled for non-Admins).
   - KB / agent per-card edit/delete already key off kb.isMine /
     agent.isMine + (for builtins) is_builtin; that path is left as-is.

3. i18n
   - Three new keys under tenant.switcher.* in en-US/zh-CN/ko-KR/ru-RU.
   - Role labels reuse the existing tenantMember.role.* bundle from
     PR 3, with a "show raw role" fallback if a locale lacks the key.

The server is the source of truth for every gated action — these
changes only hide buttons that would 403, never grant new authority.
authStore.hasRole and currentTenantRole both carry a SECURITY note
saying they read from localStorage and are UI-rendering-only.

vue-tsc --build error count unchanged from baseline (90, all
pre-existing issues unrelated to this PR).
This commit is contained in:
wizardchen
2026-05-14 15:37:01 +08:00
committed by lyingbug
parent 9e1c2c2546
commit 466182c8d6
18 changed files with 435 additions and 26 deletions
+20
View File
@@ -73,6 +73,25 @@ export const useAuthStore = defineStore('auth', () => {
return match?.role || ''
})
// hasRole answers "is the current tenant role at least <min>?", used by
// role-aware UI gating across KB / Agent / settings views. The numeric
// ordering (viewer < contributor < admin < owner) mirrors the server-side
// matrix in middleware/rbac.go so a v-if here lines up with the 403 the
// backend would return.
//
// SECURITY: shares the same caveat as currentTenantRole — derived from
// localStorage, MUST be treated as UI-rendering-only. Never rely on
// hasRole() for security decisions; the server is the source of truth.
const ROLE_LEVEL: Record<string, number> = {
viewer: 10,
contributor: 20,
admin: 30,
owner: 40,
}
const hasRole = (min: 'viewer' | 'contributor' | 'admin' | 'owner'): boolean => {
return (ROLE_LEVEL[currentTenantRole.value] ?? 0) >= ROLE_LEVEL[min]
}
const effectiveTenantId = computed(() => {
// 如果选择了其他租户,使用选择的租户ID,否则使用用户默认租户ID
return selectedTenantId.value || (tenant.value?.id ? Number(tenant.value.id) : null)
@@ -294,6 +313,7 @@ export const useAuthStore = defineStore('auth', () => {
currentUserId,
canAccessAllTenants,
currentTenantRole,
hasRole,
effectiveTenantId,
isLiteMode,