diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index e124f86..1f4645d 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,4 +1,5 @@ -import { Body, Controller, Post } from '@nestjs/common'; +import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common'; +import { AuthGuard } from '@nestjs/passport'; import { ApiBody, ApiOperation, ApiTags } from '@nestjs/swagger'; import { Serealize } from '../interceptors/serialize.interceptor'; import { AuthService } from './auth.service'; @@ -70,4 +71,13 @@ export class AuthController { loginUsuario(@Body() body: LoginUsuarioDto) { return this.authService.loginUsuario(body.usuario, body.password); } + + @Get('validar-token') + @UseGuards(AuthGuard('jwt')) + @ApiOperation({ + description: 'Endpoint utilizado para validar un token.', + }) + validarToken() { + return true; + } } diff --git a/src/auth/strategy/jwt-strategy.service.ts b/src/auth/strategy/jwt-strategy.service.ts index d47796f..e5a6c6e 100644 --- a/src/auth/strategy/jwt-strategy.service.ts +++ b/src/auth/strategy/jwt-strategy.service.ts @@ -3,12 +3,11 @@ import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy, ExtractJwt } from 'passport-jwt'; import { ModuloService } from 'src/modulo/modulo.service'; -import { Operador } from 'src/operador/entity/operador.entity'; import { OperadorService } from 'src/operador/operador.service'; -import { Usuario } from 'src/usuario/entity/usuario.entity'; import { UsuarioService } from 'src/usuario/usuario.service'; import { Modulo } from '../../modulo/entity/modulo.entity'; - +import { Operador } from 'src/operador/entity/operador.entity'; +import { Usuario } from 'src/usuario/entity/usuario.entity'; import { JwtPayload } from '../dto/jwt-payload'; @Injectable() @@ -37,7 +36,8 @@ export class JwtStrategyService extends PassportStrategy(Strategy) { await this.operadorService .findById(payload.id_operador) .then(async (operador) => { - user.operador = operador; + if (!operador.activo) + throw new ConflictException('Esta cuenta esta desactivada.'); if (operador.tipoUsuario.id_tipo_usuario === 4) { if (payload.id_modulo) await this.moduloService @@ -48,6 +48,7 @@ export class JwtStrategyService extends PassportStrategy(Strategy) { 'Credenciales no válidas, inicia sesión de nuevo.', ); } + user.operador = operador; }); return user; } diff --git a/src/carrera-programa/carrera-programa.controller.ts b/src/carrera-programa/carrera-programa.controller.ts index e67ee25..1e18f80 100644 --- a/src/carrera-programa/carrera-programa.controller.ts +++ b/src/carrera-programa/carrera-programa.controller.ts @@ -1,10 +1,12 @@ import { Body, + ConflictException, Controller, Delete, Get, Post, Query, + Request, UseGuards, } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; @@ -17,6 +19,7 @@ import { } from '@nestjs/swagger'; import { Serealize } from '../interceptors/serialize.interceptor'; import { CarreraProgramaService } from './carrera-programa.service'; +import { Operador } from 'src/operador/entity/operador.entity'; import { IdInstitucionDto } from '../dto/id-institucion.dto'; import { CreateCarreraProgramaDto } from './dto/input/create.dto'; import { DeleteCarreraProgramaDto } from './dto/input/delete.dto'; @@ -40,8 +43,13 @@ export class CarreraProgramaController { ejemplo: { value: { id_institucion_carrera: 36, id_programa: 1 } }, }, }) - create(@Body() body: CreateCarreraProgramaDto) { + create(@Request() req, @Body() body: CreateCarreraProgramaDto) { + const admin: Operador = req.user.operador; + + if (admin.tipoUsuario.id_tipo_usuario != 3) + throw new ConflictException('No tienes permiso de realizar esta acción.'); return this.carreraProgramaService.create( + admin, body.id_institucion_carrera, body.id_programa, ); @@ -58,8 +66,12 @@ export class CarreraProgramaController { description: 'Es obligatorio mandar la variable id_carrera_programa.', examples: { ejemplo: { value: { id_carrera_programa: 1 } } }, }) - delete(@Body() body: DeleteCarreraProgramaDto) { - return this.carreraProgramaService.delete(body.id_carrera_programa); + delete(@Request() req, @Body() body: DeleteCarreraProgramaDto) { + const admin: Operador = req.user.operador; + + if (admin.tipoUsuario.id_tipo_usuario != 3) + throw new ConflictException('No tienes permiso de realizar esta acción.'); + return this.carreraProgramaService.delete(admin, body.id_carrera_programa); } @Serealize(CarreraProgramaOutputDto) diff --git a/src/carrera-programa/carrera-programa.service.ts b/src/carrera-programa/carrera-programa.service.ts index a6d5cae..5d8589a 100644 --- a/src/carrera-programa/carrera-programa.service.ts +++ b/src/carrera-programa/carrera-programa.service.ts @@ -6,6 +6,7 @@ import { import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CarreraPrograma } from './entity/carrera-programa.entity'; +import { Operador } from 'src/operador/entity/operador.entity'; import { InstitucionService } from '../institucion/institucion.service'; import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service'; import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service'; @@ -20,7 +21,11 @@ export class CarreraProgramaService { private institucionProgramaService: InstitucionProgramaService, ) {} - async create(id_institucion_carrera: number, id_programa: number) { + async create( + admin: Operador, + id_institucion_carrera: number, + id_programa: number, + ) { const institucionCarrera = await this.institucionCarreraService.findById( id_institucion_carrera, ); @@ -28,6 +33,13 @@ export class CarreraProgramaService { id_programa, ); + if ( + admin.institucion.id_institucion != + institucionCarrera.institucion.id_institucion + ) + throw new ConflictException( + 'Esta carrera no pertenece a tu institución.', + ); return this.repository .findOne({ institucionCarrera, programa }) .then((existeCarretaPrograma) => { @@ -47,9 +59,18 @@ export class CarreraProgramaService { })); } - delete(id_carrera_programa: number) { + delete(admin: Operador, id_carrera_programa: number) { return this.findById(id_carrera_programa) - .then((carreraPrograma) => this.repository.delete(carreraPrograma)) + .then((carreraPrograma) => { + if ( + admin.institucion.id_institucion != + carreraPrograma.institucionCarrera.institucion.id_institucion + ) + throw new ConflictException( + 'No puedes eliminar esta asociación porque no pertenece a tu institución.', + ); + return this.repository.delete(carreraPrograma); + }) .then((_) => ({ message: 'Se eliminó correctamente la asignación del programa.', }));