Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent workflow version from bad update #6848

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';

import { WorkflowVersionUpdateOnePreQueryHook } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-update-one.pre-query-hook';
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Module({
providers: [
WorkflowVersionUpdateOnePreQueryHook,
WorkflowVersionValidationWorkspaceService,
WorkflowCommonWorkspaceService,
],
})
export class WorkflowQueryHookModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { WorkspaceQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface';
import { UpdateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service';
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';

@WorkspaceQueryHook(`workflowVersion.updateOne`)
export class WorkflowVersionUpdateOnePreQueryHook
implements WorkspaceQueryHookInstance
{
constructor(
private readonly workflowVersionValidationWorkspaceService: WorkflowVersionValidationWorkspaceService,
) {}

async execute(
_authContext: AuthContext,
_objectName: string,
payload: UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>,
): Promise<UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>> {
await this.workflowVersionValidationWorkspaceService.validateWorkflowVersionForUpdateOne(
payload,
);

return payload;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CustomException } from 'src/utils/custom-exception';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a relative import path for better maintainability


export class WorkflowVersionValidationException extends CustomException {
constructor(message: string, code: WorkflowVersionValidationExceptionCode) {
super(message, code);
}
}

export enum WorkflowVersionValidationExceptionCode {
FORBIDDEN = 'FORBIDDEN',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Injectable } from '@nestjs/common';

import { UpdateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface';

import {
WorkflowVersionValidationException,
WorkflowVersionValidationExceptionCode,
} from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.exception';
import {
WorkflowVersionStatus,
WorkflowVersionWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Injectable()
export class WorkflowVersionValidationWorkspaceService {
constructor(
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
) {}

async validateWorkflowVersionForUpdateOne(
payload: UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>,
) {
const workflowVersion =
await this.workflowCommonWorkspaceService.getWorkflowVersionOrFail(
payload.id,
);

if (workflowVersion.status !== WorkflowVersionStatus.DRAFT) {
throw new WorkflowVersionValidationException(
'Only draft workflow versions can be updated',
WorkflowVersionValidationExceptionCode.FORBIDDEN,
);
}

if (payload.data.status !== workflowVersion.status) {
throw new WorkflowVersionValidationException(
'Cannot update workflow version status manually',
WorkflowVersionValidationExceptionCode.FORBIDDEN,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';

import { WorkflowQueryHookModule } from 'src/modules/workflow/common/query-hooks/workflow-query-hook.module';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service';

@Module({
imports: [WorkflowQueryHookModule],
providers: [WorkflowCommonWorkspaceService],
exports: [WorkflowCommonWorkspaceService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export class WorkflowCommonWorkspaceService {
trigger: WorkflowTrigger;
}
> {
if (!workflowVersionId) {
throw new WorkflowTriggerException(
'Workflow version ID is required',
WorkflowTriggerExceptionCode.INVALID_INPUT,
);
}

const workflowVersionRepository =
await this.twentyORMManager.getRepository<WorkflowVersionWorkspaceEntity>(
'workflowVersion',
Expand Down
Loading