-
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-73] support settings page for policy
support settings page for policy
- Loading branch information
tal-rofe
committed
Oct 7, 2022
1 parent
1ad2b05
commit 408140c
Showing
48 changed files
with
1,175 additions
and
25 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
3 changes: 2 additions & 1 deletion
3
apps/backend/src/modules/user/modules/groups/classes/edit-label.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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsString, MinLength } from 'class-validator'; | ||
import { IsString, MaxLength, MinLength } from 'class-validator'; | ||
|
||
export class EditLabelDto { | ||
@ApiProperty({ type: String, description: 'The new label for a group', example: 'Yazif Group' }) | ||
@IsString() | ||
@MaxLength(30) | ||
@MinLength(1) | ||
readonly label!: string; | ||
} |
10 changes: 10 additions & 0 deletions
10
apps/backend/src/modules/user/modules/inline-policies/classes/edit-label.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,10 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsString, MaxLength, MinLength } from 'class-validator'; | ||
|
||
export class EditLabelDto { | ||
@ApiProperty({ type: String, description: 'The new label for a policy', example: 'Yazif Policy' }) | ||
@IsString() | ||
@MaxLength(30) | ||
@MinLength(1) | ||
readonly label!: 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
3 changes: 3 additions & 0 deletions
3
apps/backend/src/modules/user/modules/inline-policies/commands/contracts/delete.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,3 @@ | ||
export class DeleteContract { | ||
constructor(public readonly policyId: string) {} | ||
} |
3 changes: 3 additions & 0 deletions
3
...ackend/src/modules/user/modules/inline-policies/commands/contracts/edit-label.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,3 @@ | ||
export class EditLabelContract { | ||
constructor(public readonly policyId: string, public readonly label: string) {} | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/backend/src/modules/user/modules/inline-policies/commands/handlers/delete.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,14 @@ | ||
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
import { DeleteContract } from '../contracts/delete.contract'; | ||
|
||
@CommandHandler(DeleteContract) | ||
export class DeleteHandler implements ICommandHandler<DeleteContract> { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async execute(contract: DeleteContract) { | ||
await this.dbInlinePolicyService.deletePolicy(contract.policyId); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../backend/src/modules/user/modules/inline-policies/commands/handlers/edit-label.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,14 @@ | ||
import { CommandHandler, type ICommandHandler } from '@nestjs/cqrs'; | ||
|
||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
import { EditLabelContract } from '../contracts/edit-label.contract'; | ||
|
||
@CommandHandler(EditLabelContract) | ||
export class EditLabelHandler implements ICommandHandler<EditLabelContract> { | ||
constructor(private readonly dbInlinePolicyService: DBInlinePolicyService) {} | ||
|
||
async execute(contract: EditLabelContract) { | ||
await this.dbInlinePolicyService.editPolicyLabel(contract.policyId, contract.label); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
apps/backend/src/modules/user/modules/inline-policies/commands/handlers/index.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,4 @@ | ||
import { CreateInlineHandler } from './create.handler'; | ||
import { DeleteHandler } from './delete.handler'; | ||
import { EditLabelHandler } from './edit-label.handler'; | ||
|
||
export const CommandHandlers = [CreateInlineHandler]; | ||
export const CommandHandlers = [EditLabelHandler, DeleteHandler]; |
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
49 changes: 49 additions & 0 deletions
49
apps/backend/src/modules/user/modules/inline-policies/delete.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,49 @@ | ||
import { Controller, Delete, HttpCode, HttpStatus, Logger, Param, UseGuards } from '@nestjs/common'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './inline-policies.routes'; | ||
import { DeleteContract } from './commands/contracts/delete.contract'; | ||
import { BelongingInlinePolicyGuard } from './guards/belonging-inline-policy.guard'; | ||
|
||
@ApiTags('Inline Policies') | ||
@Controller(Routes.CONTROLLER) | ||
export class DeleteController { | ||
private readonly logger = new Logger(DeleteController.name); | ||
|
||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
@ApiOperation({ description: 'Deleting a policy with provided identifier' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ description: 'If successfully deleted the policy' }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or policy does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to delete the policy' }) | ||
@UseGuards(BelongingInlinePolicyGuard) | ||
@Delete(Routes.DELETE) | ||
@HttpCode(HttpStatus.OK) | ||
public async delete( | ||
@CurrentUserId() userId: string, | ||
@Param('policy_id') policyId: string, | ||
): Promise<void> { | ||
this.logger.log( | ||
`Will try to delete a policy with an ID: "${policyId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
await this.commandBus.execute<DeleteContract, void>(new DeleteContract(policyId)); | ||
|
||
this.logger.log( | ||
`Successfully deleted a policy with an ID: "${policyId}" for a user with an ID: "${userId}"`, | ||
); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
apps/backend/src/modules/user/modules/inline-policies/edit-label.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,53 @@ | ||
import { Body, Controller, HttpCode, HttpStatus, Logger, Param, Patch, UseGuards } from '@nestjs/common'; | ||
import { CommandBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './inline-policies.routes'; | ||
import { EditLabelDto } from './classes/edit-label.dto'; | ||
import { EditLabelContract } from './commands/contracts/edit-label.contract'; | ||
import { BelongingInlinePolicyGuard } from './guards/belonging-inline-policy.guard'; | ||
|
||
@ApiTags('Inline Policies') | ||
@Controller(Routes.CONTROLLER) | ||
export class EditLabelController { | ||
private readonly logger = new Logger(EditLabelController.name); | ||
|
||
constructor(private readonly commandBus: CommandBus) {} | ||
|
||
@ApiOperation({ description: "Edit a policy's label with a new label and its identifier" }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ description: 'If successfully edited the label of the policy' }) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is either missing or invalid, or policy does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to edit the label of the policy' }) | ||
@UseGuards(BelongingInlinePolicyGuard) | ||
@Patch(Routes.EDIT_LABEL) | ||
@HttpCode(HttpStatus.OK) | ||
public async editLabel( | ||
@CurrentUserId() userId: string, | ||
@Body() editLabelDto: EditLabelDto, | ||
@Param('policy_id') policyId: string, | ||
): Promise<void> { | ||
this.logger.log( | ||
`Will try to edit a policy with an ID: "${policyId}" for a user with an ID: "${userId}"`, | ||
); | ||
|
||
await this.commandBus.execute<EditLabelContract, void>( | ||
new EditLabelContract(policyId, editLabelDto.label), | ||
); | ||
|
||
this.logger.log( | ||
`Successfully edited a policy with an ID: "${policyId}" for a user with an Id: "${userId}"`, | ||
); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
apps/backend/src/modules/user/modules/inline-policies/get.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,55 @@ | ||
import { Controller, Get, HttpCode, HttpStatus, Logger, Param, UseGuards } from '@nestjs/common'; | ||
import { QueryBus } from '@nestjs/cqrs'; | ||
import { | ||
ApiBearerAuth, | ||
ApiInternalServerErrorResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
ApiUnauthorizedResponse, | ||
} from '@nestjs/swagger'; | ||
|
||
import { CurrentUserId } from '@/decorators/current-user-id.decorator'; | ||
|
||
import Routes from './inline-policies.routes'; | ||
import { GetResponse } from './classes/responses'; | ||
import { GetContract } from './queries/contracts/get.contract'; | ||
import { BelongingInlinePolicyGuard } from './guards/belonging-inline-policy.guard'; | ||
|
||
@ApiTags('Inline Policies') | ||
@Controller(Routes.CONTROLLER) | ||
export class GetController { | ||
private readonly logger = new Logger(GetController.name); | ||
|
||
constructor(private readonly queryBus: QueryBus) {} | ||
|
||
@ApiOperation({ description: 'Get data of policy' }) | ||
@ApiBearerAuth('access-token') | ||
@ApiOkResponse({ | ||
description: 'Returns data of policy', | ||
type: GetResponse, | ||
}) | ||
@ApiUnauthorizedResponse({ | ||
description: 'If access token is invalid or missing, or provided policy does not belong to user', | ||
}) | ||
@ApiInternalServerErrorResponse({ description: 'If failed to get data of policy' }) | ||
@UseGuards(BelongingInlinePolicyGuard) | ||
@Get(Routes.GET) | ||
@HttpCode(HttpStatus.OK) | ||
public async get( | ||
@CurrentUserId() userId: string, | ||
@Param('policy_id') policyId: string, | ||
): Promise<GetResponse> { | ||
this.logger.log( | ||
`Will try to get data of a policy with an ID: "${policyId}" with a user ID: "${userId}"`, | ||
); | ||
|
||
const policyData = await this.queryBus.execute<GetContract, GetResponse>(new GetContract(policyId)); | ||
|
||
this.logger.log( | ||
`Successfully got data of a policy with an ID: "${policyId}" with a user ID: "${userId}"`, | ||
); | ||
|
||
return policyData; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../backend/src/modules/user/modules/inline-policies/guards/belonging-inline-policy.guard.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,23 @@ | ||
import { Injectable, type CanActivate, type ExecutionContext } from '@nestjs/common'; | ||
|
||
import type { IJwtTokenPayload } from '@/interfaces/jwt-token'; | ||
import { DBInlinePolicyService } from '@/modules/database/inline-policy.service'; | ||
|
||
@Injectable() | ||
export class BelongingInlinePolicyGuard 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 inlinePolicyBelongsUser = await this.dbInlinePolicyService.doesInlinePolicyBelongUser( | ||
inlinePolicyId, | ||
userId, | ||
); | ||
|
||
return inlinePolicyBelongsUser; | ||
} | ||
} |
Oops, something went wrong.