feat(rbac): add tenant_members schema and auth wiring (PR 1 of 3, #1303)

Lay the foundation for tenant-level RBAC without enforcing it yet.
Schema, types, middleware lookup, and login-flow shape changes ship now;
RequireRole/route annotations and member-management endpoints follow in
PR 2 and PR 3.

Highlights:
- New tenant_members table (Postgres + SQLite) with (user_id, tenant_id)
  unique key and (owner|admin|contributor|viewer) role values. Migration
  000043 backfills one row per existing user, marking the earliest-created
  active user in each tenant as Owner and the rest as Contributor.
- KnowledgeBase.creator_id and CustomAgent.runnable_by_viewer added so
  PR 2 can wire ownership-based and Viewer-runtime checks.
- Auth middleware loads the active TenantMember row, sets a new
  TenantRoleContextKey, and falls back via three escape hatches:
  cross-tenant superusers (CanAccessAllTenants) get Admin in the target
  tenant; orphan tenants (zero members) auto-promote the first authenticating
  human to Owner; and when tenant.enable_rbac is false (the v1 default)
  unmatched lookups fail open with Admin to preserve current behaviour.
- Register / OIDC provisioning now insert an Owner membership for the
  registrant via TenantMemberService.EnsureOwner.
- LoginResponse renamed tenant -> active_tenant and gained a memberships
  array. POST /auth/switch-tenant issues a fresh token pair scoped to a
  target membership; GET /auth/config exposes auth.registration_mode for
  the frontend register-tab gating.
- New config keys: auth.registration_mode (self_serve | invite_only,
  defaults to self_serve) and tenant.enable_rbac (defaults to false).
  Both have WEKNORA_* env overrides.
- Frontend store persists memberships and exposes currentTenantRole;
  Login.vue calls /auth/config and hides the Register tab in invite_only
  mode. Go SDK types updated to match the new LoginResponse shape.
This commit is contained in:
wizardchen
2026-05-13 19:47:48 +08:00
committed by lyingbug
parent 3214e3d952
commit 7f7a48880f
26 changed files with 1295 additions and 46 deletions
+23
View File
@@ -175,6 +175,29 @@ export async function getOIDCConfig(): Promise<OIDCConfigResponse> {
}
}
/**
* 获取认证配置(仅返回前端渲染需要的公开字段,例如注册模式)。
*
* 后端通过 `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' }
}
}
/**
* 用户注册
*/