Skip to content

Commit

Permalink
TASK: Introduce WorkspaceService::deleteWorkspace
Browse files Browse the repository at this point in the history
to also removed assigned metadata and roles
  • Loading branch information
mhsdesign committed Oct 31, 2024
1 parent 92eed8f commit 74dd00b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
7 changes: 1 addition & 6 deletions Neos.Neos/Classes/Command/WorkspaceCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Neos\Neos\Command;

use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Exception\WorkspaceAlreadyExists;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Command\DeleteWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Dto\RebaseErrorHandlingStrategy;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Exception\WorkspaceRebaseFailed;
use Neos\ContentRepository\Core\Service\WorkspaceMaintenanceServiceFactory;
Expand Down Expand Up @@ -416,11 +415,7 @@ public function deleteCommand(string $workspace, bool $force = false, string $co
$this->workspacePublishingService->discardAllWorkspaceChanges($contentRepositoryId, $workspaceName);
}

$contentRepositoryInstance->handle(
DeleteWorkspace::create(
$workspaceName
)
);
$this->workspaceService->deleteWorkspace($contentRepositoryId, $workspaceName);
$this->outputLine('Deleted workspace "%s"', [$workspaceName->value]);
}

Expand Down
69 changes: 69 additions & 0 deletions Neos.Neos/Classes/Domain/Service/WorkspaceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Command\CreateRootWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Command\CreateWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Exception\WorkspaceAlreadyExists;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Command\DeleteWorkspace;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
Expand Down Expand Up @@ -195,6 +196,21 @@ public function createPersonalWorkspaceForUserIfMissing(ContentRepositoryId $con
);
}

public function deleteWorkspace(ContentRepositoryId $contentRepositoryId, WorkspaceName $workspaceName): void
{
$contentRepository = $this->contentRepositoryRegistry->get($contentRepositoryId);
$this->requireWorkspace($contentRepositoryId, $workspaceName);

$contentRepository->handle(
DeleteWorkspace::create(
$workspaceName,
)
);

$this->deleteWorkspaceMetadata($contentRepositoryId, $workspaceName);
$this->deleteWorkspaceRoleAssignments($contentRepositoryId, $workspaceName);
}

/**
* Assign a workspace role to the given user/user group
*
Expand Down Expand Up @@ -515,6 +531,59 @@ private function loadWorkspaceRoleOfUser(ContentRepositoryId $contentRepositoryI
return WorkspaceRole::from($role);
}

private function deleteWorkspaceMetadata(ContentRepositoryId $contentRepositoryId, $workspaceName): void
{
$table = self::TABLE_NAME_WORKSPACE_METADATA;
$query = <<<SQL
DELETE FROM
{$table}
WHERE
content_repository_id = :contentRepositoryId
AND workspace_name = :workspaceName
SQL;

try {
$this->dbal->executeStatement($query, [
'contentRepositoryId' => $contentRepositoryId->value,
'workspaceName' => $workspaceName->value,
]);
} catch (DbalException $e) {
throw new \RuntimeException(sprintf(
'Failed to delete metadata for workspace "%s" (Content Repository "%s"): %s',
$workspaceName->value,
$contentRepositoryId->value,
$e->getMessage()
), 1726821159, $e);
}
}


private function deleteWorkspaceRoleAssignments(ContentRepositoryId $contentRepositoryId, WorkspaceName $workspaceName): void
{
$table = self::TABLE_NAME_WORKSPACE_ROLE;
$query = <<<SQL
DELETE FROM
{$table}
WHERE
content_repository_id = :contentRepositoryId
AND workspace_name = :workspaceName
SQL;

try {
$this->dbal->executeStatement($query, [
'contentRepositoryId' => $contentRepositoryId->value,
'workspaceName' => $workspaceName->value,
]);
} catch (DbalException $e) {
throw new \RuntimeException(sprintf(
'Failed to delete role assignments for workspace "%s" (Content Repository "%s"): %s',
$workspaceName->value,
$contentRepositoryId->value,
$e->getMessage()
), 1726821159, $e);
}
}

private function requireWorkspace(ContentRepositoryId $contentRepositoryId, WorkspaceName $workspaceName): Workspace
{
$workspace = $this->contentRepositoryRegistry
Expand Down
7 changes: 1 addition & 6 deletions Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Exception\WorkspaceAlreadyExists;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Command\DeleteWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Command\DiscardIndividualNodesFromWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Command\PublishIndividualNodesFromWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Dto\NodeIdsToPublishOrDiscard;
Expand Down Expand Up @@ -405,11 +404,7 @@ public function deleteAction(WorkspaceName $workspaceName): void
$this->redirect('index');
}

$contentRepository->handle(
DeleteWorkspace::create(
$workspaceName,
)
);
$this->workspaceService->deleteWorkspace($contentRepositoryId, $workspaceName);

$this->addFlashMessage($this->translator->translateById(
'workspaces.workspaceHasBeenRemoved',
Expand Down

0 comments on commit 74dd00b

Please sign in to comment.