feat(admin): 顶部账号区支持修改自己的密码并失效旧会话
所有后台账号可在左上角修改本人密码:校验原密码、新密码至少 12 位, 成功后该账号所有既有会话立即失效需重新登录。会话校验新增密码哈希比对。 后端 615 项、前端 7 项测试全部通过。
This commit is contained in:
@@ -37,6 +37,8 @@ const issuedTokens = ref([])
|
||||
const accounts = ref([])
|
||||
const accountDialog = ref(false)
|
||||
const accountForm = reactive({ id: null, account: '', name: '', password: '', status: 1 })
|
||||
const passwordDialog = ref(false)
|
||||
const passwordForm = reactive({ oldPassword: '', newPassword: '', confirmPassword: '' })
|
||||
const clientDialog = ref(false)
|
||||
const clientForm = reactive({ id: null, name: '', memo: '', scopeIds: [] })
|
||||
const issuedOpenKey = ref(null)
|
||||
@@ -128,6 +130,27 @@ async function logout() {
|
||||
openTabs.value = ['tokens']; current.value = 'tokens'
|
||||
}
|
||||
|
||||
function openPasswordDialog() {
|
||||
Object.assign(passwordForm, { oldPassword: '', newPassword: '', confirmPassword: '' })
|
||||
passwordDialog.value = true
|
||||
}
|
||||
|
||||
async function changePassword() {
|
||||
if (!passwordForm.oldPassword) return ElMessage.warning('请输入原密码')
|
||||
if (passwordForm.newPassword.length < 12) return ElMessage.warning('新密码至少 12 位')
|
||||
if (passwordForm.newPassword !== passwordForm.confirmPassword) return ElMessage.warning('两次输入的新密码不一致')
|
||||
await run(async () => {
|
||||
await api('/auth/password', { method: 'PUT', body: JSON.stringify({
|
||||
oldPassword: passwordForm.oldPassword, newPassword: passwordForm.newPassword,
|
||||
}) })
|
||||
Object.assign(passwordForm, { oldPassword: '', newPassword: '', confirmPassword: '' })
|
||||
passwordDialog.value = false
|
||||
loggedIn.value = false; setCsrf('')
|
||||
openTabs.value = ['tokens']; current.value = 'tokens'
|
||||
ElMessage.success('密码已修改,请使用新密码重新登录')
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
async function switchMenu(id) {
|
||||
if (id === 'accounts' && session.account !== 'admin') return
|
||||
if (!openTabs.value.includes(id)) openTabs.value = [...openTabs.value, id]
|
||||
@@ -439,6 +462,7 @@ onMounted(async () => {
|
||||
<header class="header">
|
||||
<div><span class="header-kicker">控制台</span><strong>{{ title }}</strong></div>
|
||||
<div class="admin-user"><span class="avatar">{{ session.name?.slice(0, 1) }}</span>{{ session.name }}
|
||||
<el-button text type="primary" data-testid="admin-btn-change-password" @click="openPasswordDialog">修改密码</el-button>
|
||||
<el-button text data-testid="admin-btn-logout" @click="logout">退出</el-button>
|
||||
</div>
|
||||
</header>
|
||||
@@ -677,6 +701,17 @@ onMounted(async () => {
|
||||
<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="passwordDialog" title="修改密码" width="480px" destroy-on-close>
|
||||
<el-form label-width="88px">
|
||||
<el-form-item label="当前账号"><strong>{{ session.account }}</strong></el-form-item>
|
||||
<el-form-item label="原密码"><el-input v-model="passwordForm.oldPassword" type="password" show-password maxlength="1024" autocomplete="current-password" aria-label="原密码" data-testid="admin-password-old" /></el-form-item>
|
||||
<el-form-item label="新密码"><el-input v-model="passwordForm.newPassword" type="password" show-password minlength="12" maxlength="1024" autocomplete="new-password" placeholder="至少 12 位" aria-label="新密码" data-testid="admin-password-new" /></el-form-item>
|
||||
<el-form-item label="确认新密码"><el-input v-model="passwordForm.confirmPassword" type="password" show-password minlength="12" maxlength="1024" autocomplete="new-password" placeholder="再次输入新密码" aria-label="确认新密码" data-testid="admin-password-confirm" /></el-form-item>
|
||||
</el-form>
|
||||
<el-alert title="修改成功后,当前账号的已有登录会话都会失效,需要重新登录。" type="info" :closable="false" show-icon />
|
||||
<template #footer><el-button data-testid="admin-password-cancel" @click="passwordDialog = false">取消</el-button><el-button type="primary" data-testid="admin-password-save" @click="changePassword">确认修改</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>
|
||||
|
||||
@@ -75,3 +75,21 @@ test('account management is only available to the admin account', async () => {
|
||||
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')
|
||||
})
|
||||
|
||||
test('every current account can change its own password from the account area', async () => {
|
||||
const source = await readFile(new URL('../src/App.vue', import.meta.url), 'utf8')
|
||||
const required = [
|
||||
'admin-btn-change-password',
|
||||
'admin-password-old',
|
||||
'admin-password-new',
|
||||
'admin-password-confirm',
|
||||
'admin-password-cancel',
|
||||
'admin-password-save',
|
||||
]
|
||||
for (const testId of required) {
|
||||
assert.match(source, new RegExp(`data-testid=["']${testId}["']`), testId)
|
||||
}
|
||||
assert.match(source, /api\('\/auth\/password', \{ method: 'PUT'/, 'self-service password endpoint')
|
||||
assert.match(source, /newPassword: passwordForm\.newPassword/, 'only sends password fields, not another account id')
|
||||
assert.match(source, /loggedIn\.value = false/, 'returns to login after changing password')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user