feat: 登录改为 Casdoor 统一身份认证(SSO),重建用户体系

- 移除内置密码注册/登录/改密/删号/头像,统一走 Casdoor OIDC 授权码
- 服务端 casdoor-nodejs-sdk:code 换 token + 证书公钥验签 + get-account 补齐资料
- 用户表重建为 provider+provider_id(Casdoor sub 为准),邮箱不再唯一
- 管理员由 Casdoor token 的 isAdmin/IsAdmin 字段或 admin 角色判定,删除角色切换接口
- 前端登录弹窗改为申统认证跳转,注册去 Casdoor;后台用户列表只读角色
- 会话沿用 an_session httpOnly Cookie,requireUser/requireAdmin 守卫保留
- 配置注入 deploy.sh/.env.example;新增 casdoor 单测,删除 password 单测
This commit is contained in:
2026-08-09 00:18:47 +08:00
parent bdafac2857
commit 337eb4f05a
51 changed files with 1395 additions and 1504 deletions
+10 -53
View File
@@ -1,7 +1,8 @@
<script setup lang="ts">
/**
* 后台用户管理:列表 + 搜索/角色筛选 + 角色切换 + 删除确认 + 前端分页。
* - GET /api/admin/users;PATCH /api/admin/users/[id](角色切换,不能操作自己);DELETE /api/admin/users/[id]
* 后台用户管理:列表 + 搜索 + 删除确认 + 前端分页。
* - GET /api/admin/users;DELETE /api/admin/users/[id]
* - 角色由 Casdoor 统一身份认证决定(token 的 IsAdmin claim),本地仅缓存展示,不提供切换。
*/
import { computed, onMounted, ref, watch } from 'vue'
import { useUserStore } from '~/stores/user'
@@ -14,7 +15,6 @@ interface AdminUser {
id: number
username: string
email: string | null
phone: string | null
role: 'user' | 'admin'
createdAt: string
}
@@ -53,11 +53,7 @@ const filteredUsers = computed(() => {
return users.value.filter((u) => {
if (roleFilter.value !== 'all' && u.role !== roleFilter.value) return false
if (!kw) return true
return (
u.username.toLowerCase().includes(kw) ||
(u.email || '').toLowerCase().includes(kw) ||
(u.phone || '').toLowerCase().includes(kw)
)
return u.username.toLowerCase().includes(kw) || (u.email || '').toLowerCase().includes(kw)
})
})
@@ -82,22 +78,6 @@ const changePage = (p: number) => {
page.value = Math.min(Math.max(1, p), totalPages.value)
}
/* ---------- 角色切换 ---------- */
const togglingId = ref<number | null>(null)
const toggleRole = async (u: AdminUser) => {
const next: AdminUser['role'] = u.role === 'admin' ? 'user' : 'admin'
togglingId.value = u.id
try {
await $fetch(`/api/admin/users/${u.id}`, { method: 'PATCH', body: { role: next } })
u.role = next
toast.success(`已将「${u.username}」设为${next === 'admin' ? '管理员' : '普通用户'}`)
} catch (err: any) {
toast.error(err?.data?.message || '操作失败,请重试')
} finally {
togglingId.value = null
}
}
/* ---------- 删除 ---------- */
const confirmDelete = ref<AdminUser | null>(null)
const deleting = ref(false)
@@ -138,7 +118,7 @@ const formatDate = (raw?: string) => {
<template>
<div>
<AdminPageHeader title="用户管理" subtitle="管理站点注册用户,调整角色或删除账号。">
<AdminPageHeader title="用户管理" subtitle="查看站点 SSO 用户,角色由 Casdoor 统一身份判定。">
<template #actions>
<AdminButton variant="secondary" size="sm" @click="loadUsers">
<Icon name="ph:arrows-clockwise" class="w-4 h-4" /> 刷新
@@ -158,7 +138,7 @@ const formatDate = (raw?: string) => {
<input
v-model="keyword"
type="text"
placeholder="搜索用户名 / 邮箱 / 手机号"
placeholder="搜索用户名 / 邮箱"
class="w-full rounded-xl border border-gray-200 bg-white py-2.5 pl-10 pr-9 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10"
/>
<button
@@ -196,7 +176,7 @@ const formatDate = (raw?: string) => {
</AdminCard>
<!-- 空态 -->
<AdminEmptyState v-else-if="!users.length" icon="ph:users-three" title="暂无用户" description="还没有注册用户。" />
<AdminEmptyState v-else-if="!users.length" icon="ph:users-three" title="暂无用户" description="还没有登录用户。" />
<AdminEmptyState
v-else-if="!filteredUsers.length"
@@ -216,9 +196,8 @@ const formatDate = (raw?: string) => {
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap sticky left-0 bg-gray-50 z-10 border-r border-gray-100">ID</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">用户名</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">邮箱</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">手机号</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider">角色</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">注册时间</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider whitespace-nowrap">加入时间</th>
<th class="px-5 py-4 font-semibold text-xs uppercase tracking-wider text-right">操作</th>
</tr>
</thead>
@@ -227,7 +206,6 @@ const formatDate = (raw?: string) => {
<td class="px-5 py-4 text-gray-400 font-mono text-xs whitespace-nowrap sticky left-0 bg-white z-10 border-r border-gray-100">#{{ u.id }}</td>
<td class="px-5 py-4 font-semibold text-black whitespace-nowrap">{{ u.username }}</td>
<td class="px-5 py-4 text-gray-600">{{ u.email || '—' }}</td>
<td class="px-5 py-4 text-gray-600 whitespace-nowrap">{{ u.phone || '—' }}</td>
<td class="px-5 py-4 whitespace-nowrap">
<AdminBadge :tone="u.role === 'admin' ? 'black' : 'gray'">
<Icon v-if="u.role === 'admin'" name="ph:crown-simple" class="w-3 h-3" />
@@ -238,14 +216,6 @@ const formatDate = (raw?: string) => {
<td class="px-5 py-4">
<div class="flex items-center justify-end gap-2">
<template v-if="u.id !== meId">
<AdminButton
size="sm"
:variant="u.role === 'admin' ? 'secondary' : 'primary'"
:loading="togglingId === u.id"
@click="toggleRole(u)"
>
{{ u.role === 'admin' ? '设为用户' : '设为管理员' }}
</AdminButton>
<AdminButton size="sm" variant="danger" @click="askDelete(u)">
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
</AdminButton>
@@ -289,25 +259,12 @@ const formatDate = (raw?: string) => {
<dd class="text-gray-700 text-right break-all">{{ u.email || '—' }}</dd>
</div>
<div class="flex justify-between gap-3">
<dt class="text-gray-400 flex-shrink-0">手机号</dt>
<dd class="text-gray-700 text-right">{{ u.phone || '—' }}</dd>
</div>
<div class="flex justify-between gap-3">
<dt class="text-gray-400 flex-shrink-0">注册时间</dt>
<dt class="text-gray-400 flex-shrink-0">加入时间</dt>
<dd class="text-gray-700 text-right">{{ formatDate(u.createdAt) }}</dd>
</div>
</dl>
<div class="mt-4 flex flex-wrap items-center gap-2">
<template v-if="u.id !== meId">
<AdminButton
size="sm"
class="flex-1 min-h-10"
:variant="u.role === 'admin' ? 'secondary' : 'primary'"
:loading="togglingId === u.id"
@click="toggleRole(u)"
>
{{ u.role === 'admin' ? '设为用户' : '设为管理员' }}
</AdminButton>
<AdminButton size="sm" variant="danger" class="min-h-10" @click="askDelete(u)">
<Icon name="ph:trash" class="w-3.5 h-3.5" /> 删除
</AdminButton>
@@ -332,4 +289,4 @@ const formatDate = (raw?: string) => {
@confirm="doDelete"
/>
</div>
</template>
</template>