diff --git a/catalogos.sql b/catalogos.sql index d6e1d7f..1cb6a92 100644 --- a/catalogos.sql +++ b/catalogos.sql @@ -1,5 +1,5 @@ -INSERT INTO institucion (institucion, logo) VALUES ("Facultad de Arquitectura", "url"); INSERT INTO institucion (institucion, logo) VALUES ("FES Acatlán", "url"); +INSERT INTO institucion (institucion, logo) VALUES ("Facultad de Arquitectura", "url"); INSERT INTO carrera (carrera, id_institucion) VALUES ("Matemáticas Aplicadas y Computación", 2); INSERT INTO carrera (carrera, id_institucion) VALUES ("Arquitectura", 1); diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 09aec7a..4ccded8 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -9,7 +9,12 @@ export class AuthController { @Post('login_operador') loginOperador(@Body() body: AuthLoginOperadorDto) { - return this.authService.loginOperador(body.operador, body.password); + return this.authService.loginOperador( + Number(body.id_institucion), + Number(body.id_modulo), + body.operador, + body.password, + ); } @Post('login_usuario') diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 94020ab..490dc37 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -6,6 +6,7 @@ import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; import { BcryptModule } from '../bcrypt/bcrypt.module'; import { JwtStrategyService } from './strategy/jwt-strategy.service'; +import { ModuloModule } from '../modulo/modulo.module'; import { OperadorModule } from '../operador/operador.module'; import { UsuarioModule } from '../usuario/usuario.module'; @@ -22,6 +23,7 @@ import { UsuarioModule } from '../usuario/usuario.module'; }, }), BcryptModule, + ModuloModule, OperadorModule, UsuarioModule, ], diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index a7d4733..c6ae626 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,13 +1,21 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { + ConflictException, + Injectable, + UnauthorizedException, +} from '@nestjs/common'; import { JwtService } from '@nestjs/jwt'; import { BcryptService } from '../bcrypt/bcrypt.service'; +import { ModuloService } from '../modulo/modulo.service'; import { OperadorService } from '../operador/operador.service'; import { UsuarioService } from '../usuario/usuario.service'; +import { JwtPayload } from './dto/jwt-payload'; @Injectable() export class AuthService { constructor( private bcryptService: BcryptService, + private jwtService: JwtService, + private moduloService: ModuloService, private operadorService: OperadorService, private usuarioService: UsuarioService, ) {} @@ -25,14 +33,40 @@ export class AuthService { }); } - loginOperador(operador: string, password: string) { - return this.operadorService.findByOperador(operador).then((operador) => { - if (!this.bcryptService.comparar(password, operador.password)) - throw new UnauthorizedException( - 'Usuario y/o contraseña incorrectos, trata de nuevo.', - ); - /* Crear JWT y regresarlo */ - return operador; - }); + async loginOperador( + id_institucion: number, + id_modulo: number, + operador: string, + password: string, + ) { + const modulo = await this.moduloService.findById(id_modulo); + + return this.operadorService + .findByOperador(id_institucion, operador) + .then((operador) => { + if (!this.bcryptService.comparar(password, operador.password)) + throw new UnauthorizedException( + 'Usuario y/o contraseña incorrectos, trata de nuevo.', + ); + if (!operador.activo) + throw new UnauthorizedException( + 'Esta cuenta se encuentra desactivada.', + ); + if ( + modulo.institucion.id_institucion != + operador.institucion.id_institucion + ) + throw new ConflictException( + 'El módulo seleccionado no pertenece a la misma institución al la que pertenece el operador.', + ); + + const payload: JwtPayload = { + id_operador: operador.id_operador, + id_tipo_usuario: operador.tipoUsuario.id_tipo_usuario, + id_modulo: modulo.id_modulo, + }; + + return { operador, token: this.jwtService.sign(payload) }; + }); } } diff --git a/src/auth/dto/auth-login-operador.dto.ts b/src/auth/dto/auth-login-operador.dto.ts index b1123e2..7f523d0 100644 --- a/src/auth/dto/auth-login-operador.dto.ts +++ b/src/auth/dto/auth-login-operador.dto.ts @@ -1,7 +1,13 @@ -import { IsNumberString, IsString } from 'class-validator'; +import { IsInt, IsString } from 'class-validator'; export class AuthLoginOperadorDto { - @IsNumberString() + @IsInt() + id_institucion: string; + + @IsInt() + id_modulo: string; + + @IsString() operador: string; @IsString() diff --git a/src/auth/dto/jwt-payload.ts b/src/auth/dto/jwt-payload.ts new file mode 100644 index 0000000..3a1aaca --- /dev/null +++ b/src/auth/dto/jwt-payload.ts @@ -0,0 +1,9 @@ +export class JwtPayload { + id_tipo_usuario: number; + + id_usuario?: number; + + id_operador?: number; + + id_modulo?: number; +} diff --git a/src/modulo/modulo.module.ts b/src/modulo/modulo.module.ts index 868ee0f..3406016 100644 --- a/src/modulo/modulo.module.ts +++ b/src/modulo/modulo.module.ts @@ -9,5 +9,6 @@ import { InstitucionModule } from '../institucion/institucion.module'; imports: [TypeOrmModule.forFeature([Modulo]), InstitucionModule], controllers: [ModuloController], providers: [ModuloService], + exports: [ModuloService], }) export class ModuloModule {} diff --git a/src/operador/dto/operador.dto.ts b/src/operador/dto/operador.dto.ts index eebc66e..872d995 100644 --- a/src/operador/dto/operador.dto.ts +++ b/src/operador/dto/operador.dto.ts @@ -1,6 +1,6 @@ -import { IsString } from 'class-validator'; +import { IsNumberString } from 'class-validator'; export class OperadorDto { - @IsString() - operador: string; + @IsNumberString() + id_operador: string; } diff --git a/src/operador/entity/operador.entity.ts b/src/operador/entity/operador.entity.ts index e6f2af0..969f469 100644 --- a/src/operador/entity/operador.entity.ts +++ b/src/operador/entity/operador.entity.ts @@ -25,11 +25,15 @@ export class Operador { @Column({ type: String, nullable: false, length: 60 }) password: string; - @ManyToOne(() => Institucion, (institucion) => institucion.operadores) + @ManyToOne(() => Institucion, (institucion) => institucion.operadores, { + eager: true, + }) @JoinColumn({ name: 'id_institucion' }) institucion: Institucion; - @ManyToOne(() => TipoUsuario, (tipoUsuario) => tipoUsuario.operadores) + @ManyToOne(() => TipoUsuario, (tipoUsuario) => tipoUsuario.operadores, { + eager: true, + }) @JoinColumn({ name: 'id_tipo_usuario' }) tipoUsuario: TipoUsuario; diff --git a/src/operador/operador.controller.ts b/src/operador/operador.controller.ts index aaee20d..9fec5e1 100644 --- a/src/operador/operador.controller.ts +++ b/src/operador/operador.controller.ts @@ -19,7 +19,7 @@ export class OperadorController { @Get('operador') operador(@Query() query: OperadorDto) { - return this.operadorService.findByOperador(query.operador); + return this.operadorService.findById(Number(query.id_operador)); } @Get('operadores') diff --git a/src/operador/operador.service.ts b/src/operador/operador.service.ts index cbd78c9..d45dfbf 100644 --- a/src/operador/operador.service.ts +++ b/src/operador/operador.service.ts @@ -68,11 +68,14 @@ export class OperadorService { }); } - findByOperador(operador: string) { - return this.repository.findOne({ operador }).then((operador) => { - if (!operador) throw new NotFoundException('No existe este operador'); - return operador; - }); + findByOperador(id_institucion: number, operador: string) { + return this.institucionService + .findById(id_institucion) + .then((institucion) => this.repository.findOne({ institucion, operador })) + .then((operador) => { + if (!operador) throw new NotFoundException('No existe este operador.'); + return operador; + }); } update(attrs: Partial) {