完善用户管理与凭据事务处理

This commit is contained in:
xuans committed 2026-08-30 19:41:06 +08:00
1 parent 7f0af703ed
commit 0e2222bcfe
33 files changed
+2135 -68

No files matched your search

+5 -3
View File
@@ -68,16 +68,17 @@ export function useDashboard() {
`${sanitizeSSHKeySegment(sshKeyCommandUsername.value, "用户名")}+scert-${sshKeyRandomSuffix.value}`,
);
const sshKeyPath = computed(() => `/ssl/ssh/${sshKeyFileName.value}`);
const sshKeyPublicPath = computed(() => `${sshKeyPath.value}.pub`);
const sshKeyPrivatePath = computed(() => `${sshKeyPath.value}.key`);
const sshKeyPublicPath = computed(() => sshKeyPath.value);
const sshKeyRemoteTarget = computed(
() =>
`${sanitizeSSHKeySegment(sshKeyCommandUsername.value, "用户名")}@${sanitizeSSHHost(sshKeyCommandHost.value)}`,
);
const sshKeyCommands = computed(() => ({
windows:
`New-Item -ItemType Directory -Force -Path "/ssl/ssh" | Out-Null; ssh-keygen -t ed25519 -C "scert-deploy" -f "${sshKeyPath.value}"`,
`New-Item -ItemType Directory -Force -Path "/ssl/ssh" | Out-Null; ssh-keygen -t ed25519 -C "scert-deploy" -f "${sshKeyPrivatePath.value}"; Move-Item -Force "${sshKeyPrivatePath.value}.pub" "${sshKeyPublicPath.value}"`,
linux:
`sudo install -d -m 700 -o "$(id -un)" -g "$(id -gn)" /ssl/ssh && ssh-keygen -t ed25519 -C "scert-deploy" -f "${sshKeyPath.value}"`,
`sudo install -d -m 700 -o "$(id -un)" -g "$(id -gn)" /ssl/ssh && ssh-keygen -t ed25519 -C "scert-deploy" -f "${sshKeyPrivatePath.value}" && mv -f "${sshKeyPrivatePath.value}.pub" "${sshKeyPublicPath.value}" && chmod 644 "${sshKeyPublicPath.value}"`,
}));
const sshPublicKeyCommands = computed(() => ({
windows:
@@ -815,6 +816,7 @@ export function useDashboard() {
sshKeyCommandHost,
sshKeyFileName,
sshKeyPath,
sshKeyPrivatePath,
sshKeyPublicPath,
showSettings,
settingsSaving,
+7 -4
View File
@@ -1,4 +1,5 @@
<script setup>
import UserManagement from "../UserManagement/UserManagement.vue";
import { useDashboard } from "./Dashboard.js";
const {
@@ -43,6 +44,7 @@ const {
sshKeyCommandHost,
sshKeyFileName,
sshKeyPath,
sshKeyPrivatePath,
sshKeyPublicPath,
showSettings,
settingsSaving,
@@ -162,6 +164,8 @@ const {
</article>
</section>
<UserManagement />
<section class="panel operations-panel">
<div class="panel-heading">
<div>
@@ -877,7 +881,7 @@ const {
<div class="ssh-key-path-summary">
<div>
<span>本地私钥位置</span>
<code>{{ sshKeyPath }}</code>
<code>{{ sshKeyPrivatePath }}</code>
</div>
<div>
<span>本地公钥位置</span>
@@ -924,13 +928,12 @@ const {
</div>
<code>{{ sshPublicKeyCommands[sshKeyPlatform] }}</code>
<small>
本地公钥文件是上面显示的随机路径;安装后会追加到远程用户的
本地公钥文件就是上面显示的随机路径;安装后会追加到远程用户的
<code>~/.ssh/authorized_keys</code>。每台主机、每次新建配置都会生成新的文件名。
</small>
</div>
<p class="ssh-key-guide-note">
懒人方式:如果你能登录服务器控制台,只需执行生成命令,再把生成的
<code>.pub</code> 公钥内容追加到授权文件,最后在下方选择私钥文件即可。
懒人方式:如果你能登录服务器控制台,只需执行生成命令,再把随机路径中的公钥内容追加到授权文件,最后在下方选择私钥文件即可。
</p>
</section>
<div class="ssh-key-form-fields">
@@ -0,0 +1,111 @@
.user-management {
margin-top: 18px;
}
.user-management-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 22px;
margin-bottom: 20px;
}
.user-management-heading h2 {
margin-bottom: 8px;
font-size: 26px;
}
.user-management-description {
margin: 0;
color: #71809a;
font-size: 13px;
}
.user-management-actions,
.user-row-actions {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 9px;
}
.user-message {
margin: 12px 0;
padding: 11px 14px;
border-radius: 10px;
font-size: 13px;
}
.error-message {
border: 1px solid #f0c4c4;
color: #9c3b3b;
background: #fff2f2;
}
.notice-message {
border: 1px solid #bfe7d1;
color: #24734b;
background: #effbf4;
}
.user-empty {
padding: 30px 12px;
border: 1px dashed #d5ddea;
border-radius: 12px;
color: #7a879d;
text-align: center;
}
.user-table-wrap {
overflow-x: auto;
}
.user-table {
width: 100%;
border-collapse: collapse;
color: #394963;
font-size: 13px;
}
.user-table th,
.user-table td {
padding: 13px 10px;
border-bottom: 1px solid #edf0f5;
text-align: left;
vertical-align: middle;
white-space: nowrap;
}
.user-table th {
color: #7a879d;
font-size: 12px;
font-weight: 700;
}
.user-table td:first-child {
color: #263752;
}
.user-badge,
.user-status {
display: inline-flex;
align-items: center;
padding: 4px 8px;
border-radius: 999px;
font-size: 11px;
font-weight: 700;
}
.user-badge {
color: #315aaa;
background: #edf3ff;
}
.user-status.active {
color: #24734b;
background: #effbf4;
}
.user-status.disabled {
color: #91672e;
background: #fff7e8;
}
.user-modal {
width: min(100%, 620px);
}
@media (max-width: 720px) {
.user-management-heading {
align-items: stretch;
flex-direction: column;
}
.user-management-actions {
justify-content: flex-start;
}
.user-table th,
.user-table td {
padding: 11px 8px;
}
}
@@ -0,0 +1,197 @@
import { computed, onMounted, ref } from "vue";
const roleLabels = {
super_admin: "超级管理员",
admin: "管理员",
user: "普通用户",
};
const statusLabels = {
active: "正常",
disabled: "已禁用",
};
export function useUserManagement() {
const visible = ref(false);
const users = ref([]);
const loading = ref(false);
const saving = ref(false);
const error = ref("");
const notice = ref("");
const showForm = ref(false);
const editingId = ref("");
const formError = ref("");
const form = ref(createEmptyForm());
const isEditing = computed(() => Boolean(editingId.value));
const formTitle = computed(() => (isEditing.value ? "编辑用户" : "添加用户"));
async function request(path, options = {}) {
const response = await fetch(path, {
...options,
credentials: "include",
headers: {
...(options.body ? { "Content-Type": "application/json" } : {}),
...(options.headers || {}),
},
});
if (response.status === 401) {
window.location.assign("/login");
throw new Error("登录会话已过期,请重新登录");
}
return response;
}
async function loadUsers() {
loading.value = true;
error.value = "";
try {
const response = await request("/api/v1/users");
if (response.status === 403 || response.status === 404) {
visible.value = false;
return;
}
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "读取用户列表失败");
users.value = result.items || [];
visible.value = true;
} catch (err) {
if (err.message !== "登录会话已过期,请重新登录") {
error.value = err.message || "读取用户列表失败";
}
} finally {
loading.value = false;
}
}
function createEmptyForm() {
return {
username: "",
email: "",
password: "",
role: "user",
status: "active",
};
}
function openCreate() {
editingId.value = "";
form.value = createEmptyForm();
formError.value = "";
showForm.value = true;
}
function openEdit(item) {
editingId.value = item.id;
form.value = {
username: item.username || "",
email: item.email || "",
password: "",
role: item.role || "user",
status: item.status || "active",
};
formError.value = "";
showForm.value = true;
}
function closeForm() {
if (!saving.value) showForm.value = false;
}
async function saveUser() {
formError.value = "";
const payload = { ...form.value };
if (!payload.password) delete payload.password;
if (!payload.username.trim()) {
formError.value = "请输入用户名";
return;
}
if (!isEditing.value && !payload.password) {
formError.value = "请输入密码";
return;
}
saving.value = true;
try {
const path = isEditing.value
? `/api/v1/users/${editingId.value}`
: "/api/v1/users";
const response = await request(path, {
method: isEditing.value ? "PUT" : "POST",
body: JSON.stringify(payload),
});
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "保存用户失败");
showForm.value = false;
notice.value = isEditing.value ? "用户信息已更新" : "用户已创建";
await loadUsers();
} catch (err) {
formError.value = err.message || "保存用户失败";
} finally {
saving.value = false;
}
}
async function toggleStatus(item) {
const nextStatus = item.status === "active" ? "disabled" : "active";
if (item.role === "super_admin" && nextStatus === "disabled") {
notice.value = "最后一个超级管理员不能被禁用";
return;
}
try {
const response = await request(`/api/v1/users/${item.id}`, {
method: "PUT",
body: JSON.stringify({
username: item.username,
email: item.email || "",
role: item.role,
status: nextStatus,
}),
});
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "更新用户状态失败");
notice.value = nextStatus === "active" ? "用户已启用" : "用户已禁用";
await loadUsers();
} catch (err) {
error.value = err.message || "更新用户状态失败";
}
}
async function deleteUser(item) {
if (!window.confirm(`确定删除用户“${item.username}”吗?`)) return;
try {
const response = await request(`/api/v1/users/${item.id}`, {
method: "DELETE",
});
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "删除用户失败");
notice.value = "用户已删除";
await loadUsers();
} catch (err) {
error.value = err.message || "删除用户失败";
}
}
onMounted(loadUsers);
return {
visible,
users,
loading,
saving,
error,
notice,
showForm,
form,
formError,
formTitle,
isEditing,
roleLabels,
statusLabels,
openCreate,
openEdit,
closeForm,
saveUser,
toggleStatus,
deleteUser,
loadUsers,
};
}
@@ -0,0 +1,138 @@
<script setup>
import { useUserManagement } from "./UserManagement.js";
const {
visible,
users,
loading,
saving,
error,
notice,
showForm,
form,
formError,
formTitle,
isEditing,
roleLabels,
statusLabels,
openCreate,
openEdit,
closeForm,
saveUser,
toggleStatus,
deleteUser,
loadUsers,
} = useUserManagement();
</script>
<template>
<section v-if="visible" class="user-management panel">
<div class="user-management-heading">
<div>
<p class="eyebrow">权限与账号</p>
<h2>用户管理</h2>
<p class="user-management-description">
管理登录账号、角色和使用状态。密码只在服务端保存摘要。
</p>
</div>
<div class="user-management-actions">
<button class="secondary-button" type="button" :disabled="loading" @click="loadUsers">
{{ loading ? "读取中……" : "刷新列表" }}
</button>
<button class="primary-button" type="button" @click="openCreate">添加用户</button>
</div>
</div>
<p v-if="error" class="user-message error-message">{{ error }}</p>
<p v-if="notice" class="user-message notice-message">{{ notice }}</p>
<div v-if="loading && users.length === 0" class="user-empty">正在读取用户列表……</div>
<div v-else-if="users.length === 0" class="user-empty">暂无数据库用户,配置中的超级管理员仍可登录。</div>
<div v-else class="user-table-wrap">
<table class="user-table">
<thead>
<tr>
<th>账号</th>
<th>邮箱</th>
<th>角色</th>
<th>状态</th>
<th>创建时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in users" :key="item.id">
<td><strong>{{ item.username }}</strong></td>
<td>{{ item.email || "未绑定" }}</td>
<td><span class="user-badge">{{ roleLabels[item.role] || item.role }}</span></td>
<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">
<button class="text-button" type="button" @click="openEdit(item)">编辑</button>
<button class="text-button" type="button" @click="toggleStatus(item)">
{{ item.status === "active" ? "禁用" : "启用" }}
</button>
<button v-if="item.role !== 'super_admin'" class="text-button danger-text" type="button" @click="deleteUser(item)">删除</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div v-if="showForm" class="modal-backdrop" @click.self="closeForm">
<section class="modal-card user-modal" role="dialog" aria-modal="true" :aria-label="formTitle">
<div class="modal-heading">
<div>
<p class="eyebrow">账号配置</p>
<h2>{{ formTitle }}</h2>
</div>
<button class="close-button" type="button" aria-label="关闭" @click="closeForm">×</button>
</div>
<form @submit.prevent="saveUser">
<div class="form-grid">
<label class="field">
<span>用户名</span>
<input v-model="form.username" type="text" autocomplete="off" placeholder="3 到 64 个字符" />
</label>
<label class="field">
<span>绑定邮箱 <small>可选</small></span>
<input v-model="form.email" type="email" autocomplete="off" placeholder="用于邮箱登录" />
</label>
</div>
<label class="field">
<span>密码 <small>{{ isEditing ? "留空表示不修改,8 到 12 个字符" : "8 到 12 个字符" }}</small></span>
<input v-model="form.password" type="password" autocomplete="new-password" placeholder="请输入密码" />
</label>
<div class="form-grid">
<label class="field">
<span>角色</span>
<select v-model="form.role">
<option value="user">普通用户</option>
<option value="admin">管理员</option>
<option value="super_admin">超级管理员</option>
</select>
</label>
<label class="field">
<span>状态</span>
<select v-model="form.status">
<option value="active">正常</option>
<option value="disabled">已禁用</option>
</select>
</label>
</div>
<p v-if="formError" class="form-error">{{ formError }}</p>
<div class="modal-actions">
<button class="secondary-button" type="button" :disabled="saving" @click="closeForm">取消</button>
<button class="primary-button" type="submit" :disabled="saving">{{ saving ? "保存中……" : "保存用户" }}</button>
</div>
</form>
</section>
</div>
</section>
</template>
<style scoped>
@import "./UserManagement.css";
</style>