Files
kooltab-backoffice/apps/api/src/common/guards/roles.guard.ts
T
brammie15 5c604e0851
CI / build-and-test (push) Canceled after 0s
feat: add image support to the products
2026-10-21 12:45:19 +02:00

25 lines
806 B
TypeScript

import { Injectable, ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { ROLES_KEY } from '../decorators/roles.decorator';
@Injectable()
export class RolesGuard {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<string[]>(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
if (!requiredRoles) {
return true;
}
const { user } = context.switchToHttp().getRequest();
if (!user) {
return false;
}
// "viewer" was the old name of a manager; tokens issued before the rename still carry it
const role = user.role === 'viewer' ? 'manager' : user.role;
return requiredRoles.includes(role);
}
}