17 lines
770 B
TypeScript
17 lines
770 B
TypeScript
import { type CanActivate, type ExecutionContext, ForbiddenException, Injectable } from '@nestjs/common';
|
|||
|
|
import type { AuthenticatedRequest } from '../auth/auth.types.js';
|
||
|
|
import { PermissionsService } from './permissions.service.js';
|
||
|
|
|
||
|
|
/** Solo usuarios con acceso total ("*"). Se ejecuta después del guard de sesión global. */
|
||
|
|
@Injectable()
|
||
|
|
export class AdminGuard implements CanActivate {
|
||
|
|
constructor(private readonly permissions: PermissionsService) {}
|
||
|
|
|
||
|
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||
|
|
const req = context.switchToHttp().getRequest<AuthenticatedRequest>();
|
||
|
|
if (!(await this.permissions.isAdmin(req.user.webUser))) {
|
||
|
|
throw new ForbiddenException('Requiere permisos de administrador');
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|