186 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { isSuperAdmin, canAddSuperAdmin, getSuperAdminCount } from '@/lib/department-permission';
/**
* GET - 获取所有超级管理员列表
*/
export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const superAdmins = await prisma.user.findMany({
where: { isSuperAdmin: true },
select: {
id: true,
email: true,
username: true,
realName: true,
department: true,
role: true,
createdAt: true,
},
orderBy: { createdAt: 'asc' },
});
const count = await getSuperAdminCount();
const canAdd = await canAddSuperAdmin();
return NextResponse.json({
superAdmins,
count,
maxCount: 4,
canAdd,
});
} catch (error) {
console.error('[Super Admins] GET Error:', error);
return NextResponse.json({ error: '获取超级管理员列表失败' }, { status: 500 });
}
}
/**
* POST - 添加超级管理员
*/
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const { userId: targetUserId } = await request.json();
if (!targetUserId) {
return NextResponse.json({ error: '用户ID不能为空' }, { status: 400 });
}
// 检查是否可以添加(最多4个)
if (!(await canAddSuperAdmin())) {
return NextResponse.json({ error: '超级管理员数量已达上限(最多4个)' }, { status: 400 });
}
// 检查目标用户是否存在
const targetUser = await prisma.user.findUnique({
where: { id: targetUserId },
});
if (!targetUser) {
return NextResponse.json({ error: '用户不存在' }, { status: 404 });
}
// 检查是否已经是超级管理员
if (targetUser.isSuperAdmin) {
return NextResponse.json({ error: '该用户已经是超级管理员' }, { status: 400 });
}
// 检查是否是管理员角色
if (targetUser.role !== 'ADMIN') {
return NextResponse.json({ error: '只有管理员才能被设置为超级管理员' }, { status: 400 });
}
// 设置为超级管理员
const updatedUser = await prisma.user.update({
where: { id: targetUserId },
data: { isSuperAdmin: true },
select: {
id: true,
email: true,
username: true,
realName: true,
department: true,
role: true,
isSuperAdmin: true,
createdAt: true,
},
});
return NextResponse.json({
message: '超级管理员添加成功',
user: updatedUser,
});
} catch (error) {
console.error('[Super Admins] POST Error:', error);
return NextResponse.json({ error: '添加超级管理员失败' }, { status: 500 });
}
}
/**
* DELETE - 移除超级管理员
*/
export async function DELETE(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
const userId = (session?.user as any)?.id;
if (!session || !(await isSuperAdmin(userId))) {
return NextResponse.json({ error: '需要超级管理员权限' }, { status: 403 });
}
const { searchParams } = new URL(request.url);
const targetUserId = searchParams.get('userId');
if (!targetUserId) {
return NextResponse.json({ error: '用户ID不能为空' }, { status: 400 });
}
// 不能移除自己
if (targetUserId === userId) {
return NextResponse.json({ error: '不能移除自己的超级管理员权限' }, { status: 400 });
}
// 检查目标用户是否存在
const targetUser = await prisma.user.findUnique({
where: { id: targetUserId },
});
if (!targetUser) {
return NextResponse.json({ error: '用户不存在' }, { status: 404 });
}
// 检查是否是超级管理员
if (!targetUser.isSuperAdmin) {
return NextResponse.json({ error: '该用户不是超级管理员' }, { status: 400 });
}
// 检查移除后是否至少还有1个超级管理员
const count = await getSuperAdminCount();
if (count <= 1) {
return NextResponse.json({ error: '至少需要保留1个超级管理员' }, { status: 400 });
}
// 移除超级管理员权限
const updatedUser = await prisma.user.update({
where: { id: targetUserId },
data: { isSuperAdmin: false },
select: {
id: true,
email: true,
username: true,
realName: true,
department: true,
role: true,
isSuperAdmin: true,
createdAt: true,
},
});
return NextResponse.json({
message: '超级管理员权限已移除',
user: updatedUser,
});
} catch (error) {
console.error('[Super Admins] DELETE Error:', error);
return NextResponse.json({ error: '移除超级管理员失败' }, { status: 500 });
}
}