From bd129a3069a706648dc12b988546a65c45d04e48 Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 19 Oct 2026 00:09:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B6=E5=8F=A3=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E8=BE=B9=E7=95=8C=E4=B8=8E=20CI=20=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 统一 Admin 会话解析、避免登出未处理异常,补充 admin/customer logout 与品牌切换契约文档;修正 CI integration 缺少数据库/会话环境的问题,并刷新 doctor source hash。 --- .../raw/completion-evidence.json | 4 +- .github/workflows/ci.yml | 22 +++++++ apps/admin/src/app/Root.tsx | 2 + .../modules/admin/admin-config.controller.ts | 65 +++++-------------- .../src/modules/tenant/tenant.controller.ts | 20 +----- apps/api/src/modules/tenant/tenant.service.ts | 4 +- docs/openapi.yaml | 27 ++++++++ 7 files changed, 74 insertions(+), 70 deletions(-) diff --git a/.codex-tasks/template-audit-execution/raw/completion-evidence.json b/.codex-tasks/template-audit-execution/raw/completion-evidence.json index d3e5776..0ae6be9 100644 --- a/.codex-tasks/template-audit-execution/raw/completion-evidence.json +++ b/.codex-tasks/template-audit-execution/raw/completion-evidence.json @@ -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" } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fdaa63d..8178f01 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/apps/admin/src/app/Root.tsx b/apps/admin/src/app/Root.tsx index 41963da..d3a2b30 100644 --- a/apps/admin/src/app/Root.tsx +++ b/apps/admin/src/app/Root.tsx @@ -25,6 +25,8 @@ export function Root() { const logout = async () => { try { await createApiClient().logoutAdmin(); + } catch { + message.warning("已退出本地会话;服务端撤销请求未完成"); } finally { clearAdminSession(); setLoggedIn(false); diff --git a/apps/api/src/modules/admin/admin-config.controller.ts b/apps/api/src/modules/admin/admin-config.controller.ts index 1677e6c..78c7135 100644 --- a/apps/api/src/modules/admin/admin-config.controller.ts +++ b/apps/api/src/modules/admin/admin-config.controller.ts @@ -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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - this.requireAdmin(request); - return this.templateWorkspace.deleteTrustContent(this.brandId(request), id); + const identity = requireAdminIdentity(request); + return this.templateWorkspace.deleteTrustContent(identity.brandId, id); } } diff --git a/apps/api/src/modules/tenant/tenant.controller.ts b/apps/api/src/modules/tenant/tenant.controller.ts index 11635a8..9b74a83 100644 --- a/apps/api/src/modules/tenant/tenant.controller.ts +++ b/apps/api/src/modules/tenant/tenant.controller.ts @@ -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); } } diff --git a/apps/api/src/modules/tenant/tenant.service.ts b/apps/api/src/modules/tenant/tenant.service.ts index c2dc656..aca8501 100644 --- a/apps/api/src/modules/tenant/tenant.service.ts +++ b/apps/api/src/modules/tenant/tenant.service.ts @@ -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; +type AdminIdentity = { subject: string; brandId: string; tokenVersion?: number }; @Injectable() export class TenantService { diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 4a1841d..c60537f 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -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"