feat(admin): 增加账号管理与 Token 签发审计
This commit is contained in:
@@ -5,6 +5,7 @@ import { api, setCsrf } from './api'
|
||||
|
||||
const menus = [
|
||||
{ id: 'tokens', label: '用户与 Token', icon: '👥' },
|
||||
{ id: 'accounts', label: '账号管理', icon: '▣' },
|
||||
{ id: 'clients', label: '服务密钥', icon: '🔐' },
|
||||
{ id: 'tools', label: '工具管理', icon: '🔧' },
|
||||
{ id: 'scopes', label: '作用域管理', icon: '🧩' },
|
||||
@@ -31,8 +32,11 @@ const toolFilter = reactive({ category: '', kind: '', keyword: '' })
|
||||
const toolPage = reactive({ page: 1, pageSize: 20, total: 0 })
|
||||
const auditFilter = reactive({ person: '', tokenPrefix: '', tool: '', result: '', client: '', from: '', to: '' })
|
||||
const tokenDialog = ref(false)
|
||||
const tokenForm = reactive({ id: null, memo: '', scopeIds: [], scopeId: null, expireTime: null })
|
||||
const tokenForm = reactive({ id: null, memo: '', scopeIds: [], expireTime: null })
|
||||
const issuedTokens = ref([])
|
||||
const accounts = ref([])
|
||||
const accountDialog = ref(false)
|
||||
const accountForm = reactive({ id: null, account: '', name: '', password: '', status: 1 })
|
||||
const clientDialog = ref(false)
|
||||
const clientForm = reactive({ id: null, name: '', memo: '', scopeIds: [] })
|
||||
const issuedOpenKey = ref(null)
|
||||
@@ -125,9 +129,11 @@ async function logout() {
|
||||
}
|
||||
|
||||
async function switchMenu(id) {
|
||||
if (id === 'accounts' && session.account !== 'admin') return
|
||||
if (!openTabs.value.includes(id)) openTabs.value = [...openTabs.value, id]
|
||||
current.value = id
|
||||
if (id === 'tokens') await loadUsers()
|
||||
if (id === 'accounts') await loadAccounts()
|
||||
if (id === 'clients') await loadOpenClients()
|
||||
if (id === 'tools') await Promise.all([loadTools(), loadAllTools()])
|
||||
if (id === 'scopes') await loadScopes()
|
||||
@@ -169,14 +175,14 @@ async function selectUser(user) {
|
||||
|
||||
function openToken(token = null) {
|
||||
Object.assign(tokenForm, token ? {
|
||||
id: token.id, memo: token.memo || '', scopeIds: [], scopeId: token.scopeIds[0] ?? null,
|
||||
id: token.id, memo: token.memo || '', scopeIds: [...token.scopeIds],
|
||||
expireTime: token.expireTime || null,
|
||||
} : { id: null, memo: '', scopeIds: [], scopeId: null, expireTime: null })
|
||||
} : { id: null, memo: '', scopeIds: [], expireTime: null })
|
||||
tokenDialog.value = true
|
||||
}
|
||||
|
||||
async function saveToken() {
|
||||
const scopeIds = tokenForm.id ? (tokenForm.scopeId ? [tokenForm.scopeId] : []) : tokenForm.scopeIds
|
||||
const scopeIds = tokenForm.scopeIds
|
||||
if (!scopeIds.length) return ElMessage.warning('至少选择一个作用域')
|
||||
await run(async () => {
|
||||
const body = JSON.stringify({ memo: tokenForm.memo, scopeIds, expireTime: tokenForm.expireTime })
|
||||
@@ -209,6 +215,48 @@ async function deleteToken(token) {
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
async function loadAccounts() {
|
||||
accounts.value = await api('/accounts')
|
||||
}
|
||||
|
||||
function openAccount(account = null) {
|
||||
Object.assign(accountForm, account ? {
|
||||
id: account.id, account: account.account, name: account.name || '', password: '', status: account.status,
|
||||
} : { id: null, account: '', name: '', password: '', status: 1 })
|
||||
accountDialog.value = true
|
||||
}
|
||||
|
||||
async function saveAccount() {
|
||||
if (!accountForm.account.trim()) return ElMessage.warning('请输入登录账号')
|
||||
if (!accountForm.id && accountForm.password.length < 12) return ElMessage.warning('密码至少 12 位')
|
||||
if (accountForm.password && accountForm.password.length < 12) return ElMessage.warning('密码至少 12 位')
|
||||
await run(async () => {
|
||||
const body = JSON.stringify({
|
||||
account: accountForm.account, name: accountForm.name,
|
||||
password: accountForm.password, status: accountForm.status,
|
||||
})
|
||||
if (accountForm.id) {
|
||||
await api(`/accounts/${accountForm.id}`, { method: 'PUT', body })
|
||||
} else {
|
||||
await api('/accounts', { method: 'POST', body })
|
||||
}
|
||||
accountDialog.value = false
|
||||
await loadAccounts()
|
||||
ElMessage.success(accountForm.id ? '账号已更新' : '账号已添加')
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
async function deleteAccount(account) {
|
||||
try {
|
||||
await ElMessageBox.confirm(`确认删除账号“${account.account}”?删除后该账号将立即无法登录。`, '删除账号', { type: 'warning' })
|
||||
} catch { return }
|
||||
await run(async () => {
|
||||
await api(`/accounts/${account.id}`, { method: 'DELETE' })
|
||||
await loadAccounts()
|
||||
ElMessage.success('账号已删除')
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
async function loadOpenClients() {
|
||||
;[openClients.value, scopes.value] = await Promise.all([api('/open-clients'), api('/scopes')])
|
||||
}
|
||||
@@ -380,6 +428,7 @@ onMounted(async () => {
|
||||
<div class="logo"><span>趣</span> 智能体服务后台</div>
|
||||
<nav aria-label="主导航">
|
||||
<button :class="['menu-item', { active: current === 'tokens' }]" data-testid="admin-menu-tokens" @click="switchMenu('tokens')">👥 用户与 Token</button>
|
||||
<button v-if="session.account === 'admin'" :class="['menu-item', { active: current === 'accounts' }]" data-testid="admin-menu-accounts" @click="switchMenu('accounts')">▣ 账号管理</button>
|
||||
<button :class="['menu-item', { active: current === 'clients' }]" data-testid="admin-menu-clients" @click="switchMenu('clients')">🔐 服务密钥</button>
|
||||
<button :class="['menu-item', { active: current === 'tools' }]" data-testid="admin-menu-tools" @click="switchMenu('tools')">🔧 工具管理</button>
|
||||
<button :class="['menu-item', { active: current === 'scopes' }]" data-testid="admin-menu-scopes" @click="switchMenu('scopes')">🧩 作用域管理</button>
|
||||
@@ -448,6 +497,7 @@ onMounted(async () => {
|
||||
<el-table-column label="前缀" min-width="125"><template #default="{ row }"><code>{{ row.keyPrefix }}…</code></template></el-table-column>
|
||||
<el-table-column prop="memo" label="备注" min-width="150" />
|
||||
<el-table-column label="作用域" min-width="180"><template #default="{ row }"><el-tag v-for="name in row.scopeNames" :key="name" size="small" effect="plain">{{ name }}</el-tag></template></el-table-column>
|
||||
<el-table-column label="签发账号" min-width="145"><template #default="{ row }"><span v-if="row.issuerAccount">{{ row.issuerAccount }}<template v-if="row.issuerName">({{ row.issuerName }})</template></span><span v-else>{{ row.issuerId === 0 ? '系统自动' : '未知' }}</span></template></el-table-column>
|
||||
<el-table-column label="状态" width="90"><template #default="{ row }"><el-tag :type="row.status === 1 ? 'success' : 'danger'" size="small">{{ row.status === 1 ? '正常' : '已禁用' }}</el-tag></template></el-table-column>
|
||||
<el-table-column label="过期时间" min-width="150"><template #default="{ row }">{{ expiry(row.expireTime) }}</template></el-table-column>
|
||||
<el-table-column label="最后使用" min-width="150"><template #default="{ row }">{{ date(row.lastUsedTime) }}</template></el-table-column>
|
||||
@@ -463,6 +513,30 @@ onMounted(async () => {
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template v-if="session.account === 'admin' && openTabs.includes('accounts')">
|
||||
<div v-show="current === 'accounts'">
|
||||
<article class="card">
|
||||
<div class="card-title">
|
||||
<span>管理后台账号</span>
|
||||
<el-button type="primary" data-testid="admin-btn-new-account" @click="openAccount()">+ 添加账号</el-button>
|
||||
</div>
|
||||
<el-alert title="账号仅用于登录本管理后台;删除或禁用后,该账号的现有会话立即失效。" type="info" :closable="false" show-icon />
|
||||
<el-table :data="accounts" class="client-table" data-testid="admin-table-accounts" empty-text="暂无账号">
|
||||
<el-table-column prop="account" label="登录账号" min-width="180" />
|
||||
<el-table-column prop="name" label="姓名" min-width="140"><template #default="{ row }">{{ row.name || '—' }}</template></el-table-column>
|
||||
<el-table-column label="状态" width="100"><template #default="{ row }"><el-tag :type="row.status === 1 ? 'success' : 'danger'" size="small">{{ row.status === 1 ? '正常' : '已禁用' }}</el-tag></template></el-table-column>
|
||||
<el-table-column label="创建时间" min-width="165"><template #default="{ row }">{{ date(row.createTime) }}</template></el-table-column>
|
||||
<el-table-column label="修改时间" min-width="165"><template #default="{ row }">{{ date(row.modifyTime) }}</template></el-table-column>
|
||||
<el-table-column label="操作" width="145" fixed="right"><template #default="{ row }">
|
||||
<el-button link type="primary" :data-testid="`admin-account-edit-${row.id}`" @click="openAccount(row)">编辑</el-button>
|
||||
<el-button link type="danger" :disabled="row.id === session.id" :data-testid="`admin-account-delete-${row.id}`" @click="deleteAccount(row)">删除</el-button>
|
||||
</template></el-table-column>
|
||||
</el-table>
|
||||
<p class="hint">编辑时密码留空表示不修改;不能禁用或删除当前登录账号,且系统至少保留一个正常账号。</p>
|
||||
</article>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template v-if="openTabs.includes('clients')">
|
||||
<div v-show="current === 'clients'">
|
||||
<article class="card">
|
||||
@@ -583,19 +657,26 @@ onMounted(async () => {
|
||||
<el-form-item label="所属人员"><strong>{{ selectedUser?.name }}</strong></el-form-item>
|
||||
<el-form-item label="用途备注"><el-input v-model="tokenForm.memo" maxlength="255" placeholder="如:Kimi 桌面用" aria-label="Token 用途备注" data-testid="admin-token-memo" /></el-form-item>
|
||||
<el-form-item label="作用域">
|
||||
<el-radio-group v-if="tokenForm.id" v-model="tokenForm.scopeId" data-testid="admin-token-scopes">
|
||||
<el-radio v-for="scope in scopes.filter(item => item.status === 1)" :key="scope.id" :value="scope.id">{{ scope.name }}</el-radio>
|
||||
</el-radio-group>
|
||||
<el-checkbox-group v-else v-model="tokenForm.scopeIds" data-testid="admin-token-scopes">
|
||||
<el-checkbox-group v-model="tokenForm.scopeIds" data-testid="admin-token-scopes">
|
||||
<el-checkbox v-for="scope in scopes.filter(item => item.status === 1)" :key="scope.id" :value="scope.id">{{ scope.name }}</el-checkbox>
|
||||
</el-checkbox-group>
|
||||
<div class="hint" style="margin:4px 0 0">{{ tokenForm.id ? '一把 Token 只绑一个作用域' : '一作用域一 Token:选几个作用域就签发几把' }}</div>
|
||||
<div class="hint" style="margin:4px 0 0">{{ tokenForm.id ? '至少选择一个作用域,保存后调整当前 Token 的作用域' : '一作用域一 Token:选几个作用域就签发几把' }}</div>
|
||||
</el-form-item>
|
||||
<el-form-item label="过期时间"><el-date-picker v-model="tokenForm.expireTime" type="datetime" value-format="YYYY-MM-DDTHH:mm:ssZ" placeholder="留空为永久" aria-label="Token 过期时间" data-testid="admin-token-expiry" /></el-form-item>
|
||||
</el-form>
|
||||
<template #footer><el-button data-testid="admin-token-cancel" @click="tokenDialog = false">取消</el-button><el-button type="primary" data-testid="admin-token-save" @click="saveToken">确认</el-button></template>
|
||||
</el-dialog>
|
||||
|
||||
<el-dialog v-model="accountDialog" :title="accountForm.id ? '编辑后台账号' : '添加后台账号'" width="520px" destroy-on-close>
|
||||
<el-form label-width="88px">
|
||||
<el-form-item label="登录账号"><el-input v-model="accountForm.account" maxlength="64" autocomplete="off" :disabled="accountForm.account === 'admin'" aria-label="后台登录账号" data-testid="admin-account-account" /><div v-if="accountForm.account === 'admin'" class="hint">内置 admin 账号不能改名</div></el-form-item>
|
||||
<el-form-item label="姓名"><el-input v-model="accountForm.name" maxlength="64" autocomplete="off" aria-label="后台账号姓名" data-testid="admin-account-name" /></el-form-item>
|
||||
<el-form-item label="密码"><el-input v-model="accountForm.password" type="password" show-password maxlength="1024" autocomplete="new-password" :placeholder="accountForm.id ? '留空则不修改密码' : '至少 12 位'" aria-label="后台账号密码" data-testid="admin-account-password" /></el-form-item>
|
||||
<el-form-item label="状态"><el-switch v-model="accountForm.status" :active-value="1" :inactive-value="0" active-text="正常" inactive-text="禁用" :disabled="accountForm.id === session.id" data-testid="admin-account-status" /></el-form-item>
|
||||
</el-form>
|
||||
<template #footer><el-button data-testid="admin-account-cancel" @click="accountDialog = false">取消</el-button><el-button type="primary" data-testid="admin-account-save" @click="saveAccount">保存</el-button></template>
|
||||
</el-dialog>
|
||||
|
||||
<el-dialog v-model="scopeDialog" :title="scopeForm.id ? '编辑作用域' : '新建作用域'" width="520px" destroy-on-close>
|
||||
<el-form label-width="88px">
|
||||
<el-form-item label="名称"><el-input v-model="scopeForm.name" maxlength="64" aria-label="作用域名称" data-testid="admin-scope-name" /></el-form-item>
|
||||
|
||||
@@ -13,6 +13,14 @@ test('prototype interactions expose stable test ids', async () => {
|
||||
'admin-menu-tools',
|
||||
'admin-menu-scopes',
|
||||
'admin-menu-audit',
|
||||
'admin-menu-accounts',
|
||||
'admin-btn-new-account',
|
||||
'admin-table-accounts',
|
||||
'admin-account-account',
|
||||
'admin-account-name',
|
||||
'admin-account-password',
|
||||
'admin-account-status',
|
||||
'admin-account-save',
|
||||
'admin-input-user-search',
|
||||
'admin-btn-issue-token',
|
||||
'admin-btn-new-client',
|
||||
@@ -41,4 +49,29 @@ test('multi-tab navigation exposes tab bar and close controls', async () => {
|
||||
assert.match(source, /openTabs\.includes\('scopes'\)/, 'scopes panel gated by openTabs')
|
||||
assert.match(source, /openTabs\.includes\('audit'\)/, 'audit panel gated by openTabs')
|
||||
assert.match(source, /function closeTab\(/, 'closeTab handler')
|
||||
assert.match(source, /openTabs\.includes\('accounts'\)/, 'accounts panel gated by openTabs')
|
||||
})
|
||||
|
||||
test('token edit uses the same multi-scope selection as token issuing', async () => {
|
||||
const source = await readFile(new URL('../src/App.vue', import.meta.url), 'utf8')
|
||||
assert.match(source, /scopeIds: \[\.\.\.token\.scopeIds\]/, 'edit copies all current scopes')
|
||||
assert.match(source, /const scopeIds = tokenForm\.scopeIds/, 'save uses one scope list for add and edit')
|
||||
assert.doesNotMatch(source, /tokenForm\.scopeId\b/, 'legacy single-scope editor removed')
|
||||
assert.doesNotMatch(source, /<el-radio-group v-if="tokenForm\.id"/, 'single-scope radio group removed')
|
||||
})
|
||||
|
||||
test('account management uses CRUD endpoints and token rows expose issuer', async () => {
|
||||
const source = await readFile(new URL('../src/App.vue', import.meta.url), 'utf8')
|
||||
assert.match(source, /api\('\/accounts'\)/, 'account list')
|
||||
assert.match(source, /api\('\/accounts', \{ method: 'POST'/, 'account create')
|
||||
assert.match(source, /api\(`\/accounts\/\$\{accountForm\.id\}`, \{ method: 'PUT'/, 'account update')
|
||||
assert.match(source, /api\(`\/accounts\/\$\{account\.id\}`, \{ method: 'DELETE'/, 'account delete')
|
||||
assert.match(source, /row\.issuerAccount/, 'token issuer account column')
|
||||
})
|
||||
|
||||
test('account management is only available to the admin account', async () => {
|
||||
const source = await readFile(new URL('../src/App.vue', import.meta.url), 'utf8')
|
||||
assert.match(source, /<button v-if="session\.account === 'admin'"[^>]*data-testid="admin-menu-accounts"/, 'admin-only menu')
|
||||
assert.match(source, /v-if="session\.account === 'admin' && openTabs\.includes\('accounts'\)"/, 'admin-only panel')
|
||||
assert.match(source, /if \(id === 'accounts' && session\.account !== 'admin'\) return/, 'admin-only navigation guard')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user