34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import type { Session } from 'next-auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { resolveBrandCenterAccessForUser } from '@/lib/brand-center-access'
|
|
|
|
export class BrandCenterDashboardAccessError extends Error {
|
|
status: number
|
|
|
|
constructor(message: string, status: number) {
|
|
super(message)
|
|
this.name = 'BrandCenterDashboardAccessError'
|
|
this.status = status
|
|
}
|
|
}
|
|
|
|
/** Shared API gate for dashboard metrics that expose cross-project aggregates. */
|
|
export async function assertBrandCenterDashboardAccess(session: Session | null) {
|
|
const userId = session?.user?.id
|
|
if (!userId) throw new BrandCenterDashboardAccessError('请先登录', 401)
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: userId },
|
|
select: { role: true, isSuperAdmin: true, isActive: true, status: true },
|
|
})
|
|
if (!user || !user.isActive || user.status === 'archived' || user.status === 'suspended') {
|
|
throw new BrandCenterDashboardAccessError('账号不可用,请重新登录', 401)
|
|
}
|
|
|
|
const allowed = await resolveBrandCenterAccessForUser(userId, {
|
|
role: user.role,
|
|
isSuperAdmin: user.isSuperAdmin,
|
|
})
|
|
if (!allowed) throw new BrandCenterDashboardAccessError('无权查看品牌中心流程指标', 403)
|
|
}
|