refactor: 收口 Admin 会话与授权边界

This commit is contained in:
que01 committed 2026-10-19 10:13:13 +08:00
1 parent 0788480ce3
commit 4ef4be2071
67 files changed
+1890 -392

No files matched your search

+50 -11
View File
@@ -1,22 +1,30 @@
import { ForbiddenException, Injectable, UnauthorizedException } from "@nestjs/common";
import bcrypt from "bcryptjs";
import type { FastifyRequest } from "fastify";
import { AdminAccessService } from "./admin-access.service.js";
import { PrismaService } from "./prisma.service.js";
import { signIdentity } from "./auth.js";
export type AdminLoginSession = {
displayName: string;
role: string;
platformTemplateRole: "none" | "template_admin";
mustChangePassword: boolean;
};
export type AdminCurrentSession = AdminLoginSession & { authenticated: true };
@Injectable()
export class AdminAuthService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly access: AdminAccessService,
) {}
async login(
username: string,
password: string,
): Promise<{
token: string;
displayName: string;
mustChangePassword: boolean;
role: string;
platformTemplateRole: string;
}> {
): Promise<{ session: AdminLoginSession; token: string }> {
const user = await this.prisma.adminUser.findUnique({ where: { username } });
if (!user || !user.isEnabled || !(await bcrypt.compare(password, user.passwordHash))) {
throw new UnauthorizedException("账号或密码错误");
@@ -31,16 +39,37 @@ export class AdminAuthService {
});
if (!membership) throw new ForbiddenException("管理员没有品牌权限,请先联系平台负责人分配");
return {
session: {
displayName: user.displayName,
mustChangePassword: user.mustChangePassword,
role: membership.role,
platformTemplateRole: user.platformTemplateRole,
},
token: signIdentity({
subject: user.id,
brandId: membership.brandId,
role: "admin",
tokenVersion: user.tokenVersion,
}),
};
}
async currentSession(request: FastifyRequest): Promise<AdminCurrentSession> {
const access = await this.access.requireBrand(request);
const user = await this.prisma.adminUser.findUniqueOrThrow({
where: { id: access.subject },
select: {
displayName: true,
mustChangePassword: true,
platformTemplateRole: true,
},
});
return {
authenticated: true,
displayName: user.displayName,
mustChangePassword: user.mustChangePassword,
role: membership.role,
platformTemplateRole: user.platformTemplateRole,
role: access.role,
};
}
@@ -55,20 +84,30 @@ export class AdminAuthService {
async changePassword(
userId: string,
brandId: string,
currentPassword: string,
newPassword: string,
): Promise<void> {
): Promise<{ token: string }> {
const user = await this.prisma.adminUser.findUnique({ where: { id: userId } });
if (!user?.isEnabled || !(await bcrypt.compare(currentPassword, user.passwordHash))) {
throw new UnauthorizedException("当前密码不正确");
}
await this.prisma.adminUser.update({
const updated = await this.prisma.adminUser.update({
where: { id: userId },
data: {
passwordHash: await bcrypt.hash(newPassword, 12),
mustChangePassword: false,
tokenVersion: { increment: 1 },
},
select: { tokenVersion: true },
});
return {
token: signIdentity({
subject: user.id,
brandId,
role: "admin",
tokenVersion: updated.tokenVersion,
}),
};
}
}