Skip to content

Commit 0c94893

Browse files
committed
perf(files_sharing): Move events to listener classes and registration instead of boot
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 37c89f4 commit 0c94893

5 files changed

Lines changed: 158 additions & 64 deletions

File tree

apps/files_sharing/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
'OCA\\Files_Sharing\\Hooks' => $baseDir . '/../lib/Hooks.php',
5757
'OCA\\Files_Sharing\\ISharedMountPoint' => $baseDir . '/../lib/ISharedMountPoint.php',
5858
'OCA\\Files_Sharing\\ISharedStorage' => $baseDir . '/../lib/ISharedStorage.php',
59+
'OCA\\Files_Sharing\\Listener\\BeforeDirectFileDownloadListener' => $baseDir . '/../lib/Listener/BeforeDirectFileDownloadListener.php',
60+
'OCA\\Files_Sharing\\Listener\\BeforeZipCreatedListener' => $baseDir . '/../lib/Listener/BeforeZipCreatedListener.php',
5961
'OCA\\Files_Sharing\\Listener\\LoadAdditionalListener' => $baseDir . '/../lib/Listener/LoadAdditionalListener.php',
6062
'OCA\\Files_Sharing\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
6163
'OCA\\Files_Sharing\\Listener\\ShareInteractionListener' => $baseDir . '/../lib/Listener/ShareInteractionListener.php',

apps/files_sharing/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class ComposerStaticInitFiles_Sharing
7171
'OCA\\Files_Sharing\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
7272
'OCA\\Files_Sharing\\ISharedMountPoint' => __DIR__ . '/..' . '/../lib/ISharedMountPoint.php',
7373
'OCA\\Files_Sharing\\ISharedStorage' => __DIR__ . '/..' . '/../lib/ISharedStorage.php',
74+
'OCA\\Files_Sharing\\Listener\\BeforeDirectFileDownloadListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeDirectFileDownloadListener.php',
75+
'OCA\\Files_Sharing\\Listener\\BeforeZipCreatedListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeZipCreatedListener.php',
7476
'OCA\\Files_Sharing\\Listener\\LoadAdditionalListener' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalListener.php',
7577
'OCA\\Files_Sharing\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
7678
'OCA\\Files_Sharing\\Listener\\ShareInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/ShareInteractionListener.php',

apps/files_sharing/lib/AppInfo/Application.php

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
use OCA\Files_Sharing\External\Manager;
3939
use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
4040
use OCA\Files_Sharing\Helper;
41+
use OCA\Files_Sharing\Listener\BeforeDirectFileDownloadListener;
42+
use OCA\Files_Sharing\Listener\BeforeZipCreatedListener;
4143
use OCA\Files_Sharing\Listener\LoadAdditionalListener;
4244
use OCA\Files_Sharing\Listener\LoadSidebarListener;
4345
use OCA\Files_Sharing\Listener\ShareInteractionListener;
@@ -51,7 +53,6 @@
5153
use OCA\Files_Sharing\Notification\Notifier;
5254
use OCA\Files_Sharing\ShareBackend\File;
5355
use OCA\Files_Sharing\ShareBackend\Folder;
54-
use OCA\Files_Sharing\ViewOnly;
5556
use OCP\AppFramework\App;
5657
use OCP\AppFramework\Bootstrap\IBootContext;
5758
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -62,13 +63,11 @@
6263
use OCP\Files\Config\IMountProviderCollection;
6364
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
6465
use OCP\Files\Events\BeforeZipCreatedEvent;
65-
use OCP\Files\IRootFolder;
6666
use OCP\Group\Events\GroupChangedEvent;
6767
use OCP\Group\Events\GroupDeletedEvent;
6868
use OCP\Group\Events\UserAddedEvent;
6969
use OCP\IDBConnection;
7070
use OCP\IGroup;
71-
use OCP\IUserSession;
7271
use OCP\Share\Events\ShareCreatedEvent;
7372
use OCP\User\Events\UserChangedEvent;
7473
use OCP\User\Events\UserDeletedEvent;
@@ -108,12 +107,22 @@ function () use ($c) {
108107
$context->registerEventListener(UserDeletedEvent::class, DisplayNameCache::class);
109108
$context->registerEventListener(GroupChangedEvent::class, GroupDisplayNameCache::class);
110109
$context->registerEventListener(GroupDeletedEvent::class, GroupDisplayNameCache::class);
110+
111+
// sidebar and files scripts
112+
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
113+
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
114+
$context->registerEventListener(ShareCreatedEvent::class, ShareInteractionListener::class);
115+
$context->registerEventListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
116+
$context->registerEventListener(UserAddedEvent::class, UserAddedToGroupListener::class);
117+
118+
// Handle download events for view only checks
119+
$context->registerEventListener(BeforeZipCreatedEvent::class, BeforeZipCreatedListener::class);
120+
$context->registerEventListener(BeforeDirectFileDownloadEvent::class, BeforeDirectFileDownloadListener::class);
111121
}
112122

113123
public function boot(IBootContext $context): void {
114124
$context->injectFn([$this, 'registerMountProviders']);
115125
$context->injectFn([$this, 'registerEventsScripts']);
116-
$context->injectFn([$this, 'registerDownloadEvents']);
117126

118127
Helper::registerHooks();
119128

@@ -128,12 +137,6 @@ public function registerMountProviders(IMountProviderCollection $mountProviderCo
128137
}
129138

130139
public function registerEventsScripts(IEventDispatcher $dispatcher): void {
131-
// sidebar and files scripts
132-
$dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
133-
$dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
134-
$dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
135-
$dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
136-
$dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
137140
$dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () {
138141
\OCP\Util::addScript('files_sharing', 'collaboration');
139142
});
@@ -159,58 +162,4 @@ public function registerEventsScripts(IEventDispatcher $dispatcher): void {
159162
$listener->userAddedToGroup($event);
160163
});
161164
}
162-
163-
public function registerDownloadEvents(
164-
IEventDispatcher $dispatcher,
165-
IUserSession $userSession,
166-
IRootFolder $rootFolder
167-
): void {
168-
169-
$dispatcher->addListener(
170-
BeforeDirectFileDownloadEvent::class,
171-
function (BeforeDirectFileDownloadEvent $event) use ($userSession, $rootFolder): void {
172-
$pathsToCheck = [$event->getPath()];
173-
// Check only for user/group shares. Don't restrict e.g. share links
174-
$user = $userSession->getUser();
175-
if ($user) {
176-
$viewOnlyHandler = new ViewOnly(
177-
$rootFolder->getUserFolder($user->getUID())
178-
);
179-
if (!$viewOnlyHandler->check($pathsToCheck)) {
180-
$event->setSuccessful(false);
181-
$event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
182-
}
183-
}
184-
}
185-
);
186-
187-
$dispatcher->addListener(
188-
BeforeZipCreatedEvent::class,
189-
function (BeforeZipCreatedEvent $event) use ($userSession, $rootFolder): void {
190-
$dir = $event->getDirectory();
191-
$files = $event->getFiles();
192-
193-
$pathsToCheck = [];
194-
foreach ($files as $file) {
195-
$pathsToCheck[] = $dir . '/' . $file;
196-
}
197-
198-
// Check only for user/group shares. Don't restrict e.g. share links
199-
$user = $userSession->getUser();
200-
if ($user) {
201-
$viewOnlyHandler = new ViewOnly(
202-
$rootFolder->getUserFolder($user->getUID())
203-
);
204-
if (!$viewOnlyHandler->check($pathsToCheck)) {
205-
$event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
206-
$event->setSuccessful(false);
207-
} else {
208-
$event->setSuccessful(true);
209-
}
210-
} else {
211-
$event->setSuccessful(true);
212-
}
213-
}
214-
);
215-
}
216165
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @author John Molakvoæ <skjnldsv@protonmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Files_Sharing\Listener;
28+
29+
use OCA\Files_Sharing\ViewOnly;
30+
use OCP\EventDispatcher\Event;
31+
use OCP\EventDispatcher\IEventListener;
32+
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
33+
use OCP\Files\IRootFolder;
34+
use OCP\IUserSession;
35+
36+
/**
37+
* @template-implements IEventListener<BeforeDirectFileDownloadEvent|Event>
38+
*/
39+
class BeforeDirectFileDownloadListener implements IEventListener {
40+
41+
public function __construct(
42+
private IUserSession $userSession,
43+
private IRootFolder $rootFolder,
44+
) {
45+
}
46+
47+
public function handle(Event $event): void {
48+
if (!($event instanceof BeforeDirectFileDownloadEvent)) {
49+
return;
50+
}
51+
52+
$pathsToCheck = [$event->getPath()];
53+
// Check only for user/group shares. Don't restrict e.g. share links
54+
$user = $this->userSession->getUser();
55+
if ($user) {
56+
$viewOnlyHandler = new ViewOnly(
57+
$this->rootFolder->getUserFolder($user->getUID())
58+
);
59+
if (!$viewOnlyHandler->check($pathsToCheck)) {
60+
$event->setSuccessful(false);
61+
$event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
62+
}
63+
}
64+
}
65+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @author John Molakvoæ <skjnldsv@protonmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Files_Sharing\Listener;
28+
29+
use OCA\Files_Sharing\ViewOnly;
30+
use OCP\EventDispatcher\Event;
31+
use OCP\EventDispatcher\IEventListener;
32+
use OCP\Files\Events\BeforeZipCreatedEvent;
33+
use OCP\Files\IRootFolder;
34+
use OCP\IUserSession;
35+
36+
/**
37+
* @template-implements IEventListener<BeforeZipCreatedEvent|Event>
38+
*/
39+
class BeforeZipCreatedListener implements IEventListener {
40+
41+
public function __construct(
42+
private IUserSession $userSession,
43+
private IRootFolder $rootFolder,
44+
) {
45+
}
46+
47+
public function handle(Event $event): void {
48+
if (!($event instanceof BeforeZipCreatedEvent)) {
49+
return;
50+
}
51+
52+
$dir = $event->getDirectory();
53+
$files = $event->getFiles();
54+
55+
$pathsToCheck = [];
56+
foreach ($files as $file) {
57+
$pathsToCheck[] = $dir . '/' . $file;
58+
}
59+
60+
// Check only for user/group shares. Don't restrict e.g. share links
61+
$user = $this->userSession->getUser();
62+
if ($user) {
63+
$viewOnlyHandler = new ViewOnly(
64+
$this->rootFolder->getUserFolder($user->getUID())
65+
);
66+
if (!$viewOnlyHandler->check($pathsToCheck)) {
67+
$event->setErrorMessage('Access to this resource or one of its sub-items has been denied.');
68+
$event->setSuccessful(false);
69+
} else {
70+
$event->setSuccessful(true);
71+
}
72+
} else {
73+
$event->setSuccessful(true);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)