37 lines
979 B
TypeScript
37 lines
979 B
TypeScript
import {
|
|
Injectable,
|
|
CanActivate,
|
|
ExecutionContext,
|
|
ForbiddenException,
|
|
} from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { ROLES_KEY } from './roles.decorator';
|
|
|
|
@Injectable()
|
|
export class RolesGuard implements CanActivate {
|
|
constructor(private reflector: Reflector) {}
|
|
|
|
canActivate(context: ExecutionContext): boolean {
|
|
const requiredRoles = this.reflector.getAllAndOverride<string[]>(
|
|
ROLES_KEY,
|
|
[context.getHandler(), context.getClass()],
|
|
);
|
|
|
|
if (!requiredRoles || requiredRoles.length === 0) {
|
|
return true;
|
|
}
|
|
|
|
const { user } = context.switchToHttp().getRequest();
|
|
if (!user || !user.role) {
|
|
throw new ForbiddenException('شما دسترسی لازم برای این بخش را ندارید');
|
|
}
|
|
|
|
const hasRole = requiredRoles.includes(user.role);
|
|
if (!hasRole) {
|
|
throw new ForbiddenException('سطح دسترسی شما کافی نیست');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|