nextcloud/apps/files_versions/lib/BlockVersioningOperation.php
Ferdinand Thiessen 4e816f70d1 feat(files_versions): allow to block version creation using WFE
This allows users to create workflows to block the versions creation
for some files, based on tags or other conditions using the workflow
engine.
The usecase would be compliance to allow configure some rules to prevent
versioning.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
2026-06-01 16:06:46 +00:00

95 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
/*!
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Versions;
use OCA\Files_Versions\Events\CreateVersionEvent;
use OCA\WorkflowEngine\Entity\File as FileEntity;
use OCP\EventDispatcher\Event;
use OCP\Files\Folder;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\WorkflowEngine\IComplexOperation;
use OCP\WorkflowEngine\IManager;
use OCP\WorkflowEngine\IRuleMatcher;
use OCP\WorkflowEngine\ISpecificOperation;
use Psr\Log\LoggerInterface;
class BlockVersioningOperation implements ISpecificOperation, IComplexOperation {
public function __construct(
private readonly IL10N $l10n,
private readonly FileEntity $fileEntity,
private readonly LoggerInterface $logger,
private readonly IURLGenerator $urlGenerator,
) {
}
#[\Override]
public function getEntityId(): string {
return FileEntity::class;
}
#[\Override]
public function getDisplayName(): string {
return $this->l10n->t('Block file versioning');
}
#[\Override]
public function getDescription(): string {
return $this->l10n->t('Automatic tag based blocking of file version creation.');
}
#[\Override]
public function getIcon(): string {
return $this->urlGenerator->imagePath('files_versions', 'app.svg');
}
#[\Override]
public function isAvailableForScope(int $scope): bool {
return $scope === IManager::SCOPE_ADMIN;
}
#[\Override]
public function validateOperation(string $name, array $checks, string $operation): void {
if (empty($checks)) {
throw new \UnexpectedValueException($this->l10n->t('No rule given'));
}
}
#[\Override]
public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
if ($eventName !== CreateVersionEvent::class || !($event instanceof CreateVersionEvent)) {
return;
}
$node = $event->getNode();
$path = $node->getInternalPath();
$ruleMatcher->setFileInfo(
$node->getStorage(),
$path,
$node instanceof Folder,
);
$ruleMatcher->setEntitySubject($this->fileEntity, $node);
$ruleMatcher->setOperation($this);
$flows = $ruleMatcher->getFlows();
if ($flows !== []) {
$this->logger->debug('Blocking version creation due to matching workflow rules', [
'path' => $path,
]);
$event->disableVersions();
}
}
#[\Override]
public function getTriggerHint(): string {
return $this->l10n->t('A new version is created'); // TRANSLATORS: This will be shown as "When: " "A new version is created"
}
}