fix: prevent super admin demotion

This commit is contained in:
lezi-fun committed 2026-08-12 20:12:22 +08:00
1 parent 3840bb3c54
commit 96e0e6f08e
8 files changed
+47 -18

No files matched your search

+4 -1
View File
@@ -1,6 +1,6 @@
// PUT /api/users/[id]/role (admin 改角色)
import pool from '@/lib/db';
import { getUser, requireAdmin } from '@/lib/auth';
import { getUser, isSuperAdmin, requireAdmin } from '@/lib/auth';
export async function PUT(req, { params }) {
const { id } = await params;
@@ -19,6 +19,9 @@ export async function PUT(req, { params }) {
return Response.json({ error: '不能修改自己的角色' }, { status: 400 });
}
try {
if (role !== 'admin' && await isSuperAdmin(id)) {
return Response.json({ error: '不能降级超级管理员' }, { status: 403 });
}
await pool.query('UPDATE users SET role = $1 WHERE id = $2', [role, id]);
return Response.json({ success: true });
} catch {