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";
|
} from "@personal-brand/config";
|
||||||
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
import { AdminWorkspaceController } from "./admin-workspace.controller.js";
|
||||||
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
import { TemplateWorkspaceService } from "./template-workspace.service.js";
|
||||||
|
import { AdminAccessService } from "./admin-access.service.js";
|
||||||
import type {
|
import type {
|
||||||
RuntimeInquiryField,
|
RuntimeInquiryField,
|
||||||
RuntimeOffering,
|
RuntimeOffering,
|
||||||
@@ -591,28 +592,20 @@ export class AdminBookingController {
|
|||||||
constructor(private readonly bookings: BookingService) {}
|
constructor(private readonly bookings: BookingService) {}
|
||||||
@Get()
|
@Get()
|
||||||
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
list(@Req() request: FastifyRequest, @Query() query: AdminBookingQueryDto): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.listForAdmin(
|
return this.bookings.listForAdmin(brandId, query);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
query,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("unread-count")
|
@Get("unread-count")
|
||||||
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
unreadCount(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.unreadCountForAdmin(
|
return this.bookings.unreadCountForAdmin(brandId);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(":id")
|
@Get(":id")
|
||||||
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
detail(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.getForAdmin(
|
return this.bookings.getForAdmin(brandId, id);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
id,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
@Post(":id/status")
|
@Post(":id/status")
|
||||||
update(
|
update(
|
||||||
@@ -621,22 +614,14 @@ export class AdminBookingController {
|
|||||||
@Body()
|
@Body()
|
||||||
body: UpdateBookingStatusDto,
|
body: UpdateBookingStatusDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.updateStatus(
|
return this.bookings.updateStatus(brandId, id, body.status, body.adminNote);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
id,
|
|
||||||
body.status,
|
|
||||||
body.adminNote,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post(":id/read")
|
@Post(":id/read")
|
||||||
markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
markRead(@Req() request: FastifyRequest, @Param("id") id: string): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.markRead(
|
return this.bookings.markRead(brandId, id);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
id,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post(":id/tags")
|
@Post(":id/tags")
|
||||||
@@ -645,32 +630,23 @@ export class AdminBookingController {
|
|||||||
@Param("id") id: string,
|
@Param("id") id: string,
|
||||||
@Body() body: UpdateBookingTagsDto,
|
@Body() body: UpdateBookingTagsDto,
|
||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.updateTags(
|
return this.bookings.updateTags(brandId, id, body.tagIds);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
id,
|
|
||||||
body.tagIds,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/tags")
|
@Get("/tags")
|
||||||
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
listTags(@Req() request: FastifyRequest): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.listTags(
|
return this.bookings.listTags(brandId);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("/tags")
|
@Post("/tags")
|
||||||
createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
createTag(@Req() request: FastifyRequest, @Body() body: CreateTagDto): Promise<unknown> {
|
||||||
this.requireAdmin(request);
|
const brandId = this.requireAdmin(request);
|
||||||
return this.bookings.createTag(
|
return this.bookings.createTag(brandId, body.name);
|
||||||
process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001",
|
|
||||||
body.name,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private requireAdmin(request: FastifyRequest): void {
|
private requireAdmin(request: FastifyRequest): string {
|
||||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||||
let identity;
|
let identity;
|
||||||
@@ -680,6 +656,7 @@ export class AdminBookingController {
|
|||||||
throw new UnauthorizedException("管理员登录态无效或已过期");
|
throw new UnauthorizedException("管理员登录态无效或已过期");
|
||||||
}
|
}
|
||||||
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
||||||
|
return identity.brandId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -930,6 +907,7 @@ export class AdminConfigController {
|
|||||||
BookingService,
|
BookingService,
|
||||||
AdminAuthService,
|
AdminAuthService,
|
||||||
TemplateWorkspaceService,
|
TemplateWorkspaceService,
|
||||||
|
AdminAccessService,
|
||||||
],
|
],
|
||||||
exports: [PrismaService, WechatAuthService],
|
exports: [PrismaService, WechatAuthService],
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user