Skip to content

Commit c47507c

Browse files
blizzzelzody
authored andcommitted
fix(Wopi): fall back to super share if share token is not available
On internal shares the controller is called without the share token. But necessary information, like share attributes, might be necessary to know and are available from the super share of the SharedStorage in that case. For this approach was used elsewhere, too, some repetitive code was consolidated in the Helper class. Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 9075f51 commit c47507c

5 files changed

Lines changed: 47 additions & 42 deletions

File tree

lib/Controller/AssetsController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace OCA\Richdocuments\Controller;
88

9-
use OCA\Files_Sharing\SharedStorage;
109
use OCA\Richdocuments\Controller\Attribute\RestrictToWopiServer;
1110
use OCA\Richdocuments\Db\AssetMapper;
11+
use OCA\Richdocuments\Helper;
1212
use OCA\Richdocuments\Service\UserScopeService;
1313
use OCP\AppFramework\Controller;
1414
use OCP\AppFramework\Db\DoesNotExistException;
@@ -36,6 +36,7 @@ public function __construct(
3636
private IURLGenerator $urlGenerator,
3737
private IManager $taskProcessingManager,
3838
private IL10N $l10n,
39+
private Helper $helper,
3940
) {
4041
parent::__construct($appName, $request);
4142
}
@@ -57,14 +58,12 @@ public function create($path) {
5758
return new JSONResponse([], Http::STATUS_NOT_FOUND);
5859
}
5960

60-
$storage = $node->getStorage();
61-
if ($storage->instanceOfStorage(SharedStorage::class)) {
62-
/** @var SharedStorage $storage */
63-
$share = $storage->getShare();
64-
$attributes = $share->getAttributes();
65-
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
66-
throw new NotPermittedException();
67-
}
61+
$share = $this->helper->getShareFromNode($node);
62+
$attributes = $share?->getAttributes();
63+
if ($attributes !== null
64+
&& $attributes->getAttribute('permissions', 'download') === false
65+
) {
66+
throw new NotPermittedException();
6867
}
6968
} catch (NotFoundException) {
7069
return new JSONResponse([], Http::STATUS_NOT_FOUND);

lib/Controller/WopiController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function __construct(
9292
private TaskProcessingManager $taskProcessingManager,
9393
private SettingsService $settingsService,
9494
private CapabilitiesService $capabilitiesService,
95+
private Helper $helper,
9596
) {
9697
parent::__construct($appName, $request);
9798
}
@@ -219,7 +220,7 @@ public function checkFileInfo(string $fileId, string $access_token): JSONRespons
219220
$response['TemplateSource'] = $this->getWopiUrlForTemplate($wopi);
220221
}
221222

222-
$share = $this->getShareForWopiToken($wopi);
223+
$share = $this->getShareForWopiToken($wopi, $file);
223224
if ($this->permissionManager->shouldWatermark($file, $wopi->getEditorUid(), $share)) {
224225
$email = $user !== null && !$isPublic ? $user->getEMailAddress() : '';
225226
$currentDateTime = new \DateTime(
@@ -925,9 +926,13 @@ private function getFileForWopiToken(Wopi $wopi) {
925926
return array_shift($files);
926927
}
927928

928-
private function getShareForWopiToken(Wopi $wopi): ?IShare {
929+
private function getShareForWopiToken(Wopi $wopi, File $file): ?IShare {
929930
try {
930-
return $wopi->getShare() ? $this->shareManager->getShareByToken($wopi->getShare()) : null;
931+
$shareToken = $wopi->getShare();
932+
if ($shareToken) {
933+
return $this->shareManager->getShareByToken($shareToken);
934+
}
935+
return $this->helper->getShareFromNode($file);
931936
} catch (ShareNotFound) {
932937
}
933938

lib/Helper.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
use DateTime;
99
use DateTimeZone;
10+
use OCA\Files_Sharing\SharedStorage;
1011
use OCP\Files\Folder;
12+
use OCP\Files\Node;
13+
use OCP\Files\NotFoundException;
14+
use OCP\Share\IShare;
1115

1216
class Helper {
1317
/**
@@ -82,4 +86,17 @@ public function getGuestNameFromCookie() {
8286
}
8387
return $_COOKIE['guestUser'];
8488
}
89+
90+
public function getShareFromNode(Node $node): ?IShare {
91+
try {
92+
$storage = $node->getStorage();
93+
} catch (NotFoundException) {
94+
return null;
95+
}
96+
if ($storage->instanceOfStorage(SharedStorage::class)) {
97+
/** @var SharedStorage $storage */
98+
return $storage->getShare();
99+
}
100+
return null;
101+
}
85102
}

lib/Listener/BeforeFetchPreviewListener.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OCA\Richdocuments\Listener;
1212

13-
use OCA\Files_Sharing\SharedStorage;
13+
use OCA\Richdocuments\Helper;
1414
use OCA\Richdocuments\PermissionManager;
1515
use OCP\EventDispatcher\Event;
1616
use OCP\EventDispatcher\IEventListener;
@@ -20,7 +20,6 @@
2020
use OCP\Preview\BeforePreviewFetchedEvent;
2121
use OCP\Share\Exceptions\ShareNotFound;
2222
use OCP\Share\IManager;
23-
use OCP\Share\IShare;
2423

2524
/** @template-implements IEventListener<Event|BeforePreviewFetchedEvent> */
2625
class BeforeFetchPreviewListener implements IEventListener {
@@ -29,6 +28,7 @@ public function __construct(
2928
private IUserSession $userSession,
3029
private IRequest $request,
3130
private IManager $shareManager,
31+
private Helper $helper,
3232
) {
3333
}
3434

@@ -38,20 +38,12 @@ public function handle(Event $event): void {
3838
}
3939
$shareToken = $this->request->getParam('token');
4040

41-
$share = null;
42-
43-
// Get share for internal shares
44-
$storage = $event->getNode()->getStorage();
45-
if (!$shareToken && $storage->instanceOfStorage(SharedStorage::class)) {
46-
if (method_exists(IShare::class, 'getAttributes')) {
47-
/** @var SharedStorage $storage */
48-
$share = $storage->getShare();
49-
}
50-
}
51-
52-
// Get different share for public previews as the share from the node is only set for mounted shares
5341
try {
54-
$share = $shareToken ? $this->shareManager->getShareByToken($shareToken) : $share;
42+
$share = $shareToken ?
43+
// Get different share for public previews as the share from the node is only set for mounted shares
44+
$this->shareManager->getShareByToken($shareToken)
45+
// Get share for internal shares
46+
: $this->helper->getShareFromNode($event->getNode());
5547
} catch (ShareNotFound) {
5648
}
5749

lib/TokenManager.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
namespace OCA\Richdocuments;
88

99
use Exception;
10-
use OCA\Files_Sharing\SharedStorage;
1110
use OCA\Richdocuments\Db\Direct;
1211
use OCA\Richdocuments\Db\Wopi;
1312
use OCA\Richdocuments\Db\WopiMapper;
@@ -24,7 +23,6 @@
2423
use OCP\IURLGenerator;
2524
use OCP\Share\Exceptions\ShareNotFound;
2625
use OCP\Share\IManager;
27-
use OCP\Share\IShare;
2826
use OCP\Util;
2927
use Psr\Log\LoggerInterface;
3028

@@ -83,19 +81,13 @@ public function generateWopiToken(string $fileId, ?string $shareToken = null, ?s
8381

8482
// disable download if at least one shared access has it disabled
8583
foreach ($files as $file) {
86-
$storage = $file->getStorage();
87-
// using string as we have no guarantee that "files_sharing" app is loaded
88-
if ($storage->instanceOfStorage(SharedStorage::class)) {
89-
if (!method_exists(IShare::class, 'getAttributes')) {
90-
break;
91-
}
92-
/** @var SharedStorage $storage */
93-
$share = $storage->getShare();
94-
$attributes = $share->getAttributes();
95-
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
96-
$hideDownload = true;
97-
break;
98-
}
84+
$share = $this->helper->getShareFromNode($file);
85+
$attributes = $share?->getAttributes();
86+
if ($attributes !== null
87+
&& $attributes->getAttribute('permissions', 'download') === false
88+
) {
89+
$hideDownload = true;
90+
break;
9991
}
10092
}
10193
}

0 commit comments

Comments
 (0)