mirror of
https://github.com/beilunyang/moemail.git
synced 2026-09-07 00:06:44 +08:00
feat(roles): add delete user functionality to role management
Add ability for Emperor to delete users from the role management panel: - New POST /api/roles/delete endpoint with PROMOTE_USER permission guard, blocks deleting self or other emperors, cascades user data cleanup - Trash button on each non-emperor row with confirmation dialog - Auto-navigate to previous page when deleting the last user on a page - i18n strings added for all 5 locales
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { createDb } from "@/lib/db";
|
||||
import { users, userRoles, apiKeys } from "@/lib/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { ROLES, PERMISSIONS } from "@/lib/permissions";
|
||||
import { checkPermission } from "@/lib/auth";
|
||||
import { getUserId } from "@/lib/apiKey";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const canManage = await checkPermission(PERMISSIONS.PROMOTE_USER);
|
||||
if (!canManage) {
|
||||
return Response.json({ error: "权限不足" }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { userId } = await request.json() as { userId: string };
|
||||
if (!userId) {
|
||||
return Response.json({ error: "缺少必要参数" }, { status: 400 });
|
||||
}
|
||||
|
||||
const currentUserId = await getUserId();
|
||||
if (userId === currentUserId) {
|
||||
return Response.json({ error: "不能删除自己" }, { status: 400 });
|
||||
}
|
||||
|
||||
const db = createDb();
|
||||
|
||||
const currentUserRole = await db.query.userRoles.findFirst({
|
||||
where: eq(userRoles.userId, userId),
|
||||
with: {
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (currentUserRole?.role.name === ROLES.EMPEROR) {
|
||||
return Response.json({ error: "不能删除皇帝" }, { status: 400 });
|
||||
}
|
||||
|
||||
// apiKeys 未配置级联删除,需先手动删除;其余(accounts / emails→messages / webhooks / userRoles)由外键级联处理
|
||||
await db.delete(apiKeys).where(eq(apiKeys.userId, userId));
|
||||
await db.delete(users).where(eq(users.id, userId));
|
||||
|
||||
return Response.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("Failed to delete user:", error);
|
||||
return Response.json({ error: "操作失败" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user