refactor: 缓存后台请求级权限校验
This commit is contained in:
1 parent
00aeaa3cb1
commit
4cd3749806
3 files changed
+119
-17
No files matched your search
@@ -4,16 +4,36 @@ import { PrismaService } from "./prisma.service.js";
|
||||
import { readAdminToken } from "./admin-access.identity.js";
|
||||
import { readIdentity } from "./auth.js";
|
||||
|
||||
type TokenAccess = {
|
||||
subject: string;
|
||||
brandId: string;
|
||||
mustChangePassword: boolean;
|
||||
tokenVersion: number;
|
||||
platformTemplateRole: string;
|
||||
};
|
||||
|
||||
type BrandAccess = { subject: string; brandId: string; role: string };
|
||||
|
||||
@Injectable()
|
||||
export class AdminAccessService {
|
||||
/**
|
||||
* 同一 HTTP 请求内 Guard 和 Controller 都会调用访问校验。
|
||||
* 用 WeakMap 按请求缓存结果,既避免重复查询,也不会把权限状态跨请求缓存。
|
||||
*/
|
||||
private readonly tokenAccess = new WeakMap<FastifyRequest, Promise<TokenAccess>>();
|
||||
private readonly brandAccess = new WeakMap<FastifyRequest, Promise<BrandAccess>>();
|
||||
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async requireToken(request: FastifyRequest): Promise<{
|
||||
subject: string;
|
||||
brandId: string;
|
||||
mustChangePassword: boolean;
|
||||
tokenVersion: number;
|
||||
}> {
|
||||
requireToken(request: FastifyRequest): Promise<TokenAccess> {
|
||||
const cached = this.tokenAccess.get(request);
|
||||
if (cached) return cached;
|
||||
const pending = this.resolveToken(request);
|
||||
this.tokenAccess.set(request, pending);
|
||||
return pending;
|
||||
}
|
||||
|
||||
private async resolveToken(request: FastifyRequest): Promise<TokenAccess> {
|
||||
const token = readAdminToken(request);
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
let identity;
|
||||
@@ -25,7 +45,12 @@ export class AdminAccessService {
|
||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||
const user = await this.prisma.adminUser.findUnique({
|
||||
where: { id: identity.subject },
|
||||
select: { isEnabled: true, mustChangePassword: true, tokenVersion: true },
|
||||
select: {
|
||||
isEnabled: true,
|
||||
mustChangePassword: true,
|
||||
tokenVersion: true,
|
||||
platformTemplateRole: true,
|
||||
},
|
||||
});
|
||||
if (!user || identity.tokenVersion !== user.tokenVersion)
|
||||
throw new UnauthorizedException("管理员登录态已失效,请重新登录");
|
||||
@@ -35,12 +60,19 @@ export class AdminAccessService {
|
||||
brandId: identity.brandId,
|
||||
mustChangePassword: user.mustChangePassword,
|
||||
tokenVersion: user.tokenVersion,
|
||||
platformTemplateRole: user.platformTemplateRole,
|
||||
};
|
||||
}
|
||||
|
||||
async requireBrand(
|
||||
request: FastifyRequest,
|
||||
): Promise<{ subject: string; brandId: string; role: string }> {
|
||||
requireBrand(request: FastifyRequest): Promise<BrandAccess> {
|
||||
const cached = this.brandAccess.get(request);
|
||||
if (cached) return cached;
|
||||
const pending = this.resolveBrand(request);
|
||||
this.brandAccess.set(request, pending);
|
||||
return pending;
|
||||
}
|
||||
|
||||
private async resolveBrand(request: FastifyRequest): Promise<BrandAccess> {
|
||||
const access = await this.requireToken(request);
|
||||
if (access.mustChangePassword)
|
||||
throw new ForbiddenException("请先完成初始密码修改后再访问管理后台");
|
||||
@@ -63,11 +95,7 @@ export class AdminAccessService {
|
||||
const access = await this.requireToken(request);
|
||||
if (access.mustChangePassword)
|
||||
throw new ForbiddenException("请先完成初始密码修改后再访问管理后台");
|
||||
const user = await this.prisma.adminUser.findUniqueOrThrow({
|
||||
where: { id: access.subject },
|
||||
select: { platformTemplateRole: true },
|
||||
});
|
||||
if (user.platformTemplateRole !== "template_admin")
|
||||
if (access.platformTemplateRole !== "template_admin")
|
||||
throw new ForbiddenException("当前账号没有模板管理权限");
|
||||
return { subject: access.subject };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user