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

+14 -8
View File
@@ -41,12 +41,18 @@ export async function requireAdmin(user) {
}
}
// 判断是否为第一个注册的用户(超级管理员,按创建时间最早,兼容 UUID/自增 id 数据库)
export async function isFirstUser(userId) {
try {
const r = await pool.query('SELECT id FROM users ORDER BY created_at ASC, id ASC LIMIT 1');
return r.rows.length > 0 && r.rows[0].id === userId;
} catch {
return false;
}
// 超级管理员 = 当前最早注册的管理员。与管理用户列表中的标记保持一致。
export async function getSuperAdminId() {
const r = await pool.query(
`SELECT id
FROM users
WHERE role = 'admin'
ORDER BY created_at ASC, id ASC
LIMIT 1`
);
return r.rows[0]?.id || null;
}
export async function isSuperAdmin(userId) {
return !!userId && (await getSuperAdminId()) === userId;
}