Skip to content

Commit

Permalink
fixup! enh: add backend check for download permission for cloud attac…
Browse files Browse the repository at this point in the history
…hements

Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Jun 3, 2024
1 parent 6648bce commit 0e71d88
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/Service/Attachment/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@ private function handleForwardedAttachment(Account $account, array $attachment,
return $localAttachment->getId();
}

private function hasDownloadPermissions(File $file, string $fileName): bool {
$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: ".$fileName);
return false;

}
}
return true;
}

/**
* @param Account $account
* @param array $attachment
Expand All @@ -363,19 +380,8 @@ private function handleCloudAttachment(Account $account, array $attachment): ?in
if (!$file instanceof File) {
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;

}
if(!$this->hasDownloadPermissions($file, $fileName)) {
return null;
}

try {
Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/Service/Attachment/AttachmentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\Folder;
use OCP\Files\NotPermittedException;
use OCP\Share\IAttributes;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -390,6 +392,56 @@ public function testHandleAttachmentsForwardedAttachment(): void {
$this->service->handleAttachments($account, [$attachments], $client);
}

public function testHandleAttachmentsCloudAttachmentNoDownloadPermission(): void {
$userId = 'linus';
$storage = $this->createMock(SharedStorage::class);
$storage->expects(self::once())
->method('instanceOfStorage')
->with(SharedStorage::class)
->willReturn(true);
$share = $this->createMock(IShare::class);
$attributes = $this->createMock(IAttributes::class);
$attributes->expects(self::once())
->method('getAttribute')
->with('permissions', 'download')
->willReturn(false);
$share->expects(self::once())
->method('getAttributes')
->willReturn($attributes);
$storage->expects(self::once())
->method('getShare')
->willReturn($share);

$file = $this->createConfiguredMock(File::class, [
'getName' => 'cat.jpg',
'getMimeType' => 'text/plain',
'getContent' => 'sjdhfkjsdhfkjsdhfkjdshfjhdskfjhds',
'getStorage' => $storage
]);
$account = $this->createConfiguredMock(Account::class, [
'getUserId' => $userId
]);
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$attachments = [
'type' => 'cloud',
'messageId' => 999,
'fileName' => 'cat.jpg',
'mimeType' => 'text/plain',
];
$this->userFolder->expects(self::once())
->method('nodeExists')
->with('cat.jpg')
->willReturn(true);
$this->userFolder->expects(self::once())
->method('get')
->with('cat.jpg')
->willReturn($file);

$result = $this->service->handleAttachments($account, [$attachments], $client);
$this->assertEquals([], $result);

}

public function testHandleAttachmentsCloudAttachment(): void {
$userId = 'linus';
$file = $this->createConfiguredMock(File::class, [
Expand Down

0 comments on commit 0e71d88

Please sign in to comment.