-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🔥 [EXL-74] support rules list page
support rules list page
- Loading branch information
tal-rofe
committed
Nov 14, 2022
1 parent
1f0928c
commit add4dd9
Showing
50 changed files
with
924 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createParamDecorator, type ExecutionContext } from '@nestjs/common'; | ||
|
||
export const Library = createParamDecorator((_: undefined, context: ExecutionContext) => { | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
return request.policyLibrary; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { BadRequestException, Injectable, type CanActivate, type ExecutionContext } from '@nestjs/common'; | ||
|
||
import { librariesData } from '@/data/libraries-data'; | ||
|
||
import type { IJwtTokenPayload } from '../interfaces/jwt-token'; | ||
import { DBInlinePolicyService } from '../modules/database/inline-policy.service'; | ||
|
||
@Injectable() | ||
export class RuleablePolicyGuard implements CanActivate { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const user = request.user as IJwtTokenPayload; | ||
const userId = user.sub; | ||
const inlinePolicyId = request.params.policy_id as string; | ||
|
||
const policyLibrary = await this.dbInlinePolicyService.getPolicyLibrary(inlinePolicyId, userId); | ||
const matchingLibraryData = librariesData.find((library) => library.name === policyLibrary)!; | ||
|
||
if (Object.keys(matchingLibraryData.rules ?? {}).length === 0) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
request.policyLibrary = policyLibrary; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...d/src/modules/user/modules/inline-policies/queries/contracts/get-policy-rules.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import type { PolicyLibrary } from '@prisma/client'; | ||
|
||
export class GetPolicyRulesContract { | ||
constructor(readonly policyId: string, readonly page?: string) {} | ||
constructor(readonly policyId: string, readonly library: PolicyLibrary, readonly page?: string) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/backend/src/modules/user/modules/rules/classes/enable-rule.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApiProperty, ApiResponseProperty } from '@nestjs/swagger'; | ||
import type { IEnableRuleDto, IEnableRuleResponseData } from '@exlint-dashboard/common'; | ||
import { IsString } from 'class-validator'; | ||
import type { Rule } from '@prisma/client'; | ||
|
||
export class EnableRuleDto implements IEnableRuleDto { | ||
@ApiProperty({ type: String, description: 'The new label for a group', example: 'Yazif Group' }) | ||
@IsString() | ||
readonly name!: Rule['name']; | ||
} | ||
|
||
export class EnableRuleResponse implements IEnableRuleResponseData { | ||
@ApiResponseProperty({ | ||
type: String, | ||
example: '62e5362119bea07115434f4a', | ||
}) | ||
public id!: string; | ||
} |
60 changes: 60 additions & 0 deletions
60
apps/backend/src/modules/user/modules/rules/enable-rule.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Body, Controller, HttpCode, HttpStatus, Logger, Param, Post, UseGuards } from '@nestjs/common'; | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiBearerAuth, | ||
ApiCreatedResponse, | ||
ApiInternalServerErrorResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
import { PolicyLibrary } from '@prisma/client'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
import { Library } from '@/decorators/library.decorator'; | ||
import { RuleablePolicyGuard } from '@/guards/ruleable-policy.guard'; | ||
|
||
import Routes from './rules.routes'; | ||
import { EnableRuleContract } from './queries/contracts/enable-rule.contract'; | ||
import { EnableRuleDto, EnableRuleResponse } from './classes/enable-rule.dto'; | ||
|
||
@ApiTags('Rules') | ||
@Controller(Routes.CONTROLLER) | ||
export class EnableRuleController { | ||
private readonly logger = new Logger(EnableRuleController.name); | ||
|
||
constructor(private readonly queryBus: QueryBus) {} | ||
|
||
@ApiOperation({ description: 'Enable a rule for a policy' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiCreatedResponse({ description: 'If successfully enabled the rule', type: EnableRuleResponse }) | ||
@ApiBadRequestResponse({ description: "If rule name is invalid or policy's library has no rules" }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or policy does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to enable the rule' }) | ||
@UseGuards(RuleablePolicyGuard) | ||
@Post(Routes.ENABLE_RULE) | ||
@HttpCode(HttpStatus.CREATED) | ||
public async enableRule( | ||
@CurrentUserId() userId: string, | ||
@Param('policy_id') policyId: string, | ||
@Body() enableRuleDto: EnableRuleDto, | ||
@Library() policyLibrary: PolicyLibrary, | ||
): Promise<EnableRuleResponse> { | ||
this.logger.log( | ||
`Will try to enable a rule for a policy with an ID: "${policyId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
const createdRuleId = await this.queryBus.execute<EnableRuleContract, EnableRuleResponse['id']>( | ||
new EnableRuleContract(policyId, enableRuleDto.name, policyLibrary), | ||
); | ||
|
||
this.logger.log( | ||
`Successfully enabled a rule with an ID: "${createdRuleId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
return { id: createdRuleId }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
apps/backend/src/modules/user/modules/rules/queries/contracts/enable-rule.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { PolicyLibrary } from '@prisma/client'; | ||
|
||
export class EnableRuleContract { | ||
constructor( | ||
public readonly policyId: string, | ||
public readonly name: string, | ||
public readonly policyLibrary: PolicyLibrary, | ||
) {} | ||
} |
4 changes: 3 additions & 1 deletion
4
apps/backend/src/modules/user/modules/rules/queries/contracts/get-rules.contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import type { PolicyLibrary } from '@prisma/client'; | ||
|
||
export class GetRulesContract { | ||
constructor(public readonly policyId: string) {} | ||
constructor(public readonly policyId: string, public readonly policyLibrary: PolicyLibrary) {} | ||
} |
25 changes: 25 additions & 0 deletions
25
apps/backend/src/modules/user/modules/rules/queries/handlers/enable-rule.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { QueryHandler, type IQueryHandler } from '@nestjs/cqrs'; | ||
import type { IEnableRuleResponseData } from '@exlint-dashboard/common'; | ||
import { BadRequestException } from '@nestjs/common'; | ||
|
||
import { DBRuleService } from '@/modules/database/rule.service'; | ||
import { librariesData } from '@/data/libraries-data'; | ||
|
||
import { EnableRuleContract } from '../contracts/enable-rule.contract'; | ||
|
||
@QueryHandler(EnableRuleContract) | ||
export class EnableRuleHandler implements IQueryHandler<EnableRuleContract> { | ||
constructor(private readonly dbRuleService: DBRuleService) {} | ||
|
||
async execute(contract: EnableRuleContract): Promise<IEnableRuleResponseData['id']> { | ||
const libraryData = librariesData.find((library) => library.name === contract.policyLibrary)!; | ||
|
||
if (!libraryData.rules![contract.name]) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
const createdRuleRecord = await this.dbRuleService.enableRule(contract.policyId, contract.name); | ||
|
||
return createdRuleRecord.id; | ||
} | ||
} |
Oops, something went wrong.