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,7 +1,7 @@
{
"schemaVersion": 1,
"scope": "模板完成证据登记,保留命令、日志与产物哈希",
"generatedAt": "2026-10-19T00:20:00+08:00",
"generatedAt": "2026-10-19T00:40:00+08:00",
"templates": {
"acheng-campus-food": {
"real-manifest-interaction": {
@@ -292,5 +292,5 @@
}
}
},
"sourceTreeHash": "6505db4083d33579d25dd3899fdb64af2dd427bb22b0ddf002400003c35fd721"
"sourceTreeHash": "ff1ad5d31af376ed6c2aa019533b168805bdf5cfaf306418b8327fb432dbeee3"
}
+22
View File
@@ -74,6 +74,9 @@ jobs:
--health-retries 12
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/personal_brand
DEFAULT_BRAND_ID: 00000000-0000-0000-0000-000000000001
SESSION_SECRET: ci-integration-session-secret-0123456789abcdef
SEED_ADMIN_PASSWORD: ci-integration-admin-password
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
@@ -114,6 +117,25 @@ jobs:
name: Browser regression
runs-on: ubuntu-latest
needs: [build, taro]
services:
postgres:
image: postgres:16-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: personal_brand
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U postgres -d personal_brand"
--health-interval 5s
--health-timeout 5s
--health-retries 12
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/personal_brand
DEFAULT_BRAND_ID: 00000000-0000-0000-0000-000000000001
SESSION_SECRET: ci-integration-session-secret-0123456789abcdef
SEED_ADMIN_PASSWORD: ci-integration-admin-password
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
+2
View File
@@ -25,6 +25,8 @@ export function Root() {
const logout = async () => {
try {
await createApiClient().logoutAdmin();
} catch {
message.warning("已退出本地会话;服务端撤销请求未完成");
} finally {
clearAdminSession();
setLoggedIn(false);
@@ -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 {
+27
View File
@@ -58,6 +58,17 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/TokenResponse"
/customer/session/logout:
post:
operationId: logoutCustomer
security: [bearerAuth: []]
responses:
"201":
description: Customer session revoked
content:
application/json:
schema:
$ref: "#/components/schemas/LoggedOut"
/customer/bookings:
get:
operationId: listCustomerBookings
@@ -116,6 +127,17 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/AdminTokenResponse"
/admin/session/logout:
post:
operationId: logoutAdmin
security: [bearerAuth: []]
responses:
"201":
description: Admin session revoked
content:
application/json:
schema:
$ref: "#/components/schemas/LoggedOut"
/admin/bookings:
get:
operationId: listAdminBookings
@@ -326,6 +348,11 @@ components:
required: [token]
properties:
token: { type: string }
LoggedOut:
type: object
required: [loggedOut]
properties:
loggedOut: { type: boolean, enum: [true] }
AdminTokenResponse:
allOf:
- $ref: "#/components/schemas/TokenResponse"