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:
2026-08-09 02:01:14 +08:00
parent 337eb4f05a
commit 541e24c6f4
42 changed files with 1505 additions and 307 deletions
+25 -42
View File
@@ -1,27 +1,29 @@
import { SDK } from 'casdoor-nodejs-sdk'
import { getCasdoorConfig } from './casdoorConfig'
import { getConfig } from './config'
/**
* Casdoor 统一认证封装(服务端)。
*
* - 登录 URL、code->token 交换、JWT 验签全部走官方 casdoor-nodejs-sdk
* - 客户端 secret / 证书公钥只存在于服务端环境变量,绝不进入浏览器
* - 登录成功后再通过 get-account 补齐邮箱/头像/角色等字段(拿不到也不阻塞登录)
* - 配置(serverUrl/clientId/clientSecret/证书)由后台写入 settings 表,不依赖环境变量
* - 登录成功后再通过 get-account 补齐邮箱/头像等字段(拿不到也不阻塞登录)
* - 前台用户一律视为普通用户;后台权限由独立 admin_users 表管理
*/
let _sdk: SDK | null = null
let _sdkKey = ''
function getSDK(): SDK {
const { casdoor } = getConfig()
const key = [casdoor.serverUrl, casdoor.clientId, casdoor.clientSecret, casdoor.orgName, casdoor.appName, casdoor.certificate].join('|')
const cfg = getCasdoorConfig()
const key = [cfg.serverUrl, cfg.clientId, cfg.clientSecret, cfg.orgName, cfg.appName, cfg.certificate].join('|')
if (!_sdk || _sdkKey !== key) {
_sdk = new SDK({
endpoint: casdoor.serverUrl,
clientId: casdoor.clientId,
clientSecret: casdoor.clientSecret,
certificate: casdoor.certificate,
orgName: casdoor.orgName,
appName: casdoor.appName || undefined,
endpoint: cfg.serverUrl,
clientId: cfg.clientId,
clientSecret: cfg.clientSecret,
certificate: cfg.certificate,
orgName: cfg.orgName,
appName: cfg.appName || undefined,
})
_sdkKey = key
}
@@ -30,26 +32,28 @@ function getSDK(): SDK {
/** 构造 Casdoor 登录授权地址(redirect_uri = 本站回调) */
export function getCasdoorSignInUrl(): string {
const { casdoor } = getConfig()
return getSDK().getSignInUrl(casdoor.redirectUri)
const cfg = getCasdoorConfig()
const redirectUri = getConfig().siteUrl + cfg.callbackPath
return getSDK().getSignInUrl(redirectUri)
}
/** 构造 Casdoor 注册页地址(在 Casdoor 侧完成注册) */
export function getCasdoorSignUpUrl(): string {
const { casdoor } = getConfig()
return `${casdoor.serverUrl}/signup/${casdoor.appName}`
const cfg = getCasdoorConfig()
return `${cfg.serverUrl}/signup/${cfg.appName}`
}
/** 单点登出地址(访问后跳回 redirectUri;未配置则返回空串) */
export function getCasdoorLogoutUrl(): string {
const { casdoor } = getConfig()
if (!casdoor.serverUrl || !casdoor.clientId) return ''
return `${casdoor.serverUrl}/logout?client_id=${casdoor.clientId}&redirect_uri=${encodeURIComponent(casdoor.redirectUri)}`
const cfg = getCasdoorConfig()
if (!cfg.serverUrl || !cfg.clientId) return ''
const redirectUri = getConfig().siteUrl + cfg.callbackPath
return `${cfg.serverUrl}/logout?client_id=${cfg.clientId}&redirect_uri=${encodeURIComponent(redirectUri)}`
}
/** 登录回调校验的期望 state(casdoor SDK 以 appName 作为 state 返回) */
export function getCasdoorState(): string {
return getConfig().casdoor.appName
return getCasdoorConfig().appName
}
export interface CasdoorIdentity {
@@ -59,7 +63,6 @@ export interface CasdoorIdentity {
username: string
email: string
avatar: string
isAdmin: boolean
}
/**
@@ -95,33 +98,13 @@ export async function exchangeCodeForIdentity(code: string): Promise<CasdoorIden
username,
email,
avatar,
isAdmin: isAdminClaim(account) || isAdminClaim(claims),
}
}
/**
* 管理员判定:
* - token 字段 `IsAdmin` / `isAdmin` 为 true
* - roles(对象数组 {name} 或字符串数组)命中 NUXT_CASDOOR_ADMIN_ROLES
*/
export function isAdminClaim(claims: Record<string, unknown> | undefined | null): boolean {
if (!claims) return false
if ((claims as any).isAdmin === true || (claims as any).IsAdmin === true) return true
const { casdoor } = getConfig()
const roles = (claims as any).roles
if (!Array.isArray(roles) || roles.length === 0) return false
const names = roles
.map((r: any) => (typeof r === 'string' ? r : r?.name))
.filter(Boolean)
.map((n: string) => n.toLowerCase())
return names.some((n: string) => casdoor.adminRoles.includes(n))
}
/** 通过 get-account 接口补齐完整账号信息(含邮箱/头像/角色);失败不阻塞登录 */
/** 通过 get-account 接口补齐完整账号信息(含邮箱/头像);失败不阻塞登录 */
async function getAccountProfile(accessToken: string): Promise<Record<string, unknown> | null> {
const { casdoor } = getConfig()
const url = `${casdoor.serverUrl}/api/get-account?access_token=${encodeURIComponent(accessToken)}`
const cfg = getCasdoorConfig()
const url = `${cfg.serverUrl}/api/get-account?access_token=${encodeURIComponent(accessToken)}`
const res = await fetch(url)
if (!res.ok) return null
const body = (await res.json()) as { status?: string; data?: Record<string, unknown> }