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
+3 -3
View File
@@ -14,7 +14,7 @@
<div class="header-title" style="--wails-draggable: drag">
<div class="title-row" style="--wails-draggable: drag">
<h2 style="--wails-draggable: drag">{{ $t('agent.title') }}</h2>
<t-tooltip :content="$t('agent.createAgent')" placement="bottom">
<t-tooltip v-if="authStore.hasRole('contributor')" :content="$t('agent.createAgent')" placement="bottom">
<t-button
variant="text"
theme="default"
@@ -412,7 +412,7 @@
<img class="empty-img" src="@/assets/img/upload.svg" alt="">
<span class="empty-txt">{{ $t('agent.empty.title') }}</span>
<span class="empty-desc">{{ $t('agent.empty.description') }}</span>
<t-button class="agent-create-btn empty-state-btn" @click="handleCreateAgent">
<t-button v-if="authStore.hasRole('contributor')" class="agent-create-btn empty-state-btn" @click="handleCreateAgent">
<template #icon>
<span class="btn-icon-wrapper">
<svg class="sparkles-icon" width="18" height="18" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
@@ -430,7 +430,7 @@
<img class="empty-img" src="@/assets/img/upload.svg" alt="">
<span class="empty-txt">{{ $t('agent.empty.title') }}</span>
<span class="empty-desc">{{ $t('agent.empty.description') }}</span>
<t-button class="agent-create-btn empty-state-btn" @click="handleCreateAgent">
<t-button v-if="authStore.hasRole('contributor')" class="agent-create-btn empty-state-btn" @click="handleCreateAgent">
<template #icon>
<span class="btn-icon-wrapper">
<svg class="sparkles-icon" width="18" height="18" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
@@ -13,7 +13,7 @@
<div class="header-title" style="--wails-draggable: drag">
<div class="title-row" style="--wails-draggable: drag">
<h2 style="--wails-draggable: drag">{{ $t('knowledgeBase.title') }}</h2>
<t-tooltip :content="$t('knowledgeList.create')" placement="bottom">
<t-tooltip v-if="authStore.hasRole('contributor')" :content="$t('knowledgeList.create')" placement="bottom">
<t-button
variant="text"
theme="default"
@@ -517,7 +517,7 @@
<img class="empty-img" src="@/assets/img/upload.svg" alt="">
<span class="empty-txt">{{ $t('knowledgeList.empty.title') }}</span>
<span class="empty-desc">{{ $t('knowledgeList.empty.description') }}</span>
<t-button class="kb-create-btn empty-state-btn" @click="handleCreateKnowledgeBase">
<t-button v-if="authStore.hasRole('contributor')" class="kb-create-btn empty-state-btn" @click="handleCreateKnowledgeBase">
<template #icon><t-icon name="folder-add" /></template>
{{ $t('knowledgeList.create') }}
</t-button>
@@ -528,7 +528,7 @@
<img class="empty-img" src="@/assets/img/upload.svg" alt="">
<span class="empty-txt">{{ $t('knowledgeList.empty.title') }}</span>
<span class="empty-desc">{{ $t('knowledgeList.empty.description') }}</span>
<t-button class="kb-create-btn empty-state-btn" @click="handleCreateKnowledgeBase">
<t-button v-if="authStore.hasRole('contributor')" class="kb-create-btn empty-state-btn" @click="handleCreateKnowledgeBase">
<template #icon><t-icon name="folder-add" /></template>
{{ $t('knowledgeList.create') }}
</t-button>
+3
View File
@@ -53,6 +53,7 @@
<t-icon name="file-copy" />
</t-button>
<t-button
v-if="authStore.hasRole('owner')"
size="small"
variant="text"
theme="danger"
@@ -230,8 +231,10 @@ import { resetTenantApiKey } from '@/api/tenant'
import { getApiBaseUrl } from '@/utils/api-base'
import { DialogPlugin, MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import { useAuthStore } from '@/stores/auth'
const { t, locale } = useI18n()
const authStore = useAuthStore()
// Reactive state
const tenantInfo = ref<TenantInfo | null>(null)
+16 -5
View File
@@ -17,7 +17,7 @@
<h3>{{ $t('mcpSettings.configuredServices') }}</h3>
<p>{{ $t('mcpSettings.manageAndTest') }}</p>
</div>
<t-button size="small" theme="primary" variant="outline" @click="handleAdd">
<t-button v-if="authStore.hasRole('admin')" size="small" theme="primary" variant="outline" @click="handleAdd">
<template #icon><t-icon name="add" /></template>
{{ $t('mcpSettings.addService') }}
</t-button>
@@ -25,7 +25,7 @@
<div v-if="services.length === 0" class="empty-state">
<t-empty :description="$t('mcpSettings.empty')">
<t-button theme="primary" variant="outline" size="small" @click="handleAdd">
<t-button v-if="authStore.hasRole('admin')" theme="primary" variant="outline" size="small" @click="handleAdd">
<template #icon><t-icon name="add" /></template>
{{ $t('mcpSettings.addFirst') }}
</t-button>
@@ -70,7 +70,7 @@
<t-switch
v-model="service.enabled"
size="medium"
:disabled="service.is_builtin"
:disabled="service.is_builtin || !authStore.hasRole('admin')"
@change="() => handleToggleEnabled(service)"
/>
</template>
@@ -118,8 +118,10 @@ import McpServiceDialog from './components/McpServiceDialog.vue'
import McpTestResult from './components/McpTestResult.vue'
import SettingCard from '@/components/settings/SettingCard.vue'
import { useConfirmDelete } from '@/components/settings/useConfirmDelete'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
const confirmDelete = useConfirmDelete()
const services = ref<MCPService[]>([])
@@ -246,8 +248,13 @@ const handleDelete = (service: MCPService) => {
})
}
// Get service options for dropdown menu
// Get service options for dropdown menu. MCP service mutations and the
// /test endpoint (which probes external infra with stored creds) are all
// Admin+ in the backend matrix, so non-Admins see an empty action menu.
const getServiceOptions = () => {
if (!authStore.hasRole('admin')) {
return []
}
return [
{ content: t('mcpSettings.actions.test'), value: 'test' },
{ content: t('common.edit'), value: 'edit' },
@@ -255,8 +262,12 @@ const getServiceOptions = () => {
]
}
// Builtin: 仅测试
// Builtin: 仅测试 (Admin+ only as well — testing a builtin still hits
// the same /test endpoint that requires Admin+ role).
const getBuiltinServiceOptions = () => {
if (!authStore.hasRole('admin')) {
return []
}
return [
{ content: t('mcpSettings.actions.test'), value: 'test' }
]
+22 -3
View File
@@ -6,8 +6,12 @@
<h2>{{ $t('modelSettings.title') }}</h2>
<p class="section-description">{{ $t('modelSettings.description') }}</p>
</div>
<t-dropdown :options="addModelOptions" placement="bottom-right"
@click="(data: any) => openAddDialog(data.value)">
<t-dropdown
v-if="authStore.hasRole('admin')"
:options="addModelOptions"
placement="bottom-right"
@click="(data: any) => openAddDialog(data.value)"
>
<t-button theme="primary" variant="outline" size="small">
<template #icon><add-icon /></template>
{{ $t('modelSettings.actions.addModel') }}
@@ -68,7 +72,12 @@
</div>
<div v-else class="empty-state">
<t-empty :description="emptyHint">
<t-dropdown :options="addModelOptions" placement="bottom" @click="(data: any) => openAddDialog(data.value)">
<t-dropdown
v-if="authStore.hasRole('admin')"
:options="addModelOptions"
placement="bottom"
@click="(data: any) => openAddDialog(data.value)"
>
<t-button theme="primary" variant="outline" size="small">
<template #icon><add-icon /></template>
{{ $t('modelSettings.actions.addModel') }}
@@ -93,8 +102,10 @@ import ModelEditorDialog from '@/components/ModelEditorDialog.vue'
import SettingCard from '@/components/settings/SettingCard.vue'
import { useConfirmDelete } from '@/components/settings/useConfirmDelete'
import { listModels, createModel, updateModel as updateModelAPI, deleteModel as deleteModelAPI, type ModelConfig } from '@/api/model'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
const confirmDelete = useConfirmDelete()
type ModelType = 'chat' | 'embedding' | 'rerank' | 'vllm' | 'asr'
@@ -344,6 +355,14 @@ const getModelOptions = (type: ModelType, model: any) => {
return options
}
// Models are tenant-wide infrastructure (LLM credentials); the
// backend gates every mutation behind Admin+ (see RegisterModelRoutes).
// Non-Admins get an empty action menu — viewing is fine, but editing,
// copying (also goes through createModel), and deleting are not.
if (!authStore.hasRole('admin')) {
return options
}
options.push({
content: t('common.edit'),
value: `edit-${type}-${model.id}`
@@ -219,7 +219,7 @@
<template #footer>
<div class="drawer-footer-actions">
<t-button theme="default" variant="outline" @click="drawerVisible = false">{{ $t('common.cancel') }}</t-button>
<t-button theme="primary" :loading="saving" @click="onSave">{{ $t('common.save') }}</t-button>
<t-button v-if="authStore.hasRole('admin')" theme="primary" :loading="saving" @click="onSave">{{ $t('common.save') }}</t-button>
</div>
</template>
</t-drawer>
@@ -230,6 +230,7 @@
import { ref, computed, onMounted, nextTick } from 'vue'
import { useI18n } from 'vue-i18n'
import { useUIStore } from '@/stores/ui'
import { useAuthStore } from '@/stores/auth'
import {
getParserEngines,
getParserEngineConfig,
@@ -242,6 +243,7 @@ import { getWeKnoraCloudStatus } from '@/api/model'
const { t } = useI18n()
const uiStore = useUIStore()
const authStore = useAuthStore()
const CONFIGURABLE_ENGINES = new Set(['mineru', 'mineru_cloud'])
@@ -19,6 +19,7 @@
<ModelSelector
model-type="Rerank"
:selected-model-id="localConfig.rerank_model_id"
:disabled="!canEdit"
@update:selected-model-id="handleModelChange"
/>
</div>
@@ -35,6 +36,7 @@
:min="1"
:max="100"
:step="1"
:disabled="!canEdit"
@change="handleParamChange"
/>
</div>
@@ -50,6 +52,7 @@
:min="0"
:max="1"
:step="0.05"
:disabled="!canEdit"
@change="handleParamChange"
/>
</div>
@@ -65,6 +68,7 @@
:min="0"
:max="1"
:step="0.05"
:disabled="!canEdit"
@change="handleParamChange"
/>
</div>
@@ -80,6 +84,7 @@
:min="1"
:max="100"
:step="1"
:disabled="!canEdit"
@change="handleParamChange"
/>
</div>
@@ -95,6 +100,7 @@
:min="-10"
:max="10"
:step="0.1"
:disabled="!canEdit"
@change="handleParamChange"
/>
</div>
@@ -103,7 +109,7 @@
</template>
<script setup lang="ts">
import { reactive, onMounted, nextTick } from 'vue'
import { reactive, computed, onMounted, nextTick } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import ModelSelector from '@/components/ModelSelector.vue'
@@ -112,8 +118,14 @@ import {
updateTenantRetrievalConfig,
type RetrievalConfig,
} from '@/api/retrieval'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
// PUT /tenants/kv/retrieval-config requires Admin+ on the server. Hide the
// banner + lock all controls for non-Admins so they can read the
// configuration without tripping a 403 mid-edit.
const canEdit = computed(() => authStore.hasRole('admin'))
const defaultConfig: RetrievalConfig = {
embedding_top_k: 50,
@@ -465,7 +465,7 @@
<template #footer>
<div class="drawer-footer-actions">
<t-button theme="default" variant="outline" @click="drawerVisible = false">{{ $t('common.cancel') }}</t-button>
<t-button theme="primary" :loading="saving" @click="onSave">{{ $t('common.save') }}</t-button>
<t-button v-if="authStore.hasRole('admin')" theme="primary" :loading="saving" @click="onSave">{{ $t('common.save') }}</t-button>
</div>
</template>
</t-drawer>
@@ -482,8 +482,10 @@ import {
updateStorageEngineConfig,
type StorageEngineConfig,
} from '@/api/system'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
const defaultConfig = (): StorageEngineConfig => ({
default_provider: 'local',
@@ -14,7 +14,7 @@
<div class="settings-group">
<div class="section-subheader">
<h3>{{ t('vectorStoreSettings.storesTitle') }}</h3>
<t-button theme="primary" variant="outline" size="small" @click="openAddDialog">
<t-button v-if="authStore.hasRole('admin')" theme="primary" variant="outline" size="small" @click="openAddDialog">
<template #icon><add-icon /></template>
{{ t('vectorStoreSettings.addStore') }}
</t-button>
@@ -35,6 +35,7 @@
</div>
<div class="item-actions">
<t-button
v-if="authStore.hasRole('admin')"
theme="default" variant="outline" size="small"
:loading="testingId === store.id"
@click="testExisting(store)"
@@ -64,13 +65,14 @@
</div>
<div class="item-actions">
<t-button
v-if="authStore.hasRole('admin')"
theme="default" variant="outline" size="small"
:loading="testingId === store.id"
@click="testExisting(store)"
>
{{ t('vectorStoreSettings.testConnection') }}
</t-button>
<t-dropdown :options="storeActions" trigger="click" @click="(action: any) => handleAction(action, store)">
<t-dropdown v-if="authStore.hasRole('admin')" :options="storeActions" trigger="click" @click="(action: any) => handleAction(action, store)">
<t-button theme="default" variant="text" size="small" shape="square">
<template #icon><t-icon name="more" /></template>
</t-button>
@@ -262,8 +264,10 @@ import {
type VectorStoreEntity,
type VectorStoreTypeInfo,
} from '@/api/vector-store'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
// ===== State =====
const stores = ref<VectorStoreEntity[]>([])
@@ -7,7 +7,7 @@
<div class="settings-toolbar">
<h3>{{ t('webSearchSettings.providersTitle') }}</h3>
<t-button theme="primary" variant="outline" size="small" @click="openAddDialog">
<t-button v-if="authStore.hasRole('admin')" theme="primary" variant="outline" size="small" @click="openAddDialog">
<template #icon><add-icon /></template>
{{ t('webSearchSettings.addProvider') }}
</t-button>
@@ -41,7 +41,7 @@
<!-- Empty State -->
<div v-else class="empty-state">
<t-empty :description="t('webSearchSettings.noProvidersDesc')">
<t-button theme="primary" variant="outline" size="small" @click="openAddDialog">
<t-button v-if="authStore.hasRole('admin')" theme="primary" variant="outline" size="small" @click="openAddDialog">
<template #icon><add-icon /></template>
{{ t('webSearchSettings.addProvider') }}
</t-button>
@@ -171,8 +171,10 @@ import CredentialResource, {
type CredentialResourceApi,
} from '@/components/credentials/CredentialResource.vue'
import { useConfirmDelete } from '@/components/settings/useConfirmDelete'
import { useAuthStore } from '@/stores/auth'
const { t } = useI18n()
const authStore = useAuthStore()
const confirmDelete = useConfirmDelete()
// ===== State =====
@@ -406,6 +408,12 @@ const testExistingConnection = async (entity: WebSearchProviderEntity) => {
}
const getProviderOptions = (_entity: WebSearchProviderEntity) => {
// Web search providers carry external API credentials; the backend
// gates every mutation/test behind Admin+ (RegisterWebSearchProviderRoutes).
// Hide the action menu entirely for non-Admins so they don't trip 403s.
if (!authStore.hasRole('admin')) {
return []
}
return [
{ content: t('webSearchSettings.testConnection'), value: 'test' },
{ content: t('common.edit'), value: 'edit' },