diff --git a/src/decorators/set-origin.decorator.ts b/src/decorators/set-origin.decorator.ts new file mode 100644 index 0000000..c6d5338 --- /dev/null +++ b/src/decorators/set-origin.decorator.ts @@ -0,0 +1,32 @@ +import { Injectable, CanActivate, ExecutionContext, ForbiddenException } from '@nestjs/common'; +import { Reflector } from '@nestjs/core'; +import { SetMetadata } from '@nestjs/common'; + +export const SetOrigin = (origin: string) => SetMetadata('allowedOrigin', origin); + +@Injectable() +export class CORSGuard implements CanActivate { + constructor(private reflector: Reflector) {} + + canActivate(context: ExecutionContext): boolean { + const response = context.switchToHttp().getResponse(); + const request = context.switchToHttp().getRequest(); + const origin = request.headers.origin; + + // Obter a origem permitida através do decorador + const allowedOrigin = this.reflector.get('allowedOrigin', context.getHandler()); + if (process.env.ENV === "local") { + return true; + } + + // Verifica se a origem da requisição é permitida + if (allowedOrigin && origin !== allowedOrigin) { + throw new ForbiddenException('Acesso não permitido pela política CORS'); + } + + response.setHeader('Access-Control-Allow-Origin', allowedOrigin); + + return true; + } +} + diff --git a/src/modules/customers/customers.controller.ts b/src/modules/customers/customers.controller.ts index 4268e6a..7afc3fb 100644 --- a/src/modules/customers/customers.controller.ts +++ b/src/modules/customers/customers.controller.ts @@ -59,9 +59,21 @@ export class CustomersController { this.logger.info('saveCustomertheme', JSON.stringify({ id, theme: data })); - const theme = await this.customersService.createThemeByCustomer(id, data); - return theme; + try { + // const theme = await this.customersService.createThemeByCustomer(id, data); + this.logger.info('saveCustomertheme', JSON.stringify(data)); + return { theme: null }; + } catch (err) { + if (err.details === ErrorCodes.CUSTOMER.NOT_FOUND) { + this.logger.error('Error - saveCustomertheme - Expect CUSTOMER.NOT_FOUND'); + throw new HttpException(err.details, HttpStatus.NOT_FOUND); + } else { + this.logger.error('Error - saveCustomertheme Unknown Error:' + err?.message); + return { theme: null }; + }; + } + } @Get(':id/theme') diff --git a/src/modules/customers/customers.service.ts b/src/modules/customers/customers.service.ts index 5dc43f3..4e2ce3a 100644 --- a/src/modules/customers/customers.service.ts +++ b/src/modules/customers/customers.service.ts @@ -96,32 +96,23 @@ export class CustomersService implements OnModuleInit { throw new HttpException(null, HttpStatus.BAD_REQUEST); } - try { - const result = await firstValueFrom( - this.customerService.CustomerCreateTheme({ - customerId: id, - theme - }), - ); - return { theme: result }; - } catch (err) { - if (err.details === ErrorCodes.CUSTOMER.NOT_FOUND) { - this.logger.error('Error - saveCustomertheme - Expect CUSTOMER.NOT_FOUND'); - throw new HttpException(err.details, HttpStatus.NOT_FOUND); - } else { - this.logger.error('Error - saveCustomertheme Unknown Error:' + err?.message); - throw err - }; - } + const result = await firstValueFrom( + this.customerService.CustomerCreateTheme({ + customerId: id, + theme + }), + ); + return { theme: result }; + } - async getThemeByCustomer(id: string): Promise { + async getThemeByCustomer(id: string): Promise { if (!id) { this.logger.error('Error - getCustomerTheme - not found id:' + id); throw new HttpException(null, HttpStatus.BAD_REQUEST); } - const result = await firstValueFrom( + const result = await firstValueFrom( this.customerService.CustomerGetTheme({ id }), diff --git a/src/modules/open-data/open-data.controller.ts b/src/modules/open-data/open-data.controller.ts index 05f915b..86252a1 100644 --- a/src/modules/open-data/open-data.controller.ts +++ b/src/modules/open-data/open-data.controller.ts @@ -1,6 +1,6 @@ import DadosferaLogger from '@dadosfera/dadosfera-logs'; -import { Body, Controller, Inject, Post, UseFilters } from '@nestjs/common'; -import { ApiCreatedResponse, ApiHeaders, ApiTags } from '@nestjs/swagger'; +import { Body, Controller, Inject, Post, UseFilters, UseGuards } from '@nestjs/common'; +import { ApiCreatedResponse, ApiHeaders, ApiOkResponse, ApiTags } from '@nestjs/swagger'; import { ApiInternalOnlyController } from 'src/decorators/swagger.decorator'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { LanguageEnum } from 'src/utils/languages.enum'; @@ -8,6 +8,7 @@ import { UsersService } from '../users/users.service'; import { Language } from 'src/decorators/language.decorator'; import { OpenDataService } from './open-data.service'; import { CreateUserOpenDataDTO, WordpressForm } from './dto/wordpres-form'; +import { CORSGuard, SetOrigin } from 'src/decorators/set-origin.decorator'; @Controller('open-data') @ApiInternalOnlyController() @@ -26,7 +27,9 @@ export class OpenDataController { } @Post("/sharing-ocean-data") - @ApiCreatedResponse() + @SetOrigin('https://devsbm.dadosfera.io/') + @UseGuards(CORSGuard) + @ApiOkResponse() async createUser( @Body() body: WordpressForm @@ -34,10 +37,12 @@ export class OpenDataController { this.logger.info('createUser for open data' + JSON.stringify({ body })); // "401573bb-334f-44b2-b30e-88d4cea31ae9" - const OPENDATA_PUBLIC_USERS_GROUP_ID = process.env.OPEN_GROUP_ID; + // const OPENDATA_PUBLIC_USERS_GROUP_ID = process.env.OPEN_GROUP_ID; // ""f239718a-a271-4ef9-ae7e-02a2f0f3aa6e"" - const OPENDATA_CUSTOMER_ID = process.env.OPEN_CUSTOMER_ID; - const roles = [OPENDATA_PUBLIC_USERS_GROUP_ID]; + // const OPENDATA_CUSTOMER_ID = process.env.OPEN_CUSTOMER_ID; + const OPENDATA_CUSTOMER_ID = "bb4e9b26-d465-40ff-9345-e768ca69a55e"; + + const roles = ["4d621501-5ea4-444b-b46e-1a009f3132ff"]; const data = {} @@ -60,10 +65,11 @@ export class OpenDataController { lastName: data["last_name"], organization: data["organization"] } - this.logger.info('user data' + JSON.stringify({ user })); + this.logger.info('user request' + JSON.stringify({ user, roles, customer: OPENDATA_CUSTOMER_ID })); try { - await this.openDataService.createUser(OPENDATA_CUSTOMER_ID, user, roles); + const response = await this.openDataService.createUser(OPENDATA_CUSTOMER_ID, user, roles); + this.logger.info('user created with sucessfull data' + JSON.stringify(response)); return "success" } catch (e) { this.logger.error('user data' + e.message);