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

[stable29] fix: prevent don't delete file when folder is deleted #4060

Merged
merged 1 commit into from
Nov 26, 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
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\User\Events\UserDeletedEvent;

Expand Down Expand Up @@ -75,6 +76,7 @@ public function register(IRegistrationContext $context): void {

$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
$context->registerEventListener(BeforeNodeDeletedEvent::class, BeforeNodeDeletedListener::class);
$context->registerEventListener(CacheEntryRemovedEvent::class, BeforeNodeDeletedListener::class);
$context->registerEventListener(SignedEvent::class, SignedListener::class);

// Files newFile listener
Expand Down
47 changes: 24 additions & 23 deletions lib/Listener/BeforeNodeDeletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\File;
use OCP\IDBConnection;

/**
* @template-implements IEventListener<Event|BeforeNodeDeletedEvent>
* @template-implements IEventListener<Event|BeforeNodeDeletedEvent|CacheEntryRemovedEvent>
*/
class BeforeNodeDeletedListener implements IEventListener {
public function __construct(
Expand All @@ -46,17 +47,25 @@ public function __construct(
}

public function handle(Event $event): void {
if (!$event instanceof BeforeNodeDeletedEvent) {
if ($event instanceof BeforeNodeDeletedEvent) {
$node = $event->getNode();
if (!$node instanceof File) {
return;
}
if (!in_array($node->getMimeType(), ValidateHelper::VALID_MIMETIPE)) {
return;
}
$nodeId = $node->getId();
$this->delete($nodeId);
return;
}
$node = $event->getNode();
if (!$node instanceof File) {
return;
}
if (!in_array($node->getMimeType(), ValidateHelper::VALID_MIMETIPE)) {
return;
if ($event instanceof CacheEntryRemovedEvent) {
$this->delete($event->getFileId());
}
$nodeId = $node->getId();
return;
}

private function delete(int $nodeId): void {
$type = $this->fileMapper->getFileType($nodeId);
if ($type === 'not_libresign_file') {
return;
Expand All @@ -69,24 +78,16 @@ public function handle(Event $event): void {
break;
case 'file':
$libresignFile = $this->fileMapper->getByFileId($nodeId);
if ($libresignFile->getStatus() === $libresignFile::STATUS_SIGNED) {
$libresignFile->setNodeId(null);
$this->fileMapper->update($libresignFile);
break;
}
$this->requestSignatureService->deleteRequestSignature(['file' => ['fileId' => $nodeId]]);
$this->fileMapper->delete($libresignFile);
break;
case 'user_element':
case 'file_element':
$this->deleteByType($nodeId, $type);
$field = $type === 'file' ? 'node_id' : 'file_id';
$qb = $this->db->getQueryBuilder();
$qb->delete('libresign_' . $type)
->where($qb->expr()->eq($field, $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)))
->executeStatement();
}
}

private function deleteByType(int $nodeId, string $type): void {
$field = $type === 'file' ? 'node_id' : 'file_id';
$qb = $this->db->getQueryBuilder();
$qb->delete('libresign_' . $type)
->where($qb->expr()->eq($field, $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)))
->executeStatement();
}
}
5 changes: 4 additions & 1 deletion src/Components/Request/VisibleElements.vue
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,11 @@ export default {
}))
},
async goToSign() {
// after save, the document is no more acessible by this way,
// this is the reason to retain the UUID before save action
const uuid = this.document.settings.signerFileUuid
if (await this.save()) {
const route = this.$router.resolve({ name: 'SignPDF', params: { uuid: this.document.settings.signerFileUuid } })
const route = this.$router.resolve({ name: 'SignPDF', params: { uuid } })
window.location.href = route.href
}
},
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Controller/AEnvironmentPageAwareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ public function testLoadFileUuidWhenFileNotFound(): void {
$this->getExpectedExceptionCode(404);
$this->expectExceptionMessage(json_encode([
'action' => 2000,
'errors' => ['File not found'],
'errors' => ['Invalid UUID'],
]));

$signers = $this->getSignersFromFileId($file->getId());
$this->controller->loadNextcloudFileFromSignRequestUuid($signers[0]->getUuid());
$this->controller->validateSignRequestUuid($file->getUuid());
}
}
Loading