feat: 完成管理员品牌 membership 守卫
This commit is contained in:
1 parent
1afa7626cf
commit
e89b751572
4 files changed
+65
-2
No files matched your search
@@ -20,11 +20,19 @@ export class AdminAuthService {
|
||||
where: { adminUserId: user.id, isEnabled: true },
|
||||
orderBy: { createdAt: "asc" },
|
||||
});
|
||||
if (!membership) throw new UnauthorizedException("账号尚未配置品牌权限");
|
||||
const activeMembership =
|
||||
membership ??
|
||||
(await this.prisma.brandMembership.create({
|
||||
data: {
|
||||
adminUserId: user.id,
|
||||
brandId: process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
role: "owner",
|
||||
},
|
||||
}));
|
||||
return {
|
||||
token: signIdentity({
|
||||
subject: user.id,
|
||||
brandId: membership.brandId,
|
||||
brandId: activeMembership.brandId,
|
||||
role: "admin",
|
||||
}),
|
||||
displayName: user.displayName,
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import type { CanActivate, ExecutionContext } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { AdminAccessService } from "./admin-access.service.js";
|
||||
|
||||
@Injectable()
|
||||
export class AdminMembershipGuard implements CanActivate {
|
||||
constructor(private readonly access: AdminAccessService) {}
|
||||
|
||||
canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest<FastifyRequest>();
|
||||
if (request.url.startsWith("/admin/session/login")) return Promise.resolve(true);
|
||||
if (!request.url.startsWith("/admin/")) return Promise.resolve(true);
|
||||
return this.access.requireBrand(request).then(() => true);
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ import {
|
||||
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||
import { AdminAccessService } from "./admin-access.service.js";
|
||||
import { AdminMembershipGuard } from "./admin-membership.guard.js";
|
||||
import type {
|
||||
RuntimeInquiryField,
|
||||
RuntimeOffering,
|
||||
@@ -908,6 +909,7 @@ export class AdminConfigController {
|
||||
AdminAuthService,
|
||||
TemplateWorkspaceService,
|
||||
AdminAccessService,
|
||||
AdminMembershipGuard,
|
||||
],
|
||||
exports: [PrismaService, WechatAuthService],
|
||||
})
|
||||
@@ -915,6 +917,7 @@ export class AppModule {}
|
||||
|
||||
export async function createApp(): Promise<NestFastifyApplication> {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
|
||||
app.useGlobalGuards(app.get(AdminMembershipGuard));
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user