Files
persionalBrand/apps/api/src/modules/admin/admin-config.controller.ts
T
que01 4f0b50c7f1 fix: 收口模板契约与安全门禁
统一模板包 visual 契约并补齐完成证据;修复 release 版本一致性、管理员 token 撤销、CORS 与默认品牌配置;收敛依赖安全风险并完成全量回归验证。
2026-10-18 22:05:31 +08:00

104 lines
3.4 KiB
TypeScript

import {
Body,
Controller,
Delete,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} 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 { 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));
}
@Patch("brand")
async updateBrand(
@Req() request: FastifyRequest,
@Body() body: UpdateBrandDto,
): Promise<unknown> {
this.requireAdmin(request);
return this.brandConfig.updateBrand(this.brandId(request), 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);
}
@Get("trust-content")
listTrustContent(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request);
return this.templateWorkspace.listTrustContent(this.brandId(request));
}
@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);
}
@Patch("trust-content/:id")
updateTrustContent(
@Req() request: FastifyRequest,
@Param("id") id: string,
@Body() body: UpdateTrustContentDto,
): Promise<unknown> {
this.requireAdmin(request);
return this.templateWorkspace.updateTrustContent(this.brandId(request), 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);
}
}