fix: prevent super admin demotion
This commit is contained in:
1 parent
3840bb3c54
commit
96e0e6f08e
8 files changed
+47
-18
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// GET /api/users (admin) — 支持 ?username= 按用户名查询
|
||||
import pool from '@/lib/db';
|
||||
import { getUser, requireAdmin } from '@/lib/auth';
|
||||
import { getUser, getSuperAdminId, requireAdmin } from '@/lib/auth';
|
||||
|
||||
export async function GET(req) {
|
||||
const user = getUser(req);
|
||||
@@ -12,10 +12,7 @@ export async function GET(req) {
|
||||
|
||||
try {
|
||||
// 超级管理员 = 最早注册的 admin(对齐迁移:无 admin 时自动提升最早注册用户)
|
||||
const superR = await pool.query(
|
||||
`SELECT id FROM users WHERE role = 'admin' ORDER BY created_at ASC, id ASC LIMIT 1`
|
||||
);
|
||||
const superAdminId = superR.rows[0]?.id || null;
|
||||
const superAdminId = await getSuperAdminId();
|
||||
|
||||
let query = 'SELECT id, username, avatar_url, bio, role, created_at FROM users';
|
||||
const params = [];
|
||||
|
||||
@@ -80,7 +80,9 @@ export default function AdminUsers() {
|
||||
{u.created_at ? new Date(u.created_at).toLocaleDateString(dateLocale) : '—'}
|
||||
</td>
|
||||
<td style={{ padding: '10px 12px', textAlign: 'center' }}>
|
||||
{isCurrent ? (
|
||||
{u.is_super ? (
|
||||
<span style={{ fontSize: 12, color: '#94a3b8' }}>{t('admin.user.cannotSuper')}</span>
|
||||
) : isCurrent ? (
|
||||
<span style={{ fontSize: 12, color: '#94a3b8' }}>{t('admin.user.cannotSelf')}</span>
|
||||
) : isAdmin ? (
|
||||
<button className="btn-sm btn-secondary" style={{ padding: '4px 12px' }} onClick={() => handleRole(u, 'user')}>{t('admin.user.makeUser')}</button>
|
||||
|
||||
+14
-8
@@ -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;
|
||||
}
|
||||
@@ -208,6 +208,7 @@
|
||||
"registeredAt": "Registered",
|
||||
"you": "You",
|
||||
"cannotSelf": "You cannot edit yourself",
|
||||
"cannotSuper": "Super admin cannot be changed",
|
||||
"makeUser": "Make user",
|
||||
"makeAdmin": "Make admin",
|
||||
"roleSuper": "Super admin"
|
||||
|
||||
@@ -208,6 +208,7 @@
|
||||
"registeredAt": "注册时间",
|
||||
"you": "你",
|
||||
"cannotSelf": "不可操作自己",
|
||||
"cannotSuper": "不可修改超级管理员",
|
||||
"makeUser": "设为普通用户",
|
||||
"makeAdmin": "设为管理员",
|
||||
"roleSuper": "超级管理员"
|
||||
|
||||
@@ -20,6 +20,7 @@ async function api(path, options = {}) {
|
||||
}
|
||||
|
||||
let adminToken = null;
|
||||
let adminId = null;
|
||||
let userToken = null;
|
||||
let userId = null;
|
||||
const adminUser = { email: `adm${suffix}@test.com`, password: '123456', username: `adm${suffix}` };
|
||||
@@ -34,6 +35,7 @@ test('设置: 注册管理员 + 登录', async () => {
|
||||
body: JSON.stringify({ email: adminUser.email, password: adminUser.password }),
|
||||
});
|
||||
adminToken = data.token;
|
||||
adminId = data.user.id;
|
||||
assert.ok(adminToken);
|
||||
});
|
||||
|
||||
@@ -85,6 +87,23 @@ test('管理员修改普通用户角色', async () => {
|
||||
assert.equal(status, 200);
|
||||
});
|
||||
|
||||
test('超级管理员不能被其他管理员降级', async () => {
|
||||
const { data: users } = await api('/users', {
|
||||
headers: { Authorization: `Bearer ${adminToken}` },
|
||||
});
|
||||
const superAdmin = users.find((user) => user.is_super);
|
||||
assert.ok(superAdmin);
|
||||
|
||||
const actorToken = superAdmin.id === adminId ? userToken : adminToken;
|
||||
const { status, data } = await api('/users/' + superAdmin.id + '/role', {
|
||||
method: 'PUT',
|
||||
headers: { Authorization: `Bearer ${actorToken}` },
|
||||
body: JSON.stringify({ role: 'user' }),
|
||||
});
|
||||
assert.equal(status, 403);
|
||||
assert.equal(data.error, '不能降级超级管理员');
|
||||
});
|
||||
|
||||
test('更新论坛名称', async () => {
|
||||
const { status } = await api('/settings', {
|
||||
method: 'PUT',
|
||||
|
||||
Reference in new issue
Block a user