init to partner module
This commit is contained in:
@@ -11,7 +11,7 @@ export const ConsumerInfo = createParamDecorator(
|
||||
(data: keyof IConsumerPayload | undefined, ctx: ExecutionContext) => {
|
||||
try {
|
||||
const request = ctx.switchToHttp().getRequest<Request>()
|
||||
const info = request.consumerData
|
||||
const info = request.partnerData
|
||||
|
||||
if (!info) {
|
||||
throw new ForbiddenException('شما به این بخش دسترسی ندارید')
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
createParamDecorator,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common'
|
||||
import { Request } from 'express'
|
||||
import { IPartnerPayload } from '../models/partnerPayload.model'
|
||||
|
||||
export const PartnerInfo = createParamDecorator(
|
||||
(data: keyof IPartnerPayload | undefined, ctx: ExecutionContext) => {
|
||||
try {
|
||||
const request = ctx.switchToHttp().getRequest<Request>()
|
||||
const info = request.partnerData
|
||||
|
||||
if (!info) {
|
||||
throw new ForbiddenException('شما به این بخش دسترسی ندارید')
|
||||
}
|
||||
|
||||
if (data) {
|
||||
return info[data]
|
||||
}
|
||||
|
||||
return info
|
||||
} catch (err) {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
throw new BadRequestException('مشکلی در ساختار درخواست شما وجود دارد.')
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
// src/common/guards/permissions.guard.ts
|
||||
import { ITokenPayload } from '@/modules/auth/auth.utils'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import {
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common'
|
||||
import { Request as ExpressRequest } from 'express'
|
||||
|
||||
@Injectable()
|
||||
export class PartnerGuard {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async canActivate(
|
||||
tokenPayload: ITokenPayload,
|
||||
context: ExecutionContext,
|
||||
): Promise<boolean> {
|
||||
if (!tokenPayload || tokenPayload.type !== 'PARTNER') {
|
||||
throw new UnauthorizedException()
|
||||
}
|
||||
|
||||
const partner = await this.prisma.partner.findFirst({
|
||||
where: {
|
||||
partner_accounts: {
|
||||
some: {
|
||||
id: tokenPayload.account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
partner_accounts: {
|
||||
select: {
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!partner) {
|
||||
throw new ForbiddenException('شما دسترسی لازم را ندارید.')
|
||||
}
|
||||
|
||||
const req = context.switchToHttp().getRequest<ExpressRequest>()
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ import { ITokenPayload } from '@/modules/auth/auth.utils'
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common'
|
||||
@@ -12,6 +11,7 @@ import { Request as ExpressRequest } from 'express'
|
||||
import { IS_PUBLIC_KEY } from '../decorators/public.decorator'
|
||||
import { IS_PUBLIC_WITH_TOKEN_KEY } from '../decorators/withToken.decorator'
|
||||
import { ConsumerGuard } from './auth-consumer.guard'
|
||||
import { PartnerGuard } from './auth-partner.guard'
|
||||
import { PosGuard } from './auth-pos.guard'
|
||||
|
||||
@Injectable()
|
||||
@@ -21,6 +21,7 @@ export class JwtAuthGuard implements CanActivate {
|
||||
private reflector: Reflector,
|
||||
private consumerGuard: ConsumerGuard,
|
||||
private posGuard: PosGuard,
|
||||
private partnerGuard: PartnerGuard,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
@@ -59,14 +60,17 @@ export class JwtAuthGuard implements CanActivate {
|
||||
|
||||
const tokenPayload = this.jwt.decode(token) as ITokenPayload
|
||||
|
||||
if (req.url.startsWith('/api/v1/consumer')) {
|
||||
await this.consumerGuard.canActivate(tokenPayload, context)
|
||||
} else if (req.url.startsWith('/api/v1/pos')) {
|
||||
// await this.consumerGuard.canActivate(tokenPayload, context)
|
||||
await this.posGuard.canActivate(tokenPayload, context)
|
||||
} else if (tokenPayload.type != 'ADMIN') {
|
||||
throw new ForbiddenException('شما دسترسی لازم را ندارید')
|
||||
}
|
||||
// if (req.url.startsWith('/api/v1/partner')) {
|
||||
// await this.partnerGuard.canActivate(tokenPayload, context)
|
||||
// }
|
||||
// if (req.url.startsWith('/api/v1/consumer')) {
|
||||
// await this.consumerGuard.canActivate(tokenPayload, context)
|
||||
// } else if (req.url.startsWith('/api/v1/pos')) {
|
||||
// // await this.consumerGuard.canActivate(tokenPayload, context)
|
||||
// await this.posGuard.canActivate(tokenPayload, context)
|
||||
// } else if (tokenPayload.type != 'ADMIN') {
|
||||
// throw new ForbiddenException('شما دسترسی لازم را ندارید')
|
||||
// }
|
||||
return true
|
||||
} catch (err) {
|
||||
if (err) throw err
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface IPartnerPayload {
|
||||
id: string
|
||||
account_id: string
|
||||
}
|
||||
Reference in New Issue
Block a user