feat(workspace-resolver): prevent deletion of demo workspaces (#2207) (#3068)

* feat(workspace-resolver): prevent deletion of demo workspaces (#2207)

* ForbiddenException instead of Error

* Optimize user and workspace deletion checks and clarify exception messages (#2207)

- ForbiddenException messages for attempts to delete users and workspaces associated with demo accounts
This commit is contained in:
Ruslan
2023-12-20 20:52:44 +07:00
committed by GitHub
parent d70cb23f30
commit 351dc6488c
2 changed files with 25 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { Resolver, Query, Args, Mutation } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import { ForbiddenException, UseGuards } from '@nestjs/common';
import { FileUpload, GraphQLUpload } from 'graphql-upload';
@ -11,6 +11,7 @@ import { AuthWorkspace } from 'src/decorators/auth-workspace.decorator';
import { assert } from 'src/utils/assert';
import { JwtAuthGuard } from 'src/guards/jwt.auth.guard';
import { UpdateWorkspaceInput } from 'src/core/workspace/dtos/update-workspace-input';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
import { Workspace } from './workspace.entity';
@ -22,6 +23,7 @@ export class WorkspaceResolver {
constructor(
private readonly workspaceService: WorkspaceService,
private readonly fileUploadService: FileUploadService,
private readonly environmentService: EnvironmentService,
) {}
@Query(() => Workspace)
@ -67,6 +69,13 @@ export class WorkspaceResolver {
@Mutation(() => Workspace)
async deleteCurrentWorkspace(@AuthWorkspace() { id }: Workspace) {
const demoWorkspaceIds = this.environmentService.getDemoWorkspaceIds();
// Check if the id is in the list of demo workspaceIds
if (demoWorkspaceIds.includes(id)) {
throw new ForbiddenException('Demo workspaces cannot be deleted.');
}
return this.workspaceService.deleteWorkspace(id);
}
}