Skip to content

Commit

Permalink
fix: prevent don't delete file when folder is deleted
Browse files Browse the repository at this point in the history
When we delete the LibreSign folder and remove from trash, is necessary
to delete LibreSign files too

Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos authored and backportbot-libresign[bot] committed Nov 26, 2024
1 parent 5b5bcb4 commit 2f213ee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 @@ -59,6 +60,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 @@ -14,12 +14,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 @@ -30,17 +31,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 @@ -53,24 +62,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 @@ -260,8 +260,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 @@ -97,10 +97,9 @@ public function testLoadFileUuidWhenFileNotFound(): void {
$this->expectExceptionCode(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());
}
}

0 comments on commit 2f213ee

Please sign in to comment.