1a46fcf8ca
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.
345 lines
8.1 KiB
TypeScript
345 lines
8.1 KiB
TypeScript
import { post, get, put } from '@/utils/request'
|
|
import i18n from '@/i18n'
|
|
|
|
const t = (key: string) => i18n.global.t(key)
|
|
|
|
// 用户登录接口
|
|
export interface LoginRequest {
|
|
email: string
|
|
password: string
|
|
}
|
|
|
|
export interface LoginResponse {
|
|
success: boolean
|
|
message?: string
|
|
user?: {
|
|
id: string
|
|
username: string
|
|
email: string
|
|
avatar?: string
|
|
tenant_id: number
|
|
can_access_all_tenants?: boolean
|
|
is_active: boolean
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
tenant?: {
|
|
id: number
|
|
name: string
|
|
description: string
|
|
api_key: string
|
|
status: string
|
|
business: string
|
|
storage_quota: number
|
|
storage_used: number
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
token?: string
|
|
refresh_token?: string
|
|
}
|
|
|
|
export interface OIDCAuthURLResponse {
|
|
success: boolean
|
|
authorization_url?: string
|
|
state?: string
|
|
message?: string
|
|
}
|
|
|
|
export interface OIDCConfigResponse {
|
|
success: boolean
|
|
enabled: boolean
|
|
provider_display_name?: string
|
|
message?: string
|
|
}
|
|
|
|
// 用户注册接口
|
|
export interface RegisterRequest {
|
|
username: string
|
|
email: string
|
|
password: string
|
|
}
|
|
|
|
export interface RegisterResponse {
|
|
success: boolean
|
|
message?: string
|
|
data?: {
|
|
user: {
|
|
id: string
|
|
username: string
|
|
email: string
|
|
}
|
|
tenant: {
|
|
id: string
|
|
name: string
|
|
api_key: string
|
|
}
|
|
}
|
|
}
|
|
|
|
// 用户信息接口
|
|
export interface UserInfo {
|
|
id: string
|
|
username: string
|
|
email: string
|
|
avatar?: string
|
|
tenant_id: string
|
|
can_access_all_tenants?: boolean
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
// 租户信息接口
|
|
export interface TenantInfo {
|
|
id: string
|
|
name: string
|
|
description?: string
|
|
api_key: string
|
|
status?: string
|
|
business?: string
|
|
owner_id: string
|
|
storage_quota?: number
|
|
storage_used?: number
|
|
created_at: string
|
|
updated_at: string
|
|
knowledge_bases?: KnowledgeBaseInfo[]
|
|
}
|
|
|
|
// 知识库信息接口
|
|
export interface KnowledgeBaseInfo {
|
|
id: string
|
|
name: string
|
|
description: string
|
|
tenant_id: string
|
|
// creator_id is the user id of whoever originally created the KB.
|
|
// Set by PR 5 of the multi-tenant RBAC series; nullable for legacy
|
|
// KBs created before that migration backfilled the column.
|
|
creator_id?: string
|
|
created_at: string
|
|
updated_at: string
|
|
document_count?: number
|
|
chunk_count?: number
|
|
}
|
|
|
|
// 模型信息接口
|
|
export interface ModelInfo {
|
|
id: string
|
|
name: string
|
|
type: string
|
|
source: string
|
|
description?: string
|
|
is_default?: boolean
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
/**
|
|
* 用户登录
|
|
*/
|
|
export async function login(data: LoginRequest): Promise<LoginResponse> {
|
|
try {
|
|
const response = await post('/api/v1/auth/login', data)
|
|
return response as unknown as LoginResponse
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.loginFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取 OIDC 登录跳转地址
|
|
*/
|
|
export async function getOIDCAuthorizationURL(redirectURI: string): Promise<OIDCAuthURLResponse> {
|
|
try {
|
|
const response = await get(`/api/v1/auth/oidc/url?redirect_uri=${encodeURIComponent(redirectURI)}`)
|
|
return response as unknown as OIDCAuthURLResponse
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.loginFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取 OIDC 登录配置
|
|
*/
|
|
export async function getOIDCConfig(): Promise<OIDCConfigResponse> {
|
|
try {
|
|
const response = await get('/api/v1/auth/oidc/config')
|
|
return response as unknown as OIDCConfigResponse
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
enabled: false,
|
|
message: error.message || t('error.auth.loginFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取认证配置(仅返回前端渲染需要的公开字段,例如注册模式)。
|
|
*
|
|
* 后端通过 `auth.registration_mode` 控制是否允许自助注册:
|
|
* - "self_serve" 保留现有自助注册入口(默认)
|
|
* - "invite_only" 关闭注册,要求管理员邀请
|
|
*
|
|
* 失败时回落到 self_serve,避免接口异常导致注册入口直接消失。
|
|
*/
|
|
export interface AuthConfigResponse {
|
|
success: boolean
|
|
registration_mode: 'self_serve' | 'invite_only' | string
|
|
}
|
|
|
|
export async function getAuthConfig(): Promise<AuthConfigResponse> {
|
|
try {
|
|
const response = await get('/api/v1/auth/config')
|
|
return response as unknown as AuthConfigResponse
|
|
} catch {
|
|
return { success: false, registration_mode: 'self_serve' }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户注册
|
|
*/
|
|
export async function register(data: RegisterRequest): Promise<RegisterResponse> {
|
|
try {
|
|
const response = await post('/api/v1/auth/register', data)
|
|
return response as unknown as RegisterResponse
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.registerFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Lite 版自动初始化(创建默认用户/租户 + 签发令牌)
|
|
*/
|
|
export async function autoSetup(): Promise<LoginResponse> {
|
|
try {
|
|
const response = await post('/api/v1/auth/auto-setup', {})
|
|
return response as unknown as LoginResponse
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || 'Auto-setup unavailable'
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Membership row returned alongside /auth/me. Mirrors the LoginResponse
|
|
* shape so the frontend can refresh `currentTenantRole` on every page
|
|
* load — without it, role changes after login (e.g. an Owner demoting
|
|
* us in a peer tenant) stay invisible until the user logs out and back
|
|
* in.
|
|
*/
|
|
export interface MembershipInfo {
|
|
tenant_id: number
|
|
tenant_name?: string
|
|
role: string
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户信息
|
|
*/
|
|
export async function getCurrentUser(): Promise<{ success: boolean; data?: { user: UserInfo; tenant?: TenantInfo | null; memberships?: MembershipInfo[] }; message?: string }> {
|
|
try {
|
|
const response = await get('/api/v1/auth/me')
|
|
return response as unknown as { success: boolean; data?: { user: UserInfo; tenant?: TenantInfo | null; memberships?: MembershipInfo[] }; message?: string }
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.getUserFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前租户信息
|
|
*/
|
|
export async function getCurrentTenant(): Promise<{ success: boolean; data?: TenantInfo; message?: string }> {
|
|
try {
|
|
const response = await get('/api/v1/auth/tenant')
|
|
return response as unknown as { success: boolean; data?: TenantInfo; message?: string }
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.getTenantFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 刷新Token
|
|
*/
|
|
export async function refreshToken(refreshToken: string): Promise<{ success: boolean; data?: { token: string; refreshToken: string }; message?: string }> {
|
|
try {
|
|
const response: any = await post('/api/v1/auth/refresh', { refreshToken })
|
|
if (response && response.success) {
|
|
if (response.access_token || response.refresh_token) {
|
|
return {
|
|
success: true,
|
|
data: {
|
|
token: response.access_token,
|
|
refreshToken: response.refresh_token,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 其他情况直接返回原始消息
|
|
return {
|
|
success: false,
|
|
message: response?.message || t('error.auth.refreshTokenFailed')
|
|
}
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.refreshTokenFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户登出
|
|
*/
|
|
export async function logout(): Promise<{ success: boolean; message?: string }> {
|
|
try {
|
|
await post('/api/v1/auth/logout', {})
|
|
return {
|
|
success: true
|
|
}
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
message: error.message || t('error.auth.logoutFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 验证Token有效性
|
|
*/
|
|
export async function validateToken(): Promise<{ success: boolean; valid?: boolean; message?: string }> {
|
|
try {
|
|
const response = await get('/api/v1/auth/validate')
|
|
return response as unknown as { success: boolean; valid?: boolean; message?: string }
|
|
} catch (error: any) {
|
|
return {
|
|
success: false,
|
|
valid: false,
|
|
message: error.message || t('error.auth.validateTokenFailed')
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|