Commit c8bc80c7 authored by Lorenzo Trujillo Rojas's avatar Lorenzo Trujillo Rojas
Browse files

Merge branch 'main' into 'main'

Agregando validaciones en la autorizacion

See merge request pueblosmagicosconia!38
parents 91eb7c4d 4634cdb4
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -3,10 +3,12 @@ import { AdminService } from './admin.service';
import { AdminController } from './admin.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Admin } from './entities/admin.entity';
import { Town } from 'src/town/entities/town.entity';

@Module({
  controllers: [AdminController],
  providers: [AdminService],
  imports: [TypeOrmModule.forFeature([Admin])],
  imports: [TypeOrmModule.forFeature([Admin, Town])],
  exports: [AdminService],
})
export class AdminModule {}
+7 −2
Original line number Diff line number Diff line
@@ -3,12 +3,17 @@ import { CreateAdminDto } from './dto/create-admin.dto';
import { Admin } from './entities/admin.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Town } from 'src/town/entities/town.entity';

@Injectable()
export class AdminService {
  constructor(@InjectRepository(Admin) private adminRepository: Repository<Admin>) {}
  constructor(
    @InjectRepository(Admin) private adminRepository: Repository<Admin>,
    @InjectRepository(Town) private townRepository: Repository<Town>,
  ) {}
  async create(createAdminDto: CreateAdminDto) {
    await this.adminRepository.insert(createAdminDto);
    const town = await this.townRepository.findOneByOrFail({ townId: createAdminDto.idTown });
    await this.adminRepository.insert({ ...createAdminDto, idTown: town });
  }

  async findOne(email: string): Promise<Admin> {
+2 −2
Original line number Diff line number Diff line
@@ -9,8 +9,8 @@ export class Admin {
  email: string;

  @JoinColumn({ name: 'idTown' })
  @ManyToOne(() => Town, { nullable: true })
  idTown: number;
  @ManyToOne(() => Town, (town) => town.townId, { nullable: true, eager: true })
  idTown: Town;

  @Column()
  name: string;
+2 −4
Original line number Diff line number Diff line
@@ -17,9 +17,8 @@ import { join } from 'path';
import { Town } from './town/entities/town.entity';
import { TownModule } from './town/town.module';
import { TownTraduction } from './town/entities/town-traduction.entity';
import { AuthModule } from './shared/service/auth.module';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './auth/admin/auth.guard';
import { AuthAdminGuard } from './auth/admin/authAdmin.guard';
import { PlaceModule } from './place/place.module';
import { Place } from './place/entities/place.entity';
import { PointOfInterestModule } from './pointOfInterest/PointOfInterest.module';
@@ -59,7 +58,6 @@ import { PlaceTraduction } from './place/entities/place-traduction.entity';
    StateModule,
    DatabaseSeederModule,
    TownModule,
    AuthModule,
    PlaceModule,
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'static'),
@@ -67,7 +65,7 @@ import { PlaceTraduction } from './place/entities/place-traduction.entity';
    PointOfInterestModule,
  ],
  controllers: [AppController],
  providers: [AppService, DatabaseSeederModule, { provide: APP_GUARD, useClass: AuthGuard }],
  providers: [AppService, DatabaseSeederModule, { provide: APP_GUARD, useClass: AuthAdminGuard }],
  exports: [TypeOrmModule],
})
export class AppModule {}
+9 −5
Original line number Diff line number Diff line
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthService } from 'src/shared/service/auth.service';
import { Roles } from '../role.decorator';
import { AuthAdminService } from './authAdmin.service';
import { AdminService } from 'src/admin/admin.service';

@Injectable()
export class AuthGuard implements CanActivate {
export class AuthAdminGuard implements CanActivate {
  constructor(
    private authService: AuthService,
    private authAdminService: AuthAdminService,
    private adminService: AdminService,
    private reflector: Reflector,
  ) {}
  async canActivate(context: ExecutionContext): Promise<boolean> {
@@ -16,8 +18,10 @@ export class AuthGuard implements CanActivate {
    let { authorization }: any = request.headers;
    if (!authorization) throw new UnauthorizedException('session expired! Please sign In');
    authorization = authorization.split(' ')[1];
    const role = await this.authService.validateToken(authorization);
    if (!requiredRole.includes(role)) throw new UnauthorizedException('Unauthorized access');
    const jwtPayload = await this.authAdminService.validateToken(authorization);
    if (!requiredRole.includes(jwtPayload.role)) throw new UnauthorizedException('Unauthorized access');
    const admin = await this.adminService.findOne(jwtPayload.email);
    request.admin = { ...admin };
    return true;
  }
}
Loading