feat: 设计菜单权限和员工端登录
This commit is contained in:
@@ -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("当前账号没有权限执行该操作");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user