Files
persionalBrand/apps/api/src/modules/tenant/tenant.controller.ts
T
que01 3017e3eb49 fix: 收口依赖兼容与会话撤销
为 miniprogram-ci 和 Jimp 依赖增加兼容 patch 与 smoke test;修复平台模板管理员路由漏配;收紧模板契约校验;为 admin/customer 增加 token 撤销和集中会话存储;同步品牌切换角色、doctor source hash、多屏宽截图证据和 CI 构建门禁。
2026-10-18 23:43:25 +08:00

76 lines
2.4 KiB
TypeScript

import {
Controller,
Body,
ForbiddenException,
Get,
Param,
Patch,
Post,
Req,
UnauthorizedException,
} from "@nestjs/common";
import type { FastifyRequest } from "fastify";
import { readIdentity } from "../../auth.js";
import { AdminAccessService } from "../../admin-access.service.js";
import { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
import { TenantService } from "./tenant.service.js";
@Controller("admin/brands")
export class TenantController {
constructor(
private readonly access: AdminAccessService,
private readonly tenants: TenantService,
) {}
@Get("members")
async listMembers(@Req() request: FastifyRequest): Promise<unknown> {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
const current = this.adminIdentity(request);
return this.tenants.listMembers(current.brandId);
}
@Post("members")
async createMember(
@Req() request: FastifyRequest,
@Body() body: CreateBrandMemberDto,
): Promise<unknown> {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
const actor = this.adminIdentity(request);
return this.tenants.createMember(actor.brandId, body);
}
@Patch("members/:id")
async updateMember(
@Req() request: FastifyRequest,
@Param("id") id: string,
@Body() body: UpdateBrandMemberDto,
): Promise<unknown> {
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
const actor = this.adminIdentity(request);
return this.tenants.updateMember(actor.brandId, actor.subject, id, body);
}
@Get()
async list(@Req() request: FastifyRequest): Promise<unknown> {
const identity = this.adminIdentity(request);
return this.tenants.listMemberships(identity.subject);
}
@Post(":brandId/switch")
async switchBrand(
@Req() request: FastifyRequest,
@Param("brandId") brandId: string,
): Promise<{ token: string; role: string; brand: unknown }> {
const identity = this.adminIdentity(request);
return this.tenants.switchBrand(identity, brandId);
}
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;
}
}