feat: 后台管理员独立账号体系,Casdoor 配置改为后台管理
- 新增 admin_users/admin_sessions 表与 adminAuth 工具(scrypt 密码 + 独立会话 Cookie) - /admin/init 初始化首个管理员,未初始化时服务端/客户端双守卫引导 - 后台鉴权改为管理员会话,前台 Casdoor 用户一律普通用户、与后台解耦 - Casdoor 配置改存 settings 表并由「统一认证」页管理,移除全部 CASDOOR 环境变量 - 侧边栏增加统一认证/管理员入口与登出;LoginModal 感知 SSO 启用状态 - 补充 adminAuth 测试,重写 casdoor 测试,全部 23 项通过
This commit is contained in:
+34
-29
@@ -1,48 +1,53 @@
|
||||
import { useRuntimeConfig } from '#app'
|
||||
import { ref } from 'vue'
|
||||
|
||||
/**
|
||||
* Casdoor 客户端配置读取(public runtimeConfig 注入,不含任何 secret)。
|
||||
* 登录跳转走后端端点 /api/auth/casdoor-login,这里仅提供注册页与单点登出地址。
|
||||
* Casdoor 客户端公共配置读取(来自后端 /api/auth/casdoor-config,不含任何 secret)。
|
||||
* 配置由后台「统一认证」页管理,故客户端在组件挂载时拉取最新值。
|
||||
* 登录跳转走后端端点 /api/auth/casdoor-login,这里仅提供启用状态、注册页与单点登出地址。
|
||||
*/
|
||||
|
||||
export interface CasdoorPublic {
|
||||
enabled: boolean
|
||||
serverUrl: string
|
||||
clientId: string
|
||||
orgName: string
|
||||
appName: string
|
||||
redirectPath: string
|
||||
callbackPath: string
|
||||
redirectUri: string
|
||||
}
|
||||
|
||||
export function getCasdoorPublic(): CasdoorPublic {
|
||||
const c = useRuntimeConfig()
|
||||
const pub = (c.public ?? {}) as Record<string, string | undefined>
|
||||
return {
|
||||
serverUrl: (pub.casdoorServerUrl as string) || '',
|
||||
clientId: (pub.casdoorClientId as string) || '',
|
||||
orgName: (pub.casdoorOrgName as string) || '',
|
||||
appName: (pub.casdoorAppName as string) || '',
|
||||
redirectPath: (pub.casdoorRedirectPath as string) || '/api/auth/callback',
|
||||
export interface CasdoorClientState {
|
||||
config: CasdoorPublic | null
|
||||
loaded: boolean
|
||||
/** 拉取公开配置(成功或失败都置 loaded=true) */
|
||||
load: () => Promise<void>
|
||||
}
|
||||
|
||||
export function useCasdoorPublic(): CasdoorClientState {
|
||||
const config = ref<CasdoorPublic | null>(null)
|
||||
const loaded = ref(false)
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
config.value = await $fetch<CasdoorPublic>('/api/auth/casdoor-config')
|
||||
} catch {
|
||||
config.value = null
|
||||
} finally {
|
||||
loaded.value = true
|
||||
}
|
||||
}
|
||||
|
||||
return { config, loaded, load }
|
||||
}
|
||||
|
||||
/** 注册账号统一在 Casdoor 完成 */
|
||||
export function getCasdoorSignupUrl(): string {
|
||||
const { serverUrl, appName } = getCasdoorPublic()
|
||||
if (!serverUrl || !appName) return ''
|
||||
return `${serverUrl}/signup/${appName}`
|
||||
}
|
||||
|
||||
/** 本站回调完整地址(用于登出回跳) */
|
||||
export function getCasdoorRedirectUri(): string {
|
||||
const { redirectPath } = getCasdoorPublic()
|
||||
const { public: p } = useRuntimeConfig()
|
||||
const siteUrl = String((p as any).siteUrl || '').replace(/\/$/, '')
|
||||
return `${siteUrl}${redirectPath}`
|
||||
export function getCasdoorSignupUrl(cfg: CasdoorPublic): string {
|
||||
if (!cfg.serverUrl || !cfg.appName) return ''
|
||||
return `${cfg.serverUrl}/signup/${cfg.appName}`
|
||||
}
|
||||
|
||||
/** 单点登出地址(可选使用:登出后跳回本站回调) */
|
||||
export function getCasdoorLogoutUrl(): string {
|
||||
const { serverUrl, clientId } = getCasdoorPublic()
|
||||
if (!serverUrl || !clientId) return ''
|
||||
return `${serverUrl}/logout?client_id=${clientId}&redirect_uri=${encodeURIComponent(getCasdoorRedirectUri())}`
|
||||
export function getCasdoorLogoutUrl(cfg: CasdoorPublic): string {
|
||||
if (!cfg.serverUrl || !cfg.clientId) return ''
|
||||
return `${cfg.serverUrl}/logout?client_id=${cfg.clientId}&redirect_uri=${encodeURIComponent(cfg.redirectUri)}`
|
||||
}
|
||||
Reference in New Issue
Block a user