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 39ad3da commit 59aeaae
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 @@ -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());
}
}

0 comments on commit 59aeaae

Please sign in to comment.