154 lines
4.7 KiB
TypeScript
154 lines
4.7 KiB
TypeScript
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, username, department, role, isActive, canDownload } = await req.json()
|
||
|
||
if (!userId) {
|
||
return NextResponse.json(
|
||
{ error: '缺少用户ID' },
|
||
{ 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 oldUser = await prisma.user.findUnique({
|
||
where: { id: userId },
|
||
select: {
|
||
id: true,
|
||
username: true,
|
||
department: true
|
||
}
|
||
})
|
||
|
||
if (!oldUser) {
|
||
return NextResponse.json(
|
||
{ error: '用户不存在' },
|
||
{ status: 404 }
|
||
)
|
||
}
|
||
|
||
// 获取目标用户的完整信息(包括 isSuperAdmin)
|
||
const targetUserFull = await prisma.user.findUnique({
|
||
where: { id: userId },
|
||
}) as any
|
||
|
||
// 权限检查:只有最高权限账号或目标用户所属部门的管理员可以编辑
|
||
if (!currentUserFull?.isSuperAdmin) {
|
||
// 如果是部门管理员,只能编辑自己管理的部门下的用户
|
||
if (currentUser.managedDepartment) {
|
||
const allowed = isDepartmentWithinScope(
|
||
currentUser.managedDepartment,
|
||
oldUser.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 }
|
||
)
|
||
}
|
||
|
||
// 构建更新数据
|
||
const updateData: any = {}
|
||
if (username !== undefined) updateData.username = username
|
||
if (department !== undefined) updateData.department = department || ''
|
||
if (role !== undefined) updateData.role = role
|
||
if (isActive !== undefined) updateData.isActive = isActive
|
||
if (canDownload !== undefined) updateData.canDownload = canDownload
|
||
|
||
// 使用事务更新用户和素材
|
||
const result = await prisma.$transaction(async (tx) => {
|
||
// 更新用户信息
|
||
const updatedUser = await tx.user.update({
|
||
where: { id: userId },
|
||
data: updateData,
|
||
})
|
||
|
||
// 如果用户名发生变化,同步更新该用户上传的所有素材的authorName
|
||
if (username !== undefined && username !== oldUser.username) {
|
||
await tx.material.updateMany({
|
||
where: { userId: userId },
|
||
data: { authorName: username },
|
||
})
|
||
}
|
||
|
||
return updatedUser
|
||
})
|
||
|
||
console.log('[Update User] Success:', {
|
||
userId: result.id,
|
||
username: result.username,
|
||
department: result.department,
|
||
materialsUpdated: username !== undefined && username !== oldUser.username
|
||
})
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
user: result,
|
||
materialsUpdated: username !== undefined && username !== oldUser.username
|
||
})
|
||
} catch (error) {
|
||
console.error('[Update User] Error:', error)
|
||
return NextResponse.json(
|
||
{ error: '更新用户失败' },
|
||
{ status: 500 }
|
||
)
|
||
}
|
||
}
|
||
|