fix: 收口依赖兼容与会话撤销
为 miniprogram-ci 和 Jimp 依赖增加兼容 patch 与 smoke test;修复平台模板管理员路由漏配;收紧模板契约校验;为 admin/customer 增加 token 撤销和集中会话存储;同步品牌切换角色、doctor source hash、多屏宽截图证据和 CI 构建门禁。
This commit is contained in:
1 parent
4f0b50c7f1
commit
3017e3eb49
65 files changed
+1623
-975
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { ForbiddenException, Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { PrismaService } from "../../prisma.service.js";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
|
||||
export type CustomerIdentity = {
|
||||
brandId: string;
|
||||
openid: string;
|
||||
customerUserId: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class CustomerAccessService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async require(request: FastifyRequest): Promise<CustomerIdentity> {
|
||||
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 !== "customer") throw new ForbiddenException("客户登录态无效或已过期");
|
||||
const customer = await this.prisma.customerUser.findUnique({
|
||||
where: { brandId_openid: { brandId: identity.brandId, openid: identity.subject } },
|
||||
select: { id: true, tokenVersion: true },
|
||||
});
|
||||
if (!customer || identity.tokenVersion !== customer.tokenVersion)
|
||||
throw new UnauthorizedException("客户登录态无效或已过期");
|
||||
return {
|
||||
brandId: identity.brandId,
|
||||
openid: identity.subject,
|
||||
customerUserId: customer.id,
|
||||
};
|
||||
}
|
||||
|
||||
async logout(request: FastifyRequest): Promise<void> {
|
||||
const identity = await this.require(request);
|
||||
await this.prisma.customerUser.update({
|
||||
where: { id: identity.customerUserId },
|
||||
data: { tokenVersion: { increment: 1 } },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,21 @@
|
||||
import { BadRequestException, Body, Controller, Get, Param, Post, Req } from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { BookingService } from "../../booking.service.js";
|
||||
import { CreateBookingDto } from "../../booking.dto.js";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
import { CustomerAccessService } from "./customer-access.service.js";
|
||||
|
||||
@Controller("customer/bookings")
|
||||
export class CustomerBookingController {
|
||||
constructor(private readonly bookings: BookingService) {}
|
||||
constructor(
|
||||
private readonly bookings: BookingService,
|
||||
private readonly access: CustomerAccessService,
|
||||
) {}
|
||||
|
||||
@Post()
|
||||
async create(@Req() request: FastifyRequest, @Body() body: CreateBookingDto): Promise<unknown> {
|
||||
const identity = this.identity(request);
|
||||
const customerId = await this.bookings.requireCustomer(identity);
|
||||
const identity = await this.identity(request);
|
||||
const customerId = identity.customerUserId;
|
||||
return this.bookings.create(identity.brandId, customerId, {
|
||||
...body,
|
||||
extraData: body.extraData as Prisma.InputJsonObject,
|
||||
@@ -21,28 +24,19 @@ export class CustomerBookingController {
|
||||
|
||||
@Get()
|
||||
async list(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const identity = this.identity(request);
|
||||
const customerId = await this.bookings.requireCustomer(identity);
|
||||
const identity = await this.identity(request);
|
||||
const customerId = identity.customerUserId;
|
||||
return this.bookings.listForCustomer(identity.brandId, customerId);
|
||||
}
|
||||
|
||||
@Post(":id/cancel")
|
||||
async cancel(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
const identity = this.identity(request);
|
||||
const customerId = await this.bookings.requireCustomer(identity);
|
||||
const identity = await this.identity(request);
|
||||
const customerId = identity.customerUserId;
|
||||
return this.bookings.cancelForCustomer(identity.brandId, customerId, id);
|
||||
}
|
||||
|
||||
private identity(request: FastifyRequest): { brandId: string; openid: string } {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new BadRequestException("缺少客户登录态");
|
||||
try {
|
||||
const identity = readIdentity(token);
|
||||
if (identity.role !== "customer") throw new BadRequestException("客户登录态无效");
|
||||
return { brandId: identity.brandId, openid: identity.subject };
|
||||
} catch (error) {
|
||||
if (error instanceof BadRequestException) throw error;
|
||||
throw new BadRequestException("客户登录态无效");
|
||||
}
|
||||
private identity(request: FastifyRequest) {
|
||||
return this.access.require(request);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,15 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
ForbiddenException,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Req,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { Body, Controller, Get, Param, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
import { CustomerAccessService } from "./customer-access.service.js";
|
||||
import { CreateInquirySubmissionDto } from "../../booking.dto.js";
|
||||
import { CustomerInquirySubmissionService } from "./customer-inquiry-submission.service.js";
|
||||
|
||||
@Controller("customer/inquiry-submissions")
|
||||
export class CustomerInquirySubmissionController {
|
||||
constructor(private readonly submissions: CustomerInquirySubmissionService) {}
|
||||
constructor(
|
||||
private readonly submissions: CustomerInquirySubmissionService,
|
||||
private readonly access: CustomerAccessService,
|
||||
) {}
|
||||
private present<T extends { customerSubject: string; adminNote?: string | null }>(submission: T) {
|
||||
const {
|
||||
customerSubject: _customerSubject,
|
||||
@@ -26,34 +20,21 @@ export class CustomerInquirySubmissionController {
|
||||
void _adminNote;
|
||||
return publicSubmission;
|
||||
}
|
||||
private identity(request: FastifyRequest): ReturnType<typeof readIdentity> {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少客户登录态");
|
||||
try {
|
||||
const identity = readIdentity(token);
|
||||
if (identity.role !== "customer") throw new ForbiddenException("无客户权限");
|
||||
return identity;
|
||||
} catch (error) {
|
||||
if (error instanceof ForbiddenException) throw error;
|
||||
throw new UnauthorizedException("客户登录态无效或已过期");
|
||||
}
|
||||
private identity(request: FastifyRequest) {
|
||||
return this.access.require(request);
|
||||
}
|
||||
|
||||
@Get()
|
||||
async list(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
const identity = this.identity(request);
|
||||
const submissions = await this.submissions.listForCustomer(identity.brandId, identity.subject);
|
||||
const identity = await this.identity(request);
|
||||
const submissions = await this.submissions.listForCustomer(identity.brandId, identity.openid);
|
||||
return submissions.map((submission) => this.present(submission));
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
async get(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
const identity = this.identity(request);
|
||||
const submission = await this.submissions.getForCustomer(
|
||||
identity.brandId,
|
||||
identity.subject,
|
||||
id,
|
||||
);
|
||||
const identity = await this.identity(request);
|
||||
const submission = await this.submissions.getForCustomer(identity.brandId, identity.openid, id);
|
||||
return this.present(submission);
|
||||
}
|
||||
|
||||
@@ -62,8 +43,8 @@ export class CustomerInquirySubmissionController {
|
||||
@Req() request: FastifyRequest,
|
||||
@Body() body: CreateInquirySubmissionDto,
|
||||
): Promise<unknown> {
|
||||
const identity = this.identity(request);
|
||||
const submission = await this.submissions.create(identity.brandId, identity.subject, body);
|
||||
const identity = await this.identity(request);
|
||||
const submission = await this.submissions.create(identity.brandId, identity.openid, body);
|
||||
return this.present(submission);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Controller, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { CustomerAccessService } from "./customer-access.service.js";
|
||||
|
||||
@Controller("customer/session")
|
||||
export class CustomerSessionController {
|
||||
constructor(private readonly access: CustomerAccessService) {}
|
||||
|
||||
@Post("logout")
|
||||
async logout(@Req() request: FastifyRequest): Promise<{ loggedOut: true }> {
|
||||
await this.access.logout(request);
|
||||
return { loggedOut: true };
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,16 @@ import { PublicModule } from "../public/public.module.js";
|
||||
import { CustomerBookingController } from "./customer-booking.controller.js";
|
||||
import { CustomerInquirySubmissionController } from "./customer-inquiry-submission.controller.js";
|
||||
import { CustomerInquirySubmissionService } from "./customer-inquiry-submission.service.js";
|
||||
import { CustomerAccessService } from "./customer-access.service.js";
|
||||
import { CustomerSessionController } from "./customer-session.controller.js";
|
||||
|
||||
@Module({
|
||||
imports: [InfrastructureModule, PublicModule, BookingModule],
|
||||
controllers: [CustomerInquirySubmissionController, CustomerBookingController],
|
||||
providers: [CustomerInquirySubmissionService],
|
||||
controllers: [
|
||||
CustomerInquirySubmissionController,
|
||||
CustomerBookingController,
|
||||
CustomerSessionController,
|
||||
],
|
||||
providers: [CustomerInquirySubmissionService, CustomerAccessService],
|
||||
})
|
||||
export class CustomerModule {}
|
||||
@@ -18,13 +18,18 @@ export class CustomerSessionService {
|
||||
select: { id: true },
|
||||
});
|
||||
if (!brand) throw new BadRequestException("品牌尚未初始化");
|
||||
await this.prisma.customerUser.upsert({
|
||||
const customer = await this.prisma.customerUser.upsert({
|
||||
where: { brandId_openid: { brandId: brand.id, openid: session.openid } },
|
||||
update: { lastLoginAt: new Date() },
|
||||
create: { brandId: brand.id, openid: session.openid },
|
||||
});
|
||||
return {
|
||||
token: signIdentity({ subject: session.openid, brandId: brand.id, role: "customer" }),
|
||||
token: signIdentity({
|
||||
subject: session.openid,
|
||||
brandId: brand.id,
|
||||
role: "customer",
|
||||
tokenVersion: customer.tokenVersion,
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,16 @@ export class AdminSessionController {
|
||||
return this.auth.login(body.username, body.password);
|
||||
}
|
||||
|
||||
@Post("logout")
|
||||
async logout(@Req() request: FastifyRequest): Promise<{ loggedOut: true }> {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
const identity = readIdentity(token);
|
||||
if (identity.role !== "admin") throw new UnauthorizedException("管理员登录态无效");
|
||||
await this.auth.logout(identity.subject);
|
||||
return { loggedOut: true };
|
||||
}
|
||||
|
||||
@Post("change-password")
|
||||
async changePassword(
|
||||
@Req() request: FastifyRequest,
|
||||
|
||||
@@ -60,7 +60,7 @@ export class TenantController {
|
||||
async switchBrand(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("brandId") brandId: string,
|
||||
): Promise<{ token: string; brand: unknown }> {
|
||||
): Promise<{ token: string; role: string; brand: unknown }> {
|
||||
const identity = this.adminIdentity(request);
|
||||
return this.tenants.switchBrand(identity, brandId);
|
||||
}
|
||||
|
||||
@@ -113,6 +113,7 @@ export class TenantService {
|
||||
role: "admin",
|
||||
tokenVersion: identity.tokenVersion ?? 0,
|
||||
}),
|
||||
role: membership.role,
|
||||
brand: membership.brand,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user