Skip to content

Commit

Permalink
fix: Proper error message based on file permissions
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jan 17, 2024
1 parent 9145e9e commit f668505
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $t
} elseif ($fileId) {
try {
$file = $this->documentService->getFileById($fileId);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
} catch (NotFoundException|NotPermittedException $e) {
$this->logger->error('No permission to access this file', [ 'exception' => $e ]);
return new DataResponse($this->l10n->t('No permission to access this file.'), Http::STATUS_NOT_FOUND);
}
} else {
return new DataResponse('No valid file argument provided', Http::STATUS_PRECONDITION_FAILED);
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ public function getFileForSession(Session $session, ?string $shareToken = null):

/**
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getFileById(int $fileId, ?string $userId = null): File {
$userId = $userId ?? $this->userId;
Expand Down Expand Up @@ -504,6 +505,10 @@ public function getFileById(int $fileId, ?string $userId = null): File {
throw new NotFoundException();
}

if (($file->getPermissions() & Constants::PERMISSION_READ) !== Constants::PERMISSION_READ) {
throw new NotPermittedException();
}

return $file;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
:has-connection-issue="hasConnectionIssue"
@reconnect="reconnect" />

<SkeletonLoading v-if="!contentLoaded" />
<SkeletonLoading v-if="!contentLoaded && !displayedStatus" />
<Wrapper v-if="displayed"
:sync-error="syncError"
:has-connection-issue="hasConnectionIssue"
Expand Down
104 changes: 104 additions & 0 deletions tests/unit/Service/DocumentServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace OCA\Text\Tests;

use OCA\Text\Db\DocumentMapper;
use OCA\Text\Db\SessionMapper;
use OCA\Text\Db\StepMapper;
use OCA\Text\Service\DocumentService;
use OCP\Constants;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Folder;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\Lock\ILockManager;
use OCP\Files\NotPermittedException;
use OCP\ICacheFactory;
use OCP\IRequest;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

class DocumentServiceTest extends \PHPUnit\Framework\TestCase {
private DocumentService $documentService;

private DocumentMapper $documentMapper;
private StepMapper $setpMapper;
private SessionMapper $sessionMapper;
private IAppData $appData;
private string $userId;
private IRootFolder $rootFolder;
private ICacheFactory $cacheFactory;
private LoggerInterface $loggerInterface;
private IManager $shareManager;
private IRequest $request;
private \OCP\DirectEditing\IManager $directManager;
private ILockManager $lockManager;
private IUserMountCache $userMountCache;

public function setUp(): void {
$this->documentMapper = $this->createMock(DocumentMapper::class);
$this->setpMapper = $this->createMock(StepMapper::class);
$this->sessionMapper = $this->createMock(SessionMapper::class);
$this->appData = $this->createMock(IAppData::class);
$this->userId = 'admin';
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->loggerInterface = $this->createMock(LoggerInterface::class);
$this->shareManager = $this->createMock(IManager::class);
$this->request = $this->createMock(IRequest::class);
$this->directManager = $this->createMock(\OCP\DirectEditing\IManager::class);
$this->lockManager = $this->createMock(ILockManager::class);
$this->userMountCache = $this->createMock(IUserMountCache::class);

$this->documentService = new DocumentService(
$this->documentMapper,
$this->setpMapper,
$this->sessionMapper,
$this->appData,
$this->userId,
$this->rootFolder,
$this->cacheFactory,
$this->loggerInterface,
$this->shareManager,
$this->request,
$this->directManager,
$this->lockManager,
$this->userMountCache,
);
}

public function testGetFileById() {
$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

$file = $this->createMock(\OCP\Files\File::class);
$file->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
$userFolder->method('getById')->willReturn([$file]);
$actual = $this->documentService->getFileById(1234);
self::assertEquals($file, $actual);
}

public function testGetFileByIdSortUpdatableFirst() {
$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

$file1 = $this->createMock(\OCP\Files\File::class);
$file1->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
$file2 = $this->createMock(\OCP\Files\File::class);
$file2->method('getPermissions')->willReturn(Constants::PERMISSION_READ & Constants::PERMISSION_UPDATE);
$userFolder->method('getById')->willReturn([$file1, $file2]);
$actual = $this->documentService->getFileById(1234);
self::assertEquals($file2, $actual);
}

public function testGetFileByIdNoRead() {
$userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

$file = $this->createMock(\OCP\Files\File::class);
$file->method('getPermissions')->willReturn(Constants::PERMISSION_UPDATE);
$userFolder->method('getById')->willReturn([$file]);
$this->expectException(NotPermittedException::class);
$actual = $this->documentService->getFileById(1234);
}
}

0 comments on commit f668505

Please sign in to comment.