Files
stevenlee87 4960395777 refactor: favicon 移回 app/ + 删除用户改为 RESTful DELETE /api/users/[id]
- favicon: 从 public/favicon.ico 移回 app/favicon.ico(next-on-pages 1.13.16 下生产验证 /favicon.ico 正常返回)
- 删除用户: POST /api/roles/delete 改为 DELETE /api/users/[id],符合 RESTful 语义(资源 users + 标准 DELETE 方法)
2026-07-30 22:09:33 +08:00

53 lines
1.6 KiB
TypeScript

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 DELETE(
request: Request,
{ params }: { params: Promise<{ id: string }> }
) {
const canManage = await checkPermission(PERMISSIONS.PROMOTE_USER);
if (!canManage) {
return Response.json({ error: "权限不足" }, { status: 403 });
}
try {
const { id: userId } = await params;
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 targetUserRole = await db.query.userRoles.findFirst({
where: eq(userRoles.userId, userId),
with: {
role: true,
},
});
if (targetUserRole?.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 });
}
}