Merge branch 'Lino' of https://repositorio.acatlan.unam.mx/mcervantesag/api_directorio into Sam
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query,UseGuards } from '@nestjs/common';
|
||||
import { ProfesorService } from './profesor.service';
|
||||
import { profesorDto } from './dto/profesorDto.dto';
|
||||
import {Roles} from '../permissions/roles.decorator'
|
||||
import {RolesGuard} from '../permissions/roles.guard'
|
||||
import {Role} from '../permissions/role.enum'
|
||||
|
||||
@Controller('profesor')
|
||||
@UseGuards(RolesGuard)
|
||||
export class ProfesorController {
|
||||
constructor(private profesorService: ProfesorService) {}
|
||||
|
||||
@Post()
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async register(@Body() data: profesorDto) {
|
||||
return this.profesorService.register(data);
|
||||
}
|
||||
@@ -25,11 +30,13 @@ export class ProfesorController {
|
||||
}
|
||||
|
||||
@Put(':id_profesor')
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async postModify(@Param('id_profesor', ParseIntPipe) id_profesor: number, @Body() data: profesorDto) {
|
||||
return this.profesorService.modify(id_profesor, data);
|
||||
}
|
||||
|
||||
@Delete(':id_profesor')
|
||||
@Roles(Role.Responsable,Role.Admin)
|
||||
async remove(@Param('id_profesor', ParseIntPipe) id_profesor: number) {
|
||||
return this.profesorService.remove(id_profesor);
|
||||
}
|
||||
|
||||
@@ -3,9 +3,16 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ProfesorService } from './profesor.service';
|
||||
import { ProfesorController } from './profesor.controller';
|
||||
import { Profesor } from './entities/profesor.entity';
|
||||
import { DatosAcademicosModule } from '../datos_academicos/datos_academicos.module';
|
||||
import { LineasInvestigacionModule } from '../lineas_investigacion/lineas_investigacion.module';
|
||||
import { ProyectosAcademicosModule } from '../proyectos_academicos/proyectos_academicos.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Profesor])],
|
||||
imports: [TypeOrmModule.forFeature([Profesor]),
|
||||
DatosAcademicosModule,
|
||||
LineasInvestigacionModule,
|
||||
ProyectosAcademicosModule,
|
||||
],
|
||||
providers: [ProfesorService],
|
||||
controllers: [ProfesorController],
|
||||
})
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { HttpException, Injectable } from '@nestjs/common';
|
||||
import { Profesor } from './entities/profesor.entity';
|
||||
import { DatosAcademicosService } from '../datos_academicos/datos_academicos.service';
|
||||
import { LineasInvestigacionService } from '../lineas_investigacion/lineas_investigacion.service';
|
||||
import { ProyectosAcademicosService } from '../proyectos_academicos/proyectos_academicos.service';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { profesorDto } from './dto/profesorDto.dto';
|
||||
@@ -7,7 +10,10 @@ import { profesorDto } from './dto/profesorDto.dto';
|
||||
@Injectable()
|
||||
export class ProfesorService {
|
||||
constructor(
|
||||
@InjectRepository(Profesor) private profesorRepository: Repository<Profesor>
|
||||
@InjectRepository(Profesor) private profesorRepository: Repository<Profesor>,
|
||||
private datosAcademicosService: DatosAcademicosService,
|
||||
private lineasInvestigacionService: LineasInvestigacionService,
|
||||
private proyectosAcademicosService: ProyectosAcademicosService,
|
||||
) {}
|
||||
|
||||
//register teacher
|
||||
@@ -50,11 +56,16 @@ export class ProfesorService {
|
||||
|
||||
//remove teacher
|
||||
async remove(id_profesor: number) {
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor } });
|
||||
const profesor = await this.profesorRepository.findOne({ where: { id_profesor }, relations: ['datosAcademicos'] });
|
||||
if (!profesor) {
|
||||
throw new HttpException('Profesor not found', 404);
|
||||
}
|
||||
|
||||
// Delete related records in datos_academicos
|
||||
await this.datosAcademicosService.removeByProfesorId(id_profesor);
|
||||
await this.lineasInvestigacionService.removeByProfesorId(id_profesor);
|
||||
await this.proyectosAcademicosService.removeByProfesorId(id_profesor);
|
||||
|
||||
await this.profesorRepository.remove(profesor);
|
||||
return { message: 'Profesor removed successfully' };
|
||||
}
|
||||
|
||||
@@ -14,8 +14,13 @@ import {
|
||||
import { AuthGuard } from './auth.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
import { registerDto } from './dto/registerDto.dto';
|
||||
import {Roles} from '../permissions/roles.decorator'
|
||||
import {RolesGuard} from '../permissions/roles.guard'
|
||||
import {Role} from '../permissions/role.enum'
|
||||
|
||||
|
||||
@Controller('auth')
|
||||
@UseGuards(RolesGuard)
|
||||
export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
@@ -25,7 +30,9 @@ export class AuthController {
|
||||
return this.authService.signIn(data);
|
||||
}
|
||||
|
||||
@UseGuards()
|
||||
@Post("register")
|
||||
@Roles(Role.Admin)
|
||||
async register(@Body() data: registerDto){
|
||||
return this.authService.register(data)
|
||||
}
|
||||
@@ -36,14 +43,18 @@ export class AuthController {
|
||||
return req.user;
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Put('Modificacion/:id')
|
||||
@Roles(Role.Admin)
|
||||
async update(@Param('id') userId: number, @Body() data: registerDto) {
|
||||
return this.authService.update(userId, data);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async remove(@Param('id') userId: number) {
|
||||
await this.authService.remove(userId);
|
||||
@UseGuards(AuthGuard)
|
||||
@Delete('Borrado/:id')
|
||||
@Roles(Role.Admin)
|
||||
async remove(@Param('id') id: number) {
|
||||
await this.authService.remove(id);
|
||||
return { message: 'User successfully deleted' };
|
||||
}
|
||||
|
||||
|
||||
@@ -49,9 +49,11 @@ export class AuthService {
|
||||
if (!checkPassword) {
|
||||
throw new UnauthorizedException('Invalid password');
|
||||
}
|
||||
|
||||
const payload = {
|
||||
id_usuario: user.id_usuario,
|
||||
username: user.usuario,
|
||||
permissions: user.id_tipo_usuario,
|
||||
};
|
||||
const token = this.jwtService.sign(payload);
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@ export class registerDto{
|
||||
@IsNotEmpty()
|
||||
usuario: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
nombre: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
contraseña: string;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Controller, Delete, Param } from '@nestjs/common';
|
||||
import { DatosAcademicosService } from './datos_academicos.service';
|
||||
|
||||
@Controller('datos-academicos')
|
||||
export class DatosAcademicosController {
|
||||
constructor(private readonly datosAcademicosService: DatosAcademicosService) {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { DatosAcademicos } from './entities/datos_academicos.entity';
|
||||
import { DatosAcademicosService } from './datos_academicos.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([DatosAcademicos])],
|
||||
providers: [DatosAcademicosService],
|
||||
exports: [DatosAcademicosService],
|
||||
})
|
||||
export class DatosAcademicosModule {}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { DatosAcademicos } from './entities/datos_academicos.entity';
|
||||
|
||||
@Injectable()
|
||||
export class DatosAcademicosService {
|
||||
constructor(
|
||||
@InjectRepository(DatosAcademicos)
|
||||
private readonly datosAcademicosRepository: Repository<DatosAcademicos>,
|
||||
) {}
|
||||
|
||||
// Add methods to manage DatosAcademicos entities as needed
|
||||
async removeByProfesorId(id_profesor: number): Promise<void> {
|
||||
await this.datosAcademicosRepository.delete({ profesor: { id_profesor } });
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Entity, PrimaryColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Profesor } from '../../Profesor/entities/profesor.entity';
|
||||
|
||||
@Entity({ name: 'datos_academicos' })
|
||||
export class DatosAcademicos {
|
||||
@PrimaryColumn({ type: 'int', nullable: false })
|
||||
@PrimaryGeneratedColumn()
|
||||
id_datos: number;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
@@ -12,7 +12,7 @@ export class DatosAcademicos {
|
||||
@Column({ type: 'varchar', length: 30, nullable: true })
|
||||
grado_maximo: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 260, nullable: true })
|
||||
@Column({ type: 'varchar', length: 600, nullable: true })
|
||||
grados_obtenidos: string;
|
||||
|
||||
@ManyToOne(() => Profesor, (profesor) => profesor.datosAcademicos)
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { Entity, PrimaryColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Profesor } from "../../Profesor/entities/profesor.entity";
|
||||
|
||||
@Entity({ name: 'lineas_investigacion' })
|
||||
export class LineasInvestigacion {
|
||||
|
||||
@PrimaryColumn({ type: 'int', nullable: false })
|
||||
@PrimaryGeneratedColumn()
|
||||
id_linea_inv: number;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
id_profesor: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 255, nullable: true })
|
||||
@Column({ type: 'varchar', length: 300, nullable: true })
|
||||
lineas_inv: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 255, nullable: true })
|
||||
@Column({ type: 'varchar', length: 350, nullable: true })
|
||||
lineas_inv_html: string;
|
||||
|
||||
@ManyToOne(() => Profesor, profesor => profesor.lineasInvestigacion)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Controller, Delete, Param } from '@nestjs/common';
|
||||
import { LineasInvestigacionService } from './lineas_investigacion.service';
|
||||
|
||||
@Controller('lineas-investigacion')
|
||||
export class LineasInvestigacionController {
|
||||
constructor(private readonly lineasInvestigacionService: LineasInvestigacionService) {}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { LineasInvestigacion } from './entities/lineas_investigacion.entity';
|
||||
import { LineasInvestigacionService } from './lineas_investigacion.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([LineasInvestigacion])],
|
||||
providers: [LineasInvestigacionService],
|
||||
exports: [LineasInvestigacionService],
|
||||
})
|
||||
export class LineasInvestigacionModule {}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { LineasInvestigacion } from './entities/lineas_investigacion.entity';
|
||||
|
||||
@Injectable()
|
||||
export class LineasInvestigacionService {
|
||||
constructor(
|
||||
@InjectRepository(LineasInvestigacion)
|
||||
private readonly lineasInvestigacionRepository: Repository<LineasInvestigacion>,
|
||||
) {}
|
||||
|
||||
// Add methods to manage LineasInvestigacion entities as needed
|
||||
async removeByProfesorId(id_profesor: number): Promise<void> {
|
||||
await this.lineasInvestigacionRepository.delete({ profesor: { id_profesor } });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export enum Role {
|
||||
Admin = '1',
|
||||
Responsable = '2',
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { Role } from './role.enum';
|
||||
|
||||
export const ROLES_KEY = 'roles';
|
||||
export const Roles = (...roles: Role[]) => SetMetadata(ROLES_KEY, roles);
|
||||
@@ -0,0 +1,33 @@
|
||||
// roles.guard.ts
|
||||
|
||||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { Role } from './role.enum';
|
||||
import { ROLES_KEY } from './roles.decorator'; // Asegúrate de importar el decorador correcto
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(private reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
|
||||
if (!requiredRoles) {
|
||||
return true; // Si no hay roles requeridos, se permite el acceso
|
||||
}
|
||||
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const permissionsHeader = request.headers['permissions'];
|
||||
|
||||
if (!permissionsHeader) {
|
||||
return false; // Si no hay encabezado 'permissions', se niega el acceso
|
||||
}
|
||||
|
||||
const userRoles: Role[] = JSON.parse(permissionsHeader);
|
||||
|
||||
return requiredRoles.some(role => userRoles.includes(role));
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
import { Entity, PrimaryColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Profesor } from "../../Profesor/entities/profesor.entity";
|
||||
|
||||
@Entity({ name: 'proyectos_academicos' })
|
||||
export class ProyectosAcademicos {
|
||||
|
||||
@PrimaryColumn({ type: 'int', nullable: false })
|
||||
@PrimaryGeneratedColumn()
|
||||
id_proyecto: number;
|
||||
|
||||
@Column({ type: 'int', nullable: false })
|
||||
id_profesor: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 255, nullable: true })
|
||||
@Column({ type: 'varchar', length: 500, nullable: true })
|
||||
proyecto: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 255, nullable: true })
|
||||
@Column({ type: 'varchar', length: 700, nullable: true })
|
||||
proyecto_html: string;
|
||||
|
||||
@ManyToOne(() => Profesor, profesor => profesor.proyectosAcademicos)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Controller, Delete, Param } from '@nestjs/common';
|
||||
import { ProyectosAcademicosService } from './proyectos_academicos.service';
|
||||
|
||||
@Controller('proyectos-academicos')
|
||||
export class ProyectosAcademicosController {
|
||||
constructor(private readonly proyectosAcademicosService: ProyectosAcademicosService) {}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ProyectosAcademicos } from './entities/proyectos_academicos.entity';
|
||||
import { ProyectosAcademicosService } from './proyectos_academicos.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([ProyectosAcademicos])],
|
||||
providers: [ProyectosAcademicosService],
|
||||
exports: [ProyectosAcademicosService],
|
||||
})
|
||||
export class ProyectosAcademicosModule {}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ProyectosAcademicos } from './entities/proyectos_academicos.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ProyectosAcademicosService {
|
||||
constructor(
|
||||
@InjectRepository(ProyectosAcademicos)
|
||||
private readonly proyectosAcademicosRepository: Repository<ProyectosAcademicos>,
|
||||
) {}
|
||||
|
||||
// Add methods to manage ProyectosAcademicos entities as needed
|
||||
async removeByProfesorId(id_profesor: number): Promise<void> {
|
||||
await this.proyectosAcademicosRepository.delete({ profesor: { id_profesor } });
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Entity, PrimaryColumn, Column, OneToMany } from 'typeorm';
|
||||
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
|
||||
@Entity({ name: 'tipo_usuario' })
|
||||
export class TipoUsuario {
|
||||
@PrimaryColumn({ type: 'int', nullable: false })
|
||||
@PrimaryGeneratedColumn()
|
||||
id_tipo_usuario: number;
|
||||
|
||||
@Column({ type: 'varchar', length: 30, nullable: false })
|
||||
|
||||
@@ -11,6 +11,9 @@ export class User {
|
||||
@Column({ type: 'int', nullable: false })
|
||||
id_tipo_usuario: number;
|
||||
|
||||
@Column({type: 'varchar', length: 40, nullable: false })
|
||||
nombre: string;
|
||||
|
||||
@Column({ type: 'varchar', length: 60, nullable: false })
|
||||
correo: string;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user