feat: 设计菜单权限和员工端登录

This commit is contained in:
湛兮
2026-05-26 12:30:38 +08:00
parent 55b99b5307
commit aa65cb0928
17 changed files with 708 additions and 64 deletions
+17 -1
View File
@@ -1,6 +1,10 @@
import type { FastifyRequest } from "fastify";
import { forbidden, unauthorized } from "../../shared/http-error";
import { authService } from "./auth.service";
import {
hasPermission,
type PermissionCode,
} from "../permissions/permission.policy";
// 统一 JWT 鉴权入口。后续新增需要登录的路由,复用这个 guard 即可。
export async function authGuard(request: FastifyRequest): Promise<void> {
@@ -23,7 +27,19 @@ export async function managementGuard(request: FastifyRequest): Promise<void> {
const user = await authService.getCurrentUser(request.user);
if (!user.canManage) {
if (!user.permissions.includes("*") && user.permissions.length === 0) {
throw forbidden("当前账号没有后台管理权限");
}
}
export function permissionGuard(permission: PermissionCode) {
return async (request: FastifyRequest): Promise<void> => {
await authGuard(request);
const user = await authService.getCurrentUser(request.user);
if (!hasPermission(user.permissions, permission)) {
throw forbidden("当前账号没有权限执行该操作");
}
};
}