修复主超级管理员配置与用户管理边界并补充登录回归

This commit is contained in:
xuans committed 2026-09-17 12:37:01 +08:00
1 parent a70aa5fe2d
commit 60b00f1bad
8 files changed
+266 -14

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@
"version": "0.1.0",
"type": "module",
"scripts": {
"test": "node --test src/App.test.js src/views/SetupWizard/SetupWizard.test.js src/views/SetupWizard/SetupWizardBootstrap.test.js src/views/SetupWizard/DatabaseSetup.test.js src/views/SetupWizard/BootstrapPayload.test.js src/views/Dashboard/Dashboard.test.js",
"test": "node --test src/App.test.js src/views/SetupWizard/SetupWizard.test.js src/views/SetupWizard/SetupWizardBootstrap.test.js src/views/SetupWizard/DatabaseSetup.test.js src/views/SetupWizard/BootstrapPayload.test.js src/views/Dashboard/Dashboard.test.js src/views/UserManagement/UserManagement.test.js",
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
@@ -20,6 +20,7 @@ export function useUserManagement() {
const notice = ref("");
const showForm = ref(false);
const editingId = ref("");
const editingRole = ref("");
const formError = ref("");
const form = ref(createEmptyForm());
const isEditing = computed(() => Boolean(editingId.value));
@@ -75,13 +76,16 @@ export function useUserManagement() {
function openCreate() {
editingId.value = "";
editingRole.value = "";
form.value = createEmptyForm();
formError.value = "";
showForm.value = true;
}
function openEdit(item) {
if (isPrimaryAccount(item)) return;
editingId.value = item.id;
editingRole.value = item.role;
form.value = {
username: item.username || "",
email: item.email || "",
@@ -99,7 +103,15 @@ export function useUserManagement() {
async function saveUser() {
formError.value = "";
if (editingId.value === "primary-super-admin") {
formError.value = "主超级管理员不能通过用户管理修改";
return;
}
const payload = { ...form.value };
if (payload.role.trim().toLowerCase() === "super_admin" && editingRole.value !== "super_admin") {
formError.value = "不能新增或提升为超级管理员";
return;
}
if (!payload.password) delete payload.password;
if (!payload.username.trim()) {
formError.value = "请输入用户名";
@@ -131,6 +143,7 @@ export function useUserManagement() {
}
async function toggleStatus(item) {
if (isPrimaryAccount(item)) return;
const nextStatus = item.status === "active" ? "disabled" : "active";
if (item.role === "super_admin" && nextStatus === "disabled") {
notice.value = "最后一个超级管理员不能被禁用";
@@ -156,6 +169,7 @@ export function useUserManagement() {
}
async function deleteUser(item) {
if (isPrimaryAccount(item)) return;
if (!window.confirm(`确定删除用户“${item.username}”吗?`)) return;
try {
const response = await request(`/api/v1/users/${item.id}`, {
@@ -170,6 +184,10 @@ export function useUserManagement() {
}
}
function isPrimaryAccount(item) {
return item.id === "primary-super-admin";
}
onMounted(loadUsers);
return {
@@ -186,6 +204,7 @@ export function useUserManagement() {
isEditing,
roleLabels,
statusLabels,
isPrimaryAccount,
openCreate,
openEdit,
closeForm,
@@ -194,4 +213,4 @@ export function useUserManagement() {
deleteUser,
loadUsers,
};
}
}
@@ -0,0 +1,53 @@
import assert from "node:assert/strict";
import test from "node:test";
import { useUserManagement } from "./UserManagement.js";
import { mountComposable } from "../../test-support/mount.js";
async function setup(t) {
const calls = [];
const mounted = await mountComposable(t, useUserManagement, async (path, options) => {
calls.push({ path, options });
return Response.json({ items: [] });
});
calls.length = 0;
return { ...mounted, calls };
}
test("主超级管理员不开放用户管理修改操作", async (t) => {
const { state, calls } = await setup(t);
const item = { id: "primary-super-admin", role: "super_admin", status: "active" };
assert.equal(state.isPrimaryAccount(item), true);
state.openEdit(item);
assert.equal(state.showForm.value, false);
await state.toggleStatus(item);
await state.deleteUser(item);
assert.equal(calls.length, 0);
});
test("禁止新增或提升为超级管理员,包括非标准大小写", async (t) => {
const { state, calls } = await setup(t);
for (const editing of [false, true]) {
for (const role of ["super_admin", " SUPER_ADMIN "]) {
if (editing) state.openEdit({ id: "operator", username: "operator", role: "admin" });
else state.openCreate();
Object.assign(state.form.value, { username: "operator", password: "OperPass123", role });
await state.saveUser();
assert.equal(state.formError.value, "不能新增或提升为超级管理员");
}
}
assert.equal(calls.length, 0);
});
test("管理员和普通用户仍能正常创建及编辑", async (t) => {
const { state, calls } = await setup(t);
for (const role of ["admin", "user"]) {
state.openCreate();
Object.assign(state.form.value, { username: "operator", password: "OperPass123", role });
await state.saveUser();
state.openEdit({ id: "operator", username: "operator", role, status: "active" });
await state.saveUser();
}
const writes = calls.filter(({ options }) => options.method);
assert.deepEqual(writes.map(({ options }) => options.method), ["POST", "PUT", "POST", "PUT"]);
assert.equal(state.formError.value, "");
});
@@ -15,6 +15,7 @@ const {
isEditing,
roleLabels,
statusLabels,
isPrimaryAccount,
openCreate,
openEdit,
closeForm,
@@ -68,7 +69,8 @@ const {
<td><span class="user-status" :class="item.status">{{ statusLabels[item.status] || item.status }}</span></td>
<td>{{ new Date(item.created_at).toLocaleString("zh-CN") }}</td>
<td>
<div class="user-row-actions">
<span v-if="isPrimaryAccount(item)" class="user-badge">受保护账号</span>
<div v-else class="user-row-actions">
<button class="text-button" type="button" @click="openEdit(item)">编辑</button>
<button class="text-button" type="button" @click="toggleStatus(item)">
{{ item.status === "active" ? "禁用" : "启用" }}
@@ -111,7 +113,7 @@ const {
<select v-model="form.role">
<option value="user">普通用户</option>
<option value="admin">管理员</option>
<option value="super_admin">超级管理员</option>
<option v-if="isEditing && form.role === 'super_admin'" value="super_admin" disabled>超级管理员</option>
</select>
</label>
<label class="field">
@@ -135,4 +137,4 @@ const {
<style scoped>
@import "./UserManagement.css";
</style>
</style>