diff --git a/src/authentication/authentication.guard.ts b/src/authentication/authentication.guard.ts index 7e938b0..bc69f8c 100644 --- a/src/authentication/authentication.guard.ts +++ b/src/authentication/authentication.guard.ts @@ -121,6 +121,7 @@ export class AuthenticationGuard customer_id: accessTokenPayload.customer_id, customer_name: accessTokenPayload.customer_name, customer_tier: accessTokenPayload.customer_tier, + customer_modules: accessTokenPayload.customer_modules, access_token: accessToken, }; // TODO: for backwards compatibility. remove in the future diff --git a/src/authentication/permissions.enum.ts b/src/authentication/permissions.enum.ts index 5116cfe..1e01531 100644 --- a/src/authentication/permissions.enum.ts +++ b/src/authentication/permissions.enum.ts @@ -609,6 +609,12 @@ export interface DadosferaModule { key: string; permissionSeqId: number; } + +export const DADOSFERA_MODULES_KEYS = { + LOG_DASHBOARD: 'logs-dashboard', + ACCESS_DASHBOARD: 'access-dashboard' +} + export const DADOSFERA_MODULES: Array = [ { name: 'Intelligence Module', diff --git a/src/decorators/authentication.decorator.ts b/src/decorators/authentication.decorator.ts index 385ced0..03f9aed 100644 --- a/src/decorators/authentication.decorator.ts +++ b/src/decorators/authentication.decorator.ts @@ -25,6 +25,14 @@ export function RequireSomePermission( ); } +export function RequireModule( + key: string +) { + return createAuthenticatedDecorator((_, user: RequestUser) => + user.customer_modules.some(module => module === key), + ); +} + export function AuthenticateCondition(func: AuthenticationFunction) { return createAuthenticatedDecorator(func); } diff --git a/src/decorators/user.decorator.ts b/src/decorators/user.decorator.ts index 94b6045..2ce38ae 100644 --- a/src/decorators/user.decorator.ts +++ b/src/decorators/user.decorator.ts @@ -11,6 +11,7 @@ export interface RequestUser { customer_name: string; customer_tier: string; access_token: string; + customer_modules: string[]; } export const User: (options?: { required?: boolean }) => ParameterDecorator = diff --git a/src/modules/customers/customers.controller.ts b/src/modules/customers/customers.controller.ts index 7700eb3..280dce3 100644 --- a/src/modules/customers/customers.controller.ts +++ b/src/modules/customers/customers.controller.ts @@ -12,10 +12,11 @@ import { UseFilters, } from '@nestjs/common'; import { ApiOkResponse, ApiProduces, ApiTags } from '@nestjs/swagger'; -import { PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; +import { DADOSFERA_MODULES_KEYS, PERMISSIONS_GROUPS } from 'src/authentication/permissions.enum'; import { Authenticated, RequireAllPermissions, + RequireModule, } from 'src/decorators/authentication.decorator'; import { GrpcToHttpExceptionFilter } from 'src/error/grpc-to-http-exception.filter'; import { CustomersService } from './customers.service'; @@ -97,15 +98,30 @@ export class CustomersController { @RequireAllPermissions( PERMISSIONS_GROUPS.USERS.permissions.ADMIN, ) + @RequireModule(DADOSFERA_MODULES_KEYS.LOG_DASHBOARD) async getCustomerMixPanelLogsDashboard( @User() user: RequestUser, - ): Promise<{ url: string }> { - this.logger.info('getLogsDashboardUrl'); + ): Promise<{ url: string }> { + this.logger.info('getLogsDashboardUrl'); const metadata = PackTheMetadata(user); - const result = await this.customersService.getLogsDashboardUrl(metadata); + const result = await this.customersService.getLogsDashboardUrl(metadata); return result; } - + @Get('access-dashboard') + @Authenticated() + @RequireAllPermissions( + PERMISSIONS_GROUPS.USERS.permissions.ADMIN, + ) + @RequireModule(DADOSFERA_MODULES_KEYS.ACCESS_DASHBOARD) + async getAccessDashboard( + @User() user: RequestUser, + ): Promise<{ url: string }> { + this.logger.info('getAccessDashboard'); + const metadata = PackTheMetadata(user); + + const result = await this.customersService.getAccessDashboardUrl(user.customer_name, metadata); + return result; + } } diff --git a/src/modules/customers/customers.service.ts b/src/modules/customers/customers.service.ts index 36df7c3..ed302fd 100644 --- a/src/modules/customers/customers.service.ts +++ b/src/modules/customers/customers.service.ts @@ -5,6 +5,7 @@ import { HttpException, HttpStatus, InternalServerErrorException, + ForbiddenException, } from '@nestjs/common'; import { firstValueFrom, lastValueFrom } from 'rxjs'; @@ -173,4 +174,33 @@ export class CustomersService implements OnModuleInit { return res; } + + async getAccessDashboardUrl(customerName: string, metadata: Metadata) { + /* + * TODO(Refactor): dar um jeito de exibir o dash da sbm diferente dos outros customer + * pois o signicado de department para sbm significa as instituições do usuários + */ + if (customerName !== 'sbmoffshorecom') { + throw new ForbiddenException(); + } + logger.info('CustomersService - getAccessDashboardUrl'); + + const res = await this.getDashboardUrl('105', metadata); + logger.info('Done'); + + return res; + } + + private async getDashboardUrl(dashboardId: string, metadata: Metadata) { + return await lastValueFrom( + this.pipelineReadService.PipelineV2GetDashboardUrl( + { + dashboard_id: dashboardId, + exp: '15m', + metabase_customer_name: 'dadosferatech', + }, + metadata + ) + ); + } }