Skip to content

Commit

Permalink
enh: add backend check for download permission for cloud attachements
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed May 27, 2024
1 parent 7766fe3 commit 6648bce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/Service/Attachment/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use finfo;
use InvalidArgumentException;
use OCA\Files_Sharing\SharedStorage;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IAttachmentService;
use OCA\Mail\Contracts\IMailManager;
Expand Down Expand Up @@ -363,6 +364,20 @@ private function handleCloudAttachment(Account $account, array $attachment): ?in
return null;
}

$storage = $file->getStorage();
if ($storage->instanceOfStorage(SharedStorage::class)) {

/** @var SharedStorage $storage */
$share = $storage->getShare();
$attributes = $share->getAttributes();

if($attributes->getAttribute('permissions', 'download') === false) {
$this->logger->warning("Could not create attachment, no download permission for file: ".$attachment['fileName']);
return null;

}
}

try {
$localAttachment = $this->addFileFromString($account->getUserId(), $file->getName(), $file->getMimeType(), $file->getContent());
} catch (UploadException $e) {
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Service/Attachment/AttachmentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use ChristophWurst\Nextcloud\Testing\TestCase;
use Horde_Imap_Client_Socket;
use OC\Files\Node\File;
use OCA\Files_Sharing\SharedStorage;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\LocalAttachment;
Expand Down Expand Up @@ -394,7 +395,8 @@ public function testHandleAttachmentsCloudAttachment(): void {
$file = $this->createConfiguredMock(File::class, [
'getName' => 'cat.jpg',
'getMimeType' => 'text/plain',
'getContent' => 'sjdhfkjsdhfkjsdhfkjdshfjhdskfjhds'
'getContent' => 'sjdhfkjsdhfkjsdhfkjdshfjhdskfjhds',
'getStorage' => $this->createMock(SharedStorage::class)
]);
$account = $this->createConfiguredMock(Account::class, [
'getUserId' => $userId
Expand Down
8 changes: 8 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
<code>IAPIWidgetV2</code>
</UndefinedClass>
</file>
<file src="lib/Service/Attachment/AttachmentService.php">
<UndefinedDocblockClass occurrences="2">
<code>OCA\Files_Sharing\SharedStorage</code>
</UndefinedDocblockClass>
<UndefinedClass occurrences="1">
<code>SharedStorage</code>
</UndefinedClass>
</file>
<file src="lib/Dashboard/UnreadMailWidgetV2.php">
<MissingDependency>
<code>MailWidgetV2</code>
Expand Down

0 comments on commit 6648bce

Please sign in to comment.