feat: add TipoEvento entity and related functionality
- Introduced TipoEvento entity with enum for event types. - Created DTOs for creating and updating TipoEvento. - Implemented TipoEvento service with methods for CRUD operations and seeding initial data. - Added TipoEvento controller for handling HTTP requests related to event types. - Integrated TipoEvento into the main application module. - Updated Evento entity to include a relationship with TipoEvento. - Enhanced ParticipanteEvento service to register attendance using QR tokens. - Implemented QR token generation and validation for event attendance. - Updated API documentation for new endpoints and functionalities. - Adjusted TypeOrm configuration to include new entities.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { ROLES_KEY } from '../decorators/roles.decorator';
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(private reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const requiredRoles = this.reflector.get<string[]>(
|
||||
ROLES_KEY,
|
||||
context.getHandler(),
|
||||
);
|
||||
if (!requiredRoles) return true; // Si no hay roles definidos, permitir acceso.
|
||||
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const user = request.user; // Usuario ya validado en JwtValidationGuard
|
||||
|
||||
if (!user) {
|
||||
throw new ForbiddenException('Usuario no autenticado');
|
||||
}
|
||||
|
||||
// Obtener el tipo de usuario
|
||||
const tipo_usuario = user.tipoUser.tipo;
|
||||
|
||||
if (!tipo_usuario) {
|
||||
throw new ForbiddenException('No se pudo determinar el tipo de usuario');
|
||||
}
|
||||
|
||||
// Validar si el usuario tiene uno de los roles requeridos
|
||||
const hasRole = requiredRoles.includes(tipo_usuario);
|
||||
|
||||
if (!hasRole) {
|
||||
throw new ForbiddenException(
|
||||
'No tienes permisos para acceder a este recurso',
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user