|
9 | 9 |
|
10 | 10 | use OCA\DAV\Connector\Sabre\FilesPlugin; |
11 | 11 | use OCA\DAV\Connector\Sabre\Node; |
12 | | -use OCA\Richdocuments\AppConfig; |
13 | 12 | use OCA\Richdocuments\Middleware\WOPIMiddleware; |
14 | | -use OCA\Richdocuments\PermissionManager; |
| 13 | +use OCA\Richdocuments\Service\SecureViewService; |
15 | 14 | use OCA\Richdocuments\Storage\SecureViewWrapper; |
16 | 15 | use OCP\Files\ForbiddenException; |
17 | 16 | use OCP\Files\NotFoundException; |
18 | | -use OCP\Files\Storage\ISharedStorage; |
19 | | -use OCP\Files\Storage\IStorage; |
| 17 | +use OCP\Files\StorageNotAvailableException; |
20 | 18 | use OCP\IAppConfig; |
21 | | -use OCP\IUserSession; |
22 | 19 | use Sabre\DAV\INode; |
23 | 20 | use Sabre\DAV\PropFind; |
24 | 21 | use Sabre\DAV\Server; |
|
27 | 24 | class SecureViewPlugin extends ServerPlugin { |
28 | 25 | public function __construct( |
29 | 26 | protected WOPIMiddleware $wopiMiddleware, |
30 | | - protected IUserSession $userSession, |
31 | | - protected PermissionManager $permissionManager, |
32 | 27 | protected IAppConfig $appConfig, |
| 28 | + protected SecureViewService $secureViewService, |
33 | 29 | ) { |
34 | 30 | } |
35 | 31 |
|
36 | 32 | public function initialize(Server $server) { |
37 | | - if ($this->appConfig->getValueString(AppConfig::WATERMARK_APP_NAMESPACE, 'watermark_enabled', 'no') === 'no') { |
| 33 | + if (!$this->secureViewService->isEnabled()) { |
38 | 34 | return; |
39 | 35 | } |
40 | 36 | $server->on('propFind', $this->handleGetProperties(...)); |
@@ -64,49 +60,19 @@ private function handleGetProperties(PropFind $propFind, INode $node): void { |
64 | 60 | } |
65 | 61 |
|
66 | 62 | private function isDownloadable(\OCP\Files\Node $node): bool { |
67 | | - try { |
68 | | - $this->checkFileAccess($node->getInternalPath(), $node->getStorage()); |
| 63 | + $storage = $node->getStorage(); |
| 64 | + if ($this->wopiMiddleware->isWOPIRequest() |
| 65 | + || $storage === null |
| 66 | + || !$storage->instanceOfStorage(SecureViewWrapper::class) |
| 67 | + ) { |
69 | 68 | return true; |
70 | | - } catch (ForbiddenException) { |
71 | | - return false; |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - private function checkFileAccess(string $path, IStorage $storage): void { |
76 | | - if ($this->wopiMiddleware->isWOPIRequest() || !$storage->instanceOfStorage(SecureViewWrapper::class)) { |
77 | | - return; |
78 | | - } |
79 | | - |
80 | | - if ($this->shouldSecure($path, $storage)) { |
81 | | - throw new ForbiddenException('Download blocked due the secure view policy', false); |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - // FIXME: remove duplication by moving into a SecureViewService |
86 | | - private function shouldSecure(string $path, ?IStorage $sourceStorage = null): bool { |
87 | | - if ($sourceStorage !== $this && $sourceStorage !== null) { |
88 | | - $fp = $sourceStorage->fopen($path, 'r'); |
89 | | - fclose($fp); |
90 | 69 | } |
91 | 70 |
|
92 | | - $storage = $sourceStorage ?? $this; |
93 | | - $cacheEntry = $storage->getCache()->get($path); |
94 | | - if (!$cacheEntry) { |
95 | | - $parent = dirname($path); |
96 | | - if ($parent === '.') { |
97 | | - $parent = ''; |
98 | | - } |
99 | | - $cacheEntry = $storage->getCache()->get($parent); |
100 | | - if (!$cacheEntry) { |
101 | | - throw new NotFoundException(sprintf('Could not find cache entry for path and parent of %s within storage %s ', $path, $storage->getId())); |
102 | | - } |
| 71 | + try { |
| 72 | + return !$this->secureViewService->shouldSecure($node->getInternalPath(), $storage); |
| 73 | + } catch (StorageNotAvailableException|ForbiddenException|NotFoundException $e) { |
| 74 | + // Exceptions cannot be nicely inferred. |
| 75 | + return false; |
103 | 76 | } |
104 | | - |
105 | | - $isSharedStorage = $storage->instanceOfStorage(ISharedStorage::class); |
106 | | - |
107 | | - $share = $isSharedStorage ? $storage->getShare() : null; |
108 | | - $userId = $this->userSession->getUser()?->getUID(); |
109 | | - |
110 | | - return $this->permissionManager->shouldWatermark($cacheEntry, $userId, $share, $storage->getOwner($path) ?: null); |
111 | 77 | } |
112 | 78 | } |
0 commit comments