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, "schemaVersion": 1,
"scope": "模板完成证据登记,保留命令、日志与产物哈希", "scope": "模板完成证据登记,保留命令、日志与产物哈希",
"generatedAt": "2026-10-19T00:20:00+08:00", "generatedAt": "2026-10-19T00:40:00+08:00",
"templates": { "templates": {
"acheng-campus-food": { "acheng-campus-food": {
"real-manifest-interaction": { "real-manifest-interaction": {
@@ -292,5 +292,5 @@
} }
} }
}, },
"sourceTreeHash": "6505db4083d33579d25dd3899fdb64af2dd427bb22b0ddf002400003c35fd721" "sourceTreeHash": "ff1ad5d31af376ed6c2aa019533b168805bdf5cfaf306418b8327fb432dbeee3"
} }
+22
View File
@@ -74,6 +74,9 @@ jobs:
--health-retries 12 --health-retries 12
env: env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/personal_brand 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: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: pnpm/action-setup@v4 - uses: pnpm/action-setup@v4
@@ -114,6 +117,25 @@ jobs:
name: Browser regression name: Browser regression
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: [build, taro] 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: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: pnpm/action-setup@v4 - uses: pnpm/action-setup@v4
+2
View File
@@ -25,6 +25,8 @@ export function Root() {
const logout = async () => { const logout = async () => {
try { try {
await createApiClient().logoutAdmin(); await createApiClient().logoutAdmin();
} catch {
message.warning("已退出本地会话;服务端撤销请求未完成");
} finally { } finally {
clearAdminSession(); clearAdminSession();
setLoggedIn(false); setLoggedIn(false);
@@ -1,87 +1,54 @@
import { import { Body, Controller, Delete, Get, Param, Patch, Post, Req } from "@nestjs/common";
Body,
Controller,
Delete,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} from "@nestjs/common";
import type { FastifyRequest } from "fastify"; import type { FastifyRequest } from "fastify";
import { readIdentity } from "../../auth.js";
import { import {
CreateTrustContentDto, CreateTrustContentDto,
UpdateBrandDto, UpdateBrandDto,
UpdateBrandPresentationDto, UpdateBrandPresentationDto,
UpdateTrustContentDto, UpdateTrustContentDto,
} from "../../admin.dto.js"; } from "../../admin.dto.js";
import { AdminAccessService } from "../../admin-access.service.js";
import { TemplateWorkspaceService } from "../../template-workspace.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"; import { AdminBrandConfigService } from "./admin-brand-config.service.js";
@Controller("admin/config") @Controller("admin/config")
export class AdminConfigController { export class AdminConfigController {
constructor( constructor(
private readonly access: AdminAccessService,
private readonly templateWorkspace: TemplateWorkspaceService, private readonly templateWorkspace: TemplateWorkspaceService,
private readonly brandConfig: AdminBrandConfigService, 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") @Get("brand")
getBrand(@Req() request: FastifyRequest): Promise<unknown> { getBrand(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
return this.brandConfig.getBrand(this.brandId(request)); return this.brandConfig.getBrand(identity.brandId);
} }
@Patch("brand") @Patch("brand")
async updateBrand( async updateBrand(
@Req() request: FastifyRequest, @Req() request: FastifyRequest,
@Body() body: UpdateBrandDto, @Body() body: UpdateBrandDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
return this.brandConfig.updateBrand(this.brandId(request), body); return this.brandConfig.updateBrand(identity.brandId, body);
} }
@Patch("presentation") @Patch("presentation")
async updatePresentation( async updatePresentation(
@Req() request: FastifyRequest, @Req() request: FastifyRequest,
@Body() body: UpdateBrandPresentationDto, @Body() body: UpdateBrandPresentationDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
const brandId = this.brandId(request); return this.brandConfig.updatePresentation(identity.brandId, body);
return this.brandConfig.updatePresentation(brandId, body);
} }
@Get("trust-content") @Get("trust-content")
listTrustContent(@Req() request: FastifyRequest): Promise<unknown> { listTrustContent(@Req() request: FastifyRequest): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
return this.templateWorkspace.listTrustContent(this.brandId(request)); return this.templateWorkspace.listTrustContent(identity.brandId);
} }
@Post("trust-content") @Post("trust-content")
createTrustContent( createTrustContent(
@Req() request: FastifyRequest, @Req() request: FastifyRequest,
@Body() body: CreateTrustContentDto, @Body() body: CreateTrustContentDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
const brandId = this.brandId(request); return this.templateWorkspace.createTrustContent(identity.brandId, body);
return this.templateWorkspace.createTrustContent(brandId, body);
} }
@Patch("trust-content/:id") @Patch("trust-content/:id")
updateTrustContent( updateTrustContent(
@@ -89,15 +56,15 @@ export class AdminConfigController {
@Param("id") id: string, @Param("id") id: string,
@Body() body: UpdateTrustContentDto, @Body() body: UpdateTrustContentDto,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
return this.templateWorkspace.updateTrustContent(this.brandId(request), id, body); return this.templateWorkspace.updateTrustContent(identity.brandId, id, body);
} }
@Delete("trust-content/:id") @Delete("trust-content/:id")
async deleteTrustContent( async deleteTrustContent(
@Req() request: FastifyRequest, @Req() request: FastifyRequest,
@Param("id") id: string, @Param("id") id: string,
): Promise<unknown> { ): Promise<unknown> {
this.requireAdmin(request); const identity = requireAdminIdentity(request);
return this.templateWorkspace.deleteTrustContent(this.brandId(request), id); return this.templateWorkspace.deleteTrustContent(identity.brandId, id);
} }
} }
@@ -1,17 +1,7 @@
import { import { Body, Controller, Get, Param, Patch, Post, Req } from "@nestjs/common";
Controller,
Body,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} from "@nestjs/common";
import type { FastifyRequest } from "fastify"; import type { FastifyRequest } from "fastify";
import { readIdentity } from "../../auth.js";
import { AdminAccessService } from "../../admin-access.service.js"; import { AdminAccessService } from "../../admin-access.service.js";
import { requireAdminIdentity } from "../workspace/admin-access.identity.js";
import { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js"; import { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
import { TenantService } from "./tenant.service.js"; import { TenantService } from "./tenant.service.js";
@@ -66,10 +56,6 @@ export class TenantController {
} }
private adminIdentity(request: FastifyRequest) { private adminIdentity(request: FastifyRequest) {
const token = request.headers.authorization?.replace(/^Bearer\s+/i, ""); return requireAdminIdentity(request);
if (!token) throw new UnauthorizedException("缺少管理员登录态");
const identity = readIdentity(token);
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
return identity;
} }
} }
@@ -6,10 +6,10 @@ import {
} from "@nestjs/common"; } from "@nestjs/common";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import { PrismaService } from "../../prisma.service.js"; 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"; import type { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
type AdminIdentity = ReturnType<typeof readIdentity>; type AdminIdentity = { subject: string; brandId: string; tokenVersion?: number };
@Injectable() @Injectable()
export class TenantService { export class TenantService {
+27
View File
@@ -58,6 +58,17 @@ paths:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/TokenResponse" $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: /customer/bookings:
get: get:
operationId: listCustomerBookings operationId: listCustomerBookings
@@ -116,6 +127,17 @@ paths:
application/json: application/json:
schema: schema:
$ref: "#/components/schemas/AdminTokenResponse" $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: /admin/bookings:
get: get:
operationId: listAdminBookings operationId: listAdminBookings
@@ -326,6 +348,11 @@ components:
required: [token] required: [token]
properties: properties:
token: { type: string } token: { type: string }
LoggedOut:
type: object
required: [loggedOut]
properties:
loggedOut: { type: boolean, enum: [true] }
AdminTokenResponse: AdminTokenResponse:
allOf: allOf:
- $ref: "#/components/schemas/TokenResponse" - $ref: "#/components/schemas/TokenResponse"