import { NextRequest, NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/lib/auth'; import { prisma } from '@/lib/prisma'; import { getAllMaterialTypes, getDepartmentPermissions, createDefaultPermissionsForDepartment, isSuperAdmin, } from '@/lib/department-permission'; import { filterSelectableMaterialTypes } from '@/lib/material-type-options'; /** * 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 { searchParams } = new URL(request.url); const searchKeyword = searchParams.get('search') || ''; // 获取所有部门 const departments = await prisma.department.findMany({ orderBy: { name: 'asc' }, }); // 过滤部门(如果有关键词) const filteredDepartments = searchKeyword ? departments.filter((dept) => dept.name.includes(searchKeyword)) : departments; const materialTypes = filterSelectableMaterialTypes(await getAllMaterialTypes()); // 获取所有部门的权限配置 const permissionsMap: Record> = {}; const linkedDepartments: Record = {}; const hasCustomPermissions: Record = {}; // 记录哪些部门有自定义权限配置 const departmentsForInheritance = departments.map((dept) => ({ id: dept.id, name: dept.name, parentId: dept.parentId, })); for (const dept of filteredDepartments) { const permissions = await getDepartmentPermissions( dept.name, departmentsForInheritance, materialTypes, ); permissionsMap[dept.name] = permissions; // 检查是否有关联部门 const deptPermissions = await prisma.departmentMaterialPermission.findFirst({ where: { department: dept.name, linkedToDepartment: { not: null }, }, }); if (deptPermissions?.linkedToDepartment) { linkedDepartments[dept.name] = deptPermissions.linkedToDepartment; } // 检查是否有自定义权限配置(不是继承的) const customPermCount = await prisma.departmentMaterialPermission.count({ where: { department: dept.name, linkedToDepartment: null, // 没有关联到其他部门 }, }); hasCustomPermissions[dept.name] = customPermCount > 0; } return NextResponse.json({ departments: filteredDepartments.map((d) => ({ id: d.id, name: d.name, parentId: d.parentId })), permissions: permissionsMap, linkedDepartments, hasCustomPermissions, materialTypes, }); } catch (error) { console.error('[Department Permissions] 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 body = await request.json(); const { department, materialType, canView, canDownload, canShare, canDownloadSource } = body; if (!department || !materialType) { return NextResponse.json({ error: '部门名称和素材类型不能为空' }, { status: 400 }); } // 检查是否有关联部门(如果有关联,不允许直接修改) const existing = await prisma.departmentMaterialPermission.findUnique({ where: { department_materialType: { department, materialType, }, }, }); if (existing?.linkedToDepartment) { return NextResponse.json( { error: '该部门已关联到其他部门,请先取消关联' }, { status: 400 } ); } // 更新或创建权限配置 const permission = await prisma.departmentMaterialPermission.upsert({ where: { department_materialType: { department, materialType, }, }, update: { canView: canView ?? false, canDownload: canDownload ?? false, canShare: canShare ?? true, canDownloadSource: canDownloadSource ?? false, linkedToDepartment: null, // 更新时清除关联 }, create: { department, materialType, canView: canView ?? false, canDownload: canDownload ?? false, canShare: canShare ?? true, canDownloadSource: canDownloadSource ?? false, linkedToDepartment: null, }, }); return NextResponse.json({ permission }); } catch (error) { console.error('[Department Permissions] POST Error:', error); return NextResponse.json({ error: '更新权限配置失败' }, { status: 500 }); } }