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

+3 -3
View File
@@ -1,6 +1,6 @@
// PUT /api/users/[id] (更新资料), PUT /api/users/[id]/role (改角色)
import pool from '@/lib/db';
import { getUser, requireAdmin, isFirstUser } from '@/lib/auth';
import { getUser, isSuperAdmin, requireAdmin } from '@/lib/auth';
export async function PUT(req, { params }) {
const { id } = await params;
@@ -22,8 +22,8 @@ export async function PUT(req, { params }) {
if (id === user.id) {
return Response.json({ error: '不能修改自己的角色' }, { status: 400 });
}
// 超级管理员(第一个用户)不能被降级
if (role !== 'admin' && await isFirstUser(id)) {
// 超级管理员不能被降级(兼容旧的角色更新入口)
if (role !== 'admin' && await isSuperAdmin(id)) {
return Response.json({ error: '不能降级超级管理员' }, { status: 403 });
}
try {