Some checks failed
Security / deep-security-scan (push) Failing after 20m5s
DR Drill / dr-drill (push) Failing after 1m31s
CI / scheduled-backup (push) Failing after 1m31s
CI / backup-verify (push) Has been skipped
CI / weekly-dr-drill (push) Failing after 0s
CI / build-deploy (push) Has been cancelled
CI / security-scan (push) Has been cancelled
主要变更: - 新增 lesson-preparation 模块: 备课编辑器、节点编辑、AI 建议、知识点选择、版本历史、作业发布 - 新增 shared 通用组件: charts/question-bank-filters/schedule-list/ui (chip-nav/filter-bar/page-header/stat-card/stat-item) - 新增 student/admin 端 loading.tsx 与 error.tsx, 优化加载与错误态体验 - 新增 teacher/lesson-plans 页面 (列表/新建/编辑) - 新增 drizzle 迁移 0002_tiny_lionheart 及 snapshot - 新增 textbooks/schema.ts 与 exams/utils/normalize-structure.ts - 修复 Tiptap v3 SSR hydration 崩溃 (rich-text-block immediatelyRender: false) - 重构多模块 data-access/actions/组件, 修复权限校验与类型规范 - 同步架构文档 004/005 反映新增模块、导出、依赖关系 - 归档 bugs/* 测试报告与 e2e 测试脚本 (admin/parent/student/teacher web_test)
95 lines
3.1 KiB
TypeScript
95 lines
3.1 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import type { NextRequest } from "next/server"
|
|
import { getToken } from "next-auth/jwt"
|
|
|
|
import { Permissions } from "@/shared/types/permissions"
|
|
|
|
// Route prefix → minimum required permission
|
|
// Note: /admin/announcements is covered by /admin prefix (requires school:manage)
|
|
// Note: /announcements is accessible to all authenticated users (no permission entry needed)
|
|
const ROUTE_PERMISSIONS: Record<string, string> = {
|
|
"/admin": Permissions.SCHOOL_MANAGE,
|
|
"/teacher": Permissions.EXAM_READ,
|
|
"/student": Permissions.HOMEWORK_SUBMIT,
|
|
"/parent": Permissions.EXAM_READ,
|
|
"/management": Permissions.GRADE_MANAGE,
|
|
}
|
|
|
|
// API route prefix → required permission
|
|
const API_PERMISSIONS: Record<string, string> = {
|
|
"/api/ai/chat": Permissions.AI_CHAT,
|
|
}
|
|
|
|
function resolveDefaultPath(roles: string[]): string {
|
|
if (roles.includes("admin")) return "/admin/dashboard"
|
|
if (roles.includes("grade_head") || roles.includes("teaching_head")) return "/teacher/dashboard"
|
|
if (roles.includes("teacher")) return "/teacher/dashboard"
|
|
if (roles.includes("student")) return "/student/dashboard"
|
|
if (roles.includes("parent")) return "/parent/dashboard"
|
|
return "/dashboard"
|
|
}
|
|
|
|
// Next.js 16 renamed `middleware` to `proxy`.
|
|
// See: https://nextjs.org/docs/messages/middleware-to-proxy
|
|
export async function proxy(request: NextRequest) {
|
|
const { pathname } = request.nextUrl
|
|
|
|
// Skip static assets and auth pages
|
|
if (
|
|
pathname.startsWith("/_next") ||
|
|
pathname.startsWith("/api/auth") ||
|
|
pathname === "/login" ||
|
|
pathname === "/register" ||
|
|
pathname === "/favicon.ico"
|
|
) {
|
|
return NextResponse.next()
|
|
}
|
|
|
|
const token = await getToken({
|
|
req: request,
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
})
|
|
|
|
// Not authenticated → redirect to login
|
|
if (!token) {
|
|
const loginUrl = new URL("/login", request.url)
|
|
loginUrl.searchParams.set("callbackUrl", request.url)
|
|
return NextResponse.redirect(loginUrl)
|
|
}
|
|
|
|
const permissions: string[] = (token.permissions as string[]) ?? []
|
|
const roles: string[] = (token.roles as string[]) ?? []
|
|
|
|
// Check API route permissions
|
|
for (const [prefix, requiredPerm] of Object.entries(API_PERMISSIONS)) {
|
|
if (pathname.startsWith(prefix)) {
|
|
if (!permissions.includes(requiredPerm)) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 })
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
// Check page route permissions
|
|
for (const [prefix, requiredPerm] of Object.entries(ROUTE_PERMISSIONS)) {
|
|
if (pathname.startsWith(prefix)) {
|
|
if (!permissions.includes(requiredPerm)) {
|
|
const defaultPath = resolveDefaultPath(roles)
|
|
// Carry original path + reason in URL so the target page can explain
|
|
// why the user was redirected (Web Interface Guidelines: URL reflects state).
|
|
const redirectUrl = new URL(defaultPath, request.url)
|
|
redirectUrl.searchParams.set("from", pathname)
|
|
redirectUrl.searchParams.set("reason", "forbidden")
|
|
return NextResponse.redirect(redirectUrl)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return NextResponse.next()
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
|
|
}
|