WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

128 lines
3.7 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 { buildDepartmentDescendants, isDepartmentWithinScope } from '@/lib/departments'
// 激活/禁用用户
export async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session || (session.user as any).role !== 'ADMIN') {
return NextResponse.json(
{ error: '无权限' },
{ status: 403 }
)
}
const { userId, isActive } = await req.json()
if (!userId || typeof isActive !== 'boolean') {
return NextResponse.json(
{ error: '参数错误' },
{ status: 400 }
)
}
// 获取当前登录用户信息
const currentUser = await prisma.user.findUnique({
where: { id: (session.user as any).id },
select: { id: true, managedDepartment: true }
}) as any
if (!currentUser) {
return NextResponse.json(
{ error: '当前用户不存在' },
{ status: 404 }
)
}
// 获取当前用户的完整信息(包括 isSuperAdmin)
const currentUserFull = await prisma.user.findUnique({
where: { id: (session.user as any).id },
}) as any
let departmentDescendantsMap: Map<string, Set<string>> | undefined
if (currentUser.managedDepartment && !currentUserFull?.isSuperAdmin) {
const departments = await prisma.department.findMany({ select: { name: true } })
departmentDescendantsMap = buildDepartmentDescendants(departments)
}
// 获取目标用户信息
const targetUser = await prisma.user.findUnique({
where: { id: userId },
select: { id: true, department: true }
})
if (!targetUser) {
return NextResponse.json(
{ error: '用户不存在' },
{ status: 404 }
)
}
// 获取目标用户的完整信息(包括 isSuperAdmin)
const targetUserFull = await prisma.user.findUnique({
where: { id: userId },
}) as any
// 不能禁用自己
if (userId === (session.user as any).id) {
return NextResponse.json(
{ error: '不能禁用自己' },
{ status: 400 }
)
}
// 权限检查:只有最高权限账号或目标用户所属部门的管理员可以操作
if (!currentUserFull?.isSuperAdmin) {
// 如果是部门管理员,只能操作自己管理的部门下的用户
if (currentUser.managedDepartment) {
const allowed = isDepartmentWithinScope(
currentUser.managedDepartment,
targetUser.department,
departmentDescendantsMap
)
if (!allowed) {
return NextResponse.json(
{ error: '权限不足:只能操作自己管理的部门树范围内的用户' },
{ status: 403 }
)
}
} else {
// 没有管理部门的管理员不能操作任何人
return NextResponse.json(
{ error: '权限不足' },
{ status: 403 }
)
}
}
// 最高权限账号不能被非最高权限账号操作
if (targetUserFull?.isSuperAdmin && !currentUserFull?.isSuperAdmin) {
return NextResponse.json(
{ error: '权限不足:不能操作最高权限账号' },
{ status: 403 }
)
}
// 更新用户激活状态
await prisma.user.update({
where: { id: userId },
data: { isActive },
})
return NextResponse.json({
message: isActive ? '用户已激活' : '用户已禁用',
})
} catch (error) {
console.error('Toggle active error:', error)
return NextResponse.json(
{ error: '操作失败' },
{ status: 500 }
)
}
}