refactor: 收口 Admin 会话与授权边界
This commit is contained in:
1 parent
0788480ce3
commit
4ef4be2071
67 files changed
+1890
-392
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
@@ -31,6 +32,7 @@ class AdminBookingQueryDto {
|
||||
@IsOptional() @IsString() tagId?: string;
|
||||
}
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/bookings")
|
||||
export class AdminBookingController {
|
||||
constructor(private readonly bookings: BookingService) {}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import {
|
||||
@@ -10,6 +11,7 @@ import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminBrandConfigService } from "./admin-brand-config.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/config")
|
||||
export class AdminConfigController {
|
||||
constructor(
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import { Controller, Put, Query, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { AdminUploadService } from "./admin-upload.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/uploads")
|
||||
export class AdminLocalUploadController {
|
||||
constructor(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Get, Param, Patch, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
|
||||
type AdminIdentity = { subject: string; brandId: string };
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/miniapp-releases")
|
||||
export class AdminMiniAppReleaseController {
|
||||
constructor(
|
||||
@@ -34,13 +36,13 @@ export class AdminMiniAppReleaseController {
|
||||
return this.settingsService.get(identity.brandId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Patch("settings")
|
||||
async updateSettings(
|
||||
@Req() request: FastifyRequest,
|
||||
@Body() body: UpdateMiniAppSettingsDto & { privateKey?: string },
|
||||
@Body() body: UpdateMiniAppSettingsDto,
|
||||
): Promise<unknown> {
|
||||
const identity = await this.identity(request);
|
||||
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
|
||||
return this.settingsService.update(identity.brandId, body);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,5 +16,6 @@ export class UpdateMiniAppSettingsDto {
|
||||
build?: Record<string, unknown>;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
privateKey?: string;
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import { Controller, Get, Param, ParseIntPipe, Post, Body, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { BrandReleaseApiService } from "../../brand-release-api.service.js";
|
||||
import { PublishBrandReleaseDto } from "../../release.dto.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/releases")
|
||||
export class AdminReleaseController {
|
||||
constructor(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminTemplateAdmin } from "../../admin-auth.decorators.js";
|
||||
import { Controller, Get, Param, Post, Query, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
@@ -9,6 +10,7 @@ import { AdminTemplatePackageService } from "./admin-template-package.service.js
|
||||
|
||||
import { AdminTemplatePreviewService } from "./admin-template-preview.service.js";
|
||||
|
||||
@AdminTemplateAdmin()
|
||||
@Controller("admin/templates")
|
||||
export class AdminTemplatePackageController {
|
||||
constructor(
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Get, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { PresignUploadDto } from "./admin-upload.dto.js";
|
||||
import { AdminUploadService } from "./admin-upload.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/uploads")
|
||||
export class AdminUploadController {
|
||||
constructor(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminTemplateAdmin } from "../../admin-auth.decorators.js";
|
||||
import { Controller, Get, Query, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { Type } from "class-transformer";
|
||||
@@ -14,6 +15,7 @@ class TemplateOperationMetricsQueryDto {
|
||||
days = 30;
|
||||
}
|
||||
|
||||
@AdminTemplateAdmin()
|
||||
@Controller("admin/template-operations")
|
||||
export class TemplateOperationMetricsController {
|
||||
constructor(
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { Body, Controller, Post, Query, Req, UnauthorizedException } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { Body, Controller, Get, Post, Query, Req, Res } from "@nestjs/common";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { WechatAuthService } from "../../wechat-auth.js";
|
||||
import { AdminBrand, AdminPublic, AdminTokenOnly } from "../../admin-auth.decorators.js";
|
||||
import { clearAdminSessionCookie, setAdminSessionCookie } from "../../admin-session-cookie.js";
|
||||
import { AdminAuthService } from "../../admin-auth.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminLoginDto, WechatLoginDto } from "../../admin-auth.dto.js";
|
||||
import { CustomerSessionService } from "./customer-session.service.js";
|
||||
import { ChangeAdminPasswordDto } from "../../admin.dto.js";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
|
||||
@Controller("customer/session")
|
||||
export class CustomerSessionController {
|
||||
@@ -28,37 +30,55 @@ export class CustomerSessionController {
|
||||
export class AdminSessionController {
|
||||
constructor(private readonly auth: AdminAuthService) {}
|
||||
|
||||
@AdminPublic()
|
||||
@Post("login")
|
||||
login(@Body() body: AdminLoginDto): Promise<{
|
||||
token: string;
|
||||
async login(
|
||||
@Body() body: AdminLoginDto,
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{
|
||||
displayName: string;
|
||||
mustChangePassword: boolean;
|
||||
role: string;
|
||||
platformTemplateRole: string;
|
||||
platformTemplateRole: "none" | "template_admin";
|
||||
}> {
|
||||
return this.auth.login(body.username, body.password);
|
||||
const { session, token } = await this.auth.login(body.username, body.password);
|
||||
setAdminSessionCookie(reply, token);
|
||||
return session;
|
||||
}
|
||||
|
||||
@AdminBrand()
|
||||
@Get("current")
|
||||
async current(@Req() request: FastifyRequest) {
|
||||
return this.auth.currentSession(request);
|
||||
}
|
||||
|
||||
@AdminTokenOnly()
|
||||
@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("管理员登录态无效");
|
||||
async logout(
|
||||
@Req() request: FastifyRequest,
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{ loggedOut: true }> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
await this.auth.logout(identity.subject);
|
||||
clearAdminSessionCookie(reply);
|
||||
return { loggedOut: true };
|
||||
}
|
||||
|
||||
@AdminTokenOnly()
|
||||
@Post("change-password")
|
||||
async changePassword(
|
||||
@Req() request: FastifyRequest,
|
||||
@Body() body: ChangeAdminPasswordDto,
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{ changed: 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.changePassword(identity.subject, body.currentPassword, body.newPassword);
|
||||
const identity = requireAdminIdentity(request);
|
||||
const { token } = await this.auth.changePassword(
|
||||
identity.subject,
|
||||
identity.brandId,
|
||||
body.currentPassword,
|
||||
body.newPassword,
|
||||
);
|
||||
setAdminSessionCookie(reply, token);
|
||||
return { changed: true };
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,40 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "../../admin-access.service.js";
|
||||
import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Get, Param, Patch, Post, Req, Res } from "@nestjs/common";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { setAdminSessionCookie } from "../../admin-session-cookie.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { CreateBrandMemberDto, UpdateBrandMemberDto } from "../../admin.dto.js";
|
||||
import { TenantService } from "./tenant.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/brands")
|
||||
export class TenantController {
|
||||
constructor(
|
||||
private readonly access: AdminAccessService,
|
||||
private readonly tenants: TenantService,
|
||||
) {}
|
||||
constructor(private readonly tenants: TenantService) {}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@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);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@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);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@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);
|
||||
}
|
||||
@@ -50,9 +49,12 @@ export class TenantController {
|
||||
async switchBrand(
|
||||
@Req() request: FastifyRequest,
|
||||
@Param("brandId") brandId: string,
|
||||
): Promise<{ token: string; role: string; brand: unknown }> {
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{ role: string; brand: unknown }> {
|
||||
const identity = this.adminIdentity(request);
|
||||
return this.tenants.switchBrand(identity, brandId);
|
||||
const result = await this.tenants.switchBrand(identity, brandId);
|
||||
setAdminSessionCookie(reply, result.token);
|
||||
return { role: result.role, brand: result.brand };
|
||||
}
|
||||
|
||||
private adminIdentity(request: FastifyRequest) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Get, Param, Patch, Post, Query, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import {
|
||||
@@ -9,6 +10,7 @@ import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminSampleContentService } from "./admin-sample-content.service.js";
|
||||
import { AdminWorkspaceCapabilitiesService } from "./admin-workspace-capabilities.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/workspace")
|
||||
class AdminCapabilityController {
|
||||
constructor(
|
||||
@@ -16,6 +18,7 @@ class AdminCapabilityController {
|
||||
private readonly sampleContentService: AdminSampleContentService,
|
||||
) {}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Get("capabilities")
|
||||
list(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
return this.capabilitiesService.list(requireAdminIdentity(request).brandId);
|
||||
@@ -38,6 +41,7 @@ class AdminCapabilityController {
|
||||
return this.sampleContentService.import(identity.brandId, identity.subject, body);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Patch("capabilities/:key")
|
||||
update(
|
||||
@Req() request: FastifyRequest,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import {
|
||||
@@ -10,10 +11,12 @@ import {
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminInquiryService } from "./admin-inquiry.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/workspace")
|
||||
class AdminInquiryController {
|
||||
constructor(private readonly inquiryService: AdminInquiryService) {}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Post("fields")
|
||||
createField(
|
||||
@Req() request: FastifyRequest,
|
||||
@@ -22,6 +25,7 @@ class AdminInquiryController {
|
||||
return this.inquiryService.createField(requireAdminIdentity(request).brandId, body);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Patch("fields/:id")
|
||||
async updateField(
|
||||
@Req() request: FastifyRequest,
|
||||
@@ -31,6 +35,7 @@ class AdminInquiryController {
|
||||
return this.inquiryService.updateField(requireAdminIdentity(request).brandId, id, body);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Delete("fields/:id")
|
||||
async deleteField(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
return this.inquiryService.deleteField(requireAdminIdentity(request).brandId, id);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminBrand } from "../../admin-auth.decorators.js";
|
||||
import { CreateOfferingDto, UpdateOfferingDto } from "../../admin.dto.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminOfferingService } from "./admin-offering.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/workspace")
|
||||
class AdminOfferingController {
|
||||
constructor(private readonly offeringService: AdminOfferingService) {}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Body, Controller, Get, Post, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { RollbackTemplateDto, UpgradeTemplateDto } from "../../admin.dto.js";
|
||||
import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/workspace")
|
||||
class AdminTemplateController {
|
||||
constructor(private readonly templateWorkspace: TemplateWorkspaceService) {}
|
||||
@@ -13,6 +15,7 @@ class AdminTemplateController {
|
||||
return this.templateWorkspace.preview(requireAdminIdentity(request).brandId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Post("template/upgrade")
|
||||
async upgrade(
|
||||
@Req() request: FastifyRequest,
|
||||
@@ -23,6 +26,7 @@ class AdminTemplateController {
|
||||
return this.templateWorkspace.upgrade(identity.brandId, body, identity.subject, requestId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Post("template/rollback")
|
||||
async rollback(
|
||||
@Req() request: FastifyRequest,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { AdminBrand, AdminRoles } from "../../admin-auth.decorators.js";
|
||||
import { Controller, Get, Post, Body, Req } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { InstallTemplateDto, SelectTemplateVersionDto } from "../../admin.dto.js";
|
||||
@@ -5,6 +6,7 @@ import { TemplateWorkspaceService } from "../../template-workspace.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminWorkspaceService } from "./admin-workspace.service.js";
|
||||
|
||||
@AdminBrand()
|
||||
@Controller("admin/workspace")
|
||||
class AdminWorkspacePreviewController {
|
||||
constructor(
|
||||
@@ -22,6 +24,7 @@ class AdminWorkspacePreviewController {
|
||||
return this.workspaceService.getPreview(requireAdminIdentity(request).brandId);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Post("install")
|
||||
async install(
|
||||
@Req() request: FastifyRequest,
|
||||
@@ -38,6 +41,7 @@ class AdminWorkspacePreviewController {
|
||||
);
|
||||
}
|
||||
|
||||
@AdminRoles(["owner", "admin", "platform-admin"])
|
||||
@Post("template/select-version")
|
||||
async selectVersion(
|
||||
@Req() request: FastifyRequest,
|
||||
|
||||
Reference in new issue
Block a user