fix: 收口会话边界与 CI 环境

统一 Admin 会话解析、避免登出未处理异常,补充 admin/customer logout 与品牌切换契约文档;修正 CI integration 缺少数据库/会话环境的问题,并刷新 doctor source hash。
This commit is contained in:
que01 committed 2026-10-19 00:09:39 +08:00
1 parent 3017e3eb49
commit bd129a3069
7 files changed
+74 -70

No files matched your search

@@ -1,87 +1,54 @@
import {
Body,
Controller,
Delete,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} from "@nestjs/common";
import { Body, Controller, Delete, Get, Param, Patch, Post, Req } from "@nestjs/common";
import type { FastifyRequest } from "fastify";
import { readIdentity } from "../../auth.js";
import {
CreateTrustContentDto,
UpdateBrandDto,
UpdateBrandPresentationDto,
UpdateTrustContentDto,
} from "../../admin.dto.js";
import { AdminAccessService } from "../../admin-access.service.js";
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
import { requireAdminIdentity } from "../workspace/admin-access.identity.js";
import { AdminBrandConfigService } from "./admin-brand-config.service.js";
@Controller("admin/config")
export class AdminConfigController {
constructor(
private readonly access: AdminAccessService,
private readonly templateWorkspace: TemplateWorkspaceService,
private readonly brandConfig: AdminBrandConfigService,
) {}
private brandId(request: FastifyRequest): string {
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
if (!token) throw new UnauthorizedException("缺少管理员登录态");
const identity = readIdentity(token);
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
return identity.brandId;
}
private requireAdmin(request: FastifyRequest): void {
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
if (!token) throw new UnauthorizedException("缺少管理员登录态");
let identity;
try {
identity = readIdentity(token);
} catch {
throw new UnauthorizedException("管理员登录态无效或已过期");
}
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
}
@Get("brand")
getBrand(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request);
return this.brandConfig.getBrand(this.brandId(request));
const identity = requireAdminIdentity(request);
return this.brandConfig.getBrand(identity.brandId);
}
@Patch("brand")
async updateBrand(
@Req() request: FastifyRequest,
@Body() body: UpdateBrandDto,
): Promise<unknown> {
this.requireAdmin(request);
return this.brandConfig.updateBrand(this.brandId(request), body);
const identity = requireAdminIdentity(request);
return this.brandConfig.updateBrand(identity.brandId, body);
}
@Patch("presentation")
async updatePresentation(
@Req() request: FastifyRequest,
@Body() body: UpdateBrandPresentationDto,
): Promise<unknown> {
this.requireAdmin(request);
const brandId = this.brandId(request);
return this.brandConfig.updatePresentation(brandId, body);
const identity = requireAdminIdentity(request);
return this.brandConfig.updatePresentation(identity.brandId, body);
}
@Get("trust-content")
listTrustContent(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request);
return this.templateWorkspace.listTrustContent(this.brandId(request));
const identity = requireAdminIdentity(request);
return this.templateWorkspace.listTrustContent(identity.brandId);
}
@Post("trust-content")
createTrustContent(
@Req() request: FastifyRequest,
@Body() body: CreateTrustContentDto,
): Promise<unknown> {
this.requireAdmin(request);
const brandId = this.brandId(request);
return this.templateWorkspace.createTrustContent(brandId, body);
const identity = requireAdminIdentity(request);
return this.templateWorkspace.createTrustContent(identity.brandId, body);
}
@Patch("trust-content/:id")
updateTrustContent(
@@ -89,15 +56,15 @@ export class AdminConfigController {
@Param("id") id: string,
@Body() body: UpdateTrustContentDto,
): Promise<unknown> {
this.requireAdmin(request);
return this.templateWorkspace.updateTrustContent(this.brandId(request), id, body);
const identity = requireAdminIdentity(request);
return this.templateWorkspace.updateTrustContent(identity.brandId, id, body);
}
@Delete("trust-content/:id")
async deleteTrustContent(
@Req() request: FastifyRequest,
@Param("id") id: string,
): Promise<unknown> {
this.requireAdmin(request);
return this.templateWorkspace.deleteTrustContent(this.brandId(request), id);
const identity = requireAdminIdentity(request);
return this.templateWorkspace.deleteTrustContent(identity.brandId, id);
}
}
@@ -1,17 +1,7 @@
import {
Controller,
Body,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} from "@nestjs/common";
import { Body, Controller, Get, Param, Patch, Post, Req } from "@nestjs/common";
import type { FastifyRequest } from "fastify";
import { readIdentity } from "../../auth.js";
import { AdminAccessService } from "../../admin-access.service.js";
import { requireAdminIdentity } from "../workspace/admin-access.identity.js";
import { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
import { TenantService } from "./tenant.service.js";
@@ -66,10 +56,6 @@ export class TenantController {
}
private adminIdentity(request: FastifyRequest) {
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
if (!token) throw new UnauthorizedException("缺少管理员登录态");
const identity = readIdentity(token);
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
return identity;
return requireAdminIdentity(request);
}
}
@@ -6,10 +6,10 @@ import {
} from "@nestjs/common";
import bcrypt from "bcryptjs";
import { PrismaService } from "../../prisma.service.js";
import { readIdentity, signIdentity } from "../../auth.js";
import { signIdentity } from "../../auth.js";
import type { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
type AdminIdentity = ReturnType<typeof readIdentity>;
type AdminIdentity = { subject: string; brandId: string; tokenVersion?: number };
@Injectable()
export class TenantService {