feat: 增加管理员品牌权限校验服务
This commit is contained in:
1 parent
d5e1543e60
commit
1afa7626cf
2 files changed
+48
-42
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { ForbiddenException, Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { PrismaService } from "./prisma.service.js";
|
||||
import { readIdentity } from "./auth.js";
|
||||
|
||||
@Injectable()
|
||||
export class AdminAccessService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async requireBrand(
|
||||
request: FastifyRequest,
|
||||
): Promise<{ subject: string; brandId: string; role: string }> {
|
||||
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("无管理员权限");
|
||||
const membership = await this.prisma.brandMembership.findUnique({
|
||||
where: { adminUserId_brandId: { adminUserId: identity.subject, brandId: identity.brandId } },
|
||||
});
|
||||
if (!membership?.isEnabled) throw new ForbiddenException("管理员没有当前品牌权限");
|
||||
return { subject: identity.subject, brandId: identity.brandId, role: membership.role };
|
||||
}
|
||||
}
|
||||
+20
-42
@@ -49,6 +49,7 @@ import {
|
||||
} from "@personal-brand/config";
|
||||
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||
import { AdminAccessService } from "./admin-access.service.js";
|
||||
import type {
|
||||
RuntimeInquiryField,
|
||||
RuntimeOffering,
|
||||
@@ -591,28 +592,20 @@ export class AdminBookingController {
|
||||
constructor(private readonly bookings: BookingService) {}
|
||||
@Get()
|
||||
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.listForAdmin(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
query,
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.listForAdmin(brandId, query);
|
||||
}
|
||||
|
||||
@Get("unread-count")
|
||||
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.unreadCountForAdmin(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.unreadCountForAdmin(brandId);
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.getForAdmin(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
id,
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.getForAdmin(brandId, id);
|
||||
}
|
||||
@Post(":id/status")
|
||||
update(
|
||||
@@ -621,22 +614,14 @@ export class AdminBookingController {
|
||||
@Body()
|
||||
body: UpdateBookingStatusDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.updateStatus(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
id,
|
||||
body.status,
|
||||
body.adminNote,
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.updateStatus(brandId, id, body.status, body.adminNote);
|
||||
}
|
||||
|
||||
@Post(":id/read")
|
||||
markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.markRead(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
id,
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.markRead(brandId, id);
|
||||
}
|
||||
|
||||
@Post(":id/tags")
|
||||
@@ -645,32 +630,23 @@ export class AdminBookingController {
|
||||
@Param("id") id: string,
|
||||
@Body() body: UpdateBookingTagsDto,
|
||||
): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.updateTags(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
id,
|
||||
body.tagIds,
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.updateTags(brandId, id, body.tagIds);
|
||||
}
|
||||
|
||||
@Get("/tags")
|
||||
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.listTags(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.listTags(brandId);
|
||||
}
|
||||
|
||||
@Post("/tags")
|
||||
createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
||||
this.requireAdmin(request);
|
||||
return this.bookings.createTag(
|
||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
||||
body.name,
|
||||
);
|
||||
const brandId = this.requireAdmin(request);
|
||||
return this.bookings.createTag(brandId, body.name);
|
||||
}
|
||||
|
||||
private requireAdmin(request: FastifyRequest): void {
|
||||
private requireAdmin(request: FastifyRequest): string {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
let identity;
|
||||
@@ -680,6 +656,7 @@ export class AdminBookingController {
|
||||
throw new UnauthorizedException("管理员登录态无效或已过期");
|
||||
}
|
||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||
return identity.brandId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -930,6 +907,7 @@ export class AdminConfigController {
|
||||
BookingService,
|
||||
AdminAuthService,
|
||||
TemplateWorkspaceService,
|
||||
AdminAccessService,
|
||||
],
|
||||
exports: [PrismaService, WechatAuthService],
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user