Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function checkFileInfo(string $fileId, string $access_token): JSONRespons
$isTaskProcessingEnabled = $isSmartPickerEnabled && $this->taskProcessingManager->isTaskProcessingEnabled();

$share = $this->getShareForWopiToken($wopi, $file);
$shouldUseSecureView = $this->permissionManager->shouldWatermark($file, $wopi->getEditorUid(), $share);
$shouldUseSecureView = $this->permissionManager->shouldWatermarkByNode($file, $wopi->getEditorUid(), $share);

// If the file is locked manually by a user we want to open it read only for all others
$canWriteThroughLock = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/BeforeFetchPreviewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle(Event $event): void {
}

$userId = $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null;
if ($this->permissionManager->shouldWatermark($event->getNode(), $userId, $share)) {
if ($this->permissionManager->shouldWatermarkByNode($event->getNode(), $userId, $share)) {
throw new NotFoundException();
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/OverwritePublicSharePropertiesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle(Event $event): void {
return;
}

if ($this->permissionManager->shouldWatermark($node, $this->userId, $share)) {
if ($this->permissionManager->shouldWatermarkByNode($node, $this->userId, $share)) {
$share->setHideDownload(true);
}
}
Expand Down
52 changes: 41 additions & 11 deletions lib/PermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,54 @@ public function userIsFeatureLocked(?string $userId = null): bool {
return false;
}

public function shouldWatermark(Node|ICacheEntry $nodeOrCacheEntry, ?string $userId = null, ?IShare $share = null, ?string $ownerId = null): bool {
public function shouldWatermarkByNode(
Node $node,
?string $userId = null,
?IShare $share = null
): bool {
return $this->shouldWatermark(
$node->getId(),
$node->getMimetype(),
$node->isUpdateable(),
$userId,
$share,
$node->getOwner()?->getUID(),
);
}

public function shouldWatermarkByCacheEntry(
ICacheEntry $cacheEntry,
?string $userId = null,
?IShare $share = null,
?string $ownerId = null
): bool {
return $this->shouldWatermark(
$cacheEntry->getId(),
$cacheEntry->getMimetype(),
(bool)($cacheEntry->getPermissions() & Constants::PERMISSION_UPDATE),
$userId,
$share,
$ownerId,
);
}

protected function shouldWatermark(
int $fileId,
string $mimetype,
bool $isFileUpdatable,
?string $userId,
?IShare $share,
?string $ownerId,
): bool {
if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_enabled', 'no') === 'no') {
return false;
}

if (!in_array($nodeOrCacheEntry->getMimetype(), $this->appConfig->getMimeTypes(), true)) {
if (!in_array($mimetype, $this->appConfig->getMimeTypes(), true)) {
return false;
}

$fileId = $nodeOrCacheEntry->getId();

$isUpdatable = $nodeOrCacheEntry instanceof Node
? $nodeOrCacheEntry->isUpdateable()
: $nodeOrCacheEntry->getPermissions() & Constants::PERMISSION_UPDATE;
$isUpdatable = $isUpdatable && (!$share || $share->getPermissions() & Constants::PERMISSION_UPDATE);
$isUpdatable = $isFileUpdatable && (!$share || $share->getPermissions() & Constants::PERMISSION_UPDATE);

$hasShareAttributes = $share && method_exists($share, 'getAttributes') && $share->getAttributes() instanceof IAttributes;
$isDisabledDownload = $hasShareAttributes && $share->getAttributes()->getAttribute('permissions', 'download') === false;
Expand Down Expand Up @@ -157,9 +190,6 @@ public function shouldWatermark(Node|ICacheEntry $nodeOrCacheEntry, ?string $use
}

if ($this->config->getAppValue(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_shareAll', 'no') === 'yes') {
if (!$ownerId && $nodeOrCacheEntry instanceof Node) {
$ownerId = $nodeOrCacheEntry->getOwner()?->getUID();
}
if ($userId === null || $ownerId !== $userId) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Storage/SecureViewWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function shouldSecure(string $path, ?IStorage $sourceStorage = null): bo
$share = $isSharedStorage ? $storage->getShare() : null;
$userId = $this->userSession->getUser()?->getUID();

return $this->permissionManager->shouldWatermark($cacheEntry, $userId, $share, $storage->getOwner($path) ?: null);
return $this->permissionManager->shouldWatermarkByCacheEntry($cacheEntry, $userId, $share, $storage->getOwner($path) ?: null);
}


Expand Down
42 changes: 38 additions & 4 deletions tests/lib/PermissionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCA\Richdocuments\PermissionManager;
use OCP\Constants;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Node;
use OCP\IConfig;
use OCP\IGroupManager;
Expand Down Expand Up @@ -162,7 +164,7 @@ public function testShouldWatermarkOptionLinkTags(array $objectTagIds, array $wa
$this->systemTagMapper->expects($this->once())->method('getTagIdsForObjects')->willReturn(['testFileId' => $objectTagIds]);
$this->appConfig->expects($this->once())->method('getAppValueArray')->willReturn($watermarkTagIds);

$result = $this->permissionManager->shouldWatermark($node, $userId, $share);
$result = $this->permissionManager->shouldWatermarkByNode($node, $userId, $share);

$this->assertTrue($result);
}
Expand Down Expand Up @@ -190,7 +192,7 @@ public function testShouldWatermarkOptionAllTags(array $objectTagIds, array $wat
$this->systemTagMapper->expects($this->once())->method('getTagIdsForObjects')->willReturn(['testFileId' => $objectTagIds]);
$this->appConfig->expects($this->once())->method('getAppValueArray')->willReturn($watermarkTagIds);

$result = $this->permissionManager->shouldWatermark($node, $userId, null);
$result = $this->permissionManager->shouldWatermarkByNode($node, $userId, null);

$this->assertTrue($result);
}
Expand All @@ -214,19 +216,51 @@ public function testShouldWatermarkOptionTalkPublic(): void {
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkTags', 'no', 'no'],
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_shareTalkPublic', 'no', 'yes'],
]);
$result = $this->permissionManager->shouldWatermark($node, $userId, $share);
$result = $this->permissionManager->shouldWatermarkByNode($node, $userId, $share);

$this->assertTrue($result);
}

public function testShouldWatermarkCacheEntry(): void {
$cacheEntry = $this->createCacheEntryMock();
$share = $this->createShareMock(IShare::TYPE_ROOM);
$userId = null;

$this->appConfig->expects($this->any())
->method('getMimeTypes')
->willReturn(Capabilities::MIMETYPES);

$this->config
->method('getAppValue')
->willReturnMap([
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_enabled', 'no', 'yes'],
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkAll', 'no', 'no'],
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkRead', 'no', 'no'],
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkSecure', 'no', 'no'],
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_linkTags', 'no', 'no'],
[AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_shareTalkPublic', 'no', 'yes'],
]);
$result = $this->permissionManager->shouldWatermarkByCacheEntry($cacheEntry, $userId, $share);

$this->assertTrue($result);
}

private function createNodeMock(): Node {
$node = $this->createMock(Node::class);
$node->expects($this->any())->method('getMimetype')->willReturn('application/vnd.oasis.opendocument.spreadsheet');
$node->expects($this->any())->method('getId')->willReturn('testFileId');
$node->expects($this->any())->method('getId')->willReturn(42);
$node->expects($this->any())->method('isUpdateable')->willReturn(false);
return $node;
}

private function createCacheEntryMock(): ICacheEntry {
$node = $this->createMock(ICacheEntry::class);
$node->expects($this->any())->method('getMimetype')->willReturn('application/vnd.oasis.opendocument.spreadsheet');
$node->expects($this->any())->method('getId')->willReturn(42);
$node->expects($this->any())->method('getPermissions')->willReturn(Constants::PERMISSION_READ);
return $node;
}

private function createShareMock(?int $shareType): ?IShare {
if ($shareType === null) {
return null;
Expand Down
Loading