33 lines
970 B
TypeScript
33 lines
970 B
TypeScript
import {
|
|
CanActivate,
|
|
ExecutionContext,
|
|
ForbiddenException,
|
|
Injectable,
|
|
} from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.module';
|
|
import { AuthUser, JwtAuthGuard } from './jwt-auth.guard';
|
|
|
|
@Injectable()
|
|
export class PartnerPrimaryGuard implements CanActivate {
|
|
constructor(
|
|
private readonly jwtAuthGuard: JwtAuthGuard,
|
|
private readonly prisma: PrismaService,
|
|
) {}
|
|
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
this.jwtAuthGuard.canActivate(context);
|
|
const req = context.switchToHttp().getRequest();
|
|
const user = req.user as AuthUser;
|
|
if (user.actorType !== 'PARTNER') {
|
|
throw new ForbiddenException('仅合伙人主账号可操作');
|
|
}
|
|
const account = await this.prisma.partnerAccount.findUniqueOrThrow({
|
|
where: { id: user.actorId },
|
|
});
|
|
if (account.isPrimary !== 1) {
|
|
throw new ForbiddenException('仅主账号可操作');
|
|
}
|
|
return true;
|
|
}
|
|
}
|