From fcbfcd5310b596b8698d5d6f2c9c9da4da987383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Thu, 26 Oct 2023 15:06:22 +0200 Subject: [PATCH] chore: Move away from app.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- appinfo/app.php | 52 ------------------- lib/AppInfo/Application.php | 75 ++++++++++++++++++---------- lib/Listener/FilesScriptListener.php | 12 +++++ lib/Listener/LoadViewerListener.php | 15 ++++++ 4 files changed, 75 insertions(+), 79 deletions(-) delete mode 100644 appinfo/app.php create mode 100644 lib/Listener/FilesScriptListener.php create mode 100644 lib/Listener/LoadViewerListener.php diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100644 index 90b40faf4..000000000 --- a/appinfo/app.php +++ /dev/null @@ -1,52 +0,0 @@ -. - * - */ - -namespace OCA\Officeonline\AppInfo; - -use OCA\Officeonline\PermissionManager; - -$currentUser = \OC::$server->getUserSession()->getUser(); -if ($currentUser !== null) { - /** @var PermissionManager $permissionManager */ - $permissionManager = \OC::$server->query(PermissionManager::class); - if (!$permissionManager->isEnabledForUser($currentUser)) { - return; - } -} - -$eventDispatcher = \OC::$server->getEventDispatcher(); -$eventDispatcher->addListener( - 'OCA\Files::loadAdditionalScripts', - function () { - \OCP\Util::addScript('officeonline', 'files', 'viewer'); - } -); -$eventDispatcher->addListener( - 'OCA\Files_Sharing::loadAdditionalScripts', - function () { - \OCP\Util::addScript('officeonline', 'files', 'viewer'); - } -); - -$app = \OC::$server->query(Application::class); -$app->registerProvider(); -$app->updateCSP(); diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 749515404..081ccc7c4 100755 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -27,9 +27,14 @@ use OC\Files\Type\Detection; use OC\Security\CSP\ContentSecurityPolicy; use OCA\Federation\TrustedServers; +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\Files_Sharing\Listener\LoadAdditionalListener; use OCA\Officeonline\Capabilities; use OCA\Officeonline\Hooks\WopiLockHooks; +use OCA\Officeonline\Listener\FilesScriptListener; +use OCA\Officeonline\Listener\LoadViewerListener; use OCA\Officeonline\Middleware\WOPIMiddleware; +use OCA\Officeonline\PermissionManager; use OCA\Officeonline\Preview\MSExcel; use OCA\Officeonline\Preview\MSWord; use OCA\Officeonline\Preview\OOXML; @@ -38,44 +43,46 @@ use OCA\Officeonline\Service\FederationService; use OCA\Viewer\Event\LoadViewer; use OCP\AppFramework\App; -use OCP\AppFramework\QueryException; -use OCP\EventDispatcher\IEventDispatcher; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\IPreview; use Psr\Log\LoggerInterface; -class Application extends App { +class Application extends App implements IBootstrap { public const APP_ID = 'officeonline'; - /** - * Strips the path and query parameters from the URL. - * - * @param string $url - * @return string - */ - private function domainOnly(string $url): string { - $parsed_url = parse_url(trim($url)); - $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; - $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; - $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; - return "$scheme$host$port"; - } - public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); + } - try { - /** @var IEventDispatcher $eventDispatcher */ - $eventDispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class); - if (class_exists(LoadViewer::class)) { - $eventDispatcher->addListener(LoadViewer::class, function () { - \OCP\Util::addScript('officeonline', 'viewer'); - }); + public function register(IRegistrationContext $context): void { + $context->registerCapability(Capabilities::class); + $context->registerMiddleWare(WOPIMiddleware::class); + $context->registerEventListener(LoadAdditionalScriptsEvent::class, FilesScriptListener::class); + $context->registerEventListener(LoadAdditionalListener::class, FilesScriptListener::class); + $context->registerEventListener(LoadViewer::class, LoadViewerListener::class); + } + + public function boot(IBootContext $context): void { + if (!$this->isEnabled()) { + return; + } + $this->registerProvider(); + $this->updateCSP(); + } + + public function isEnabled(): bool { + $currentUser = \OC::$server->getUserSession()->getUser(); + if ($currentUser !== null) { + /** @var PermissionManager $permissionManager */ + $permissionManager = \OCP\Server::get(PermissionManager::class); + if (!$permissionManager->isEnabledForUser($currentUser)) { + return false; } - } catch (QueryException $e) { } - $this->getContainer()->registerCapability(Capabilities::class); - $this->getContainer()->registerMiddleWare(WOPIMiddleware::class); + return true; } public function registerProvider() { @@ -159,4 +166,18 @@ public function updateCSP() { $cspManager->addDefaultPolicy($policy); } + + /** + * Strips the path and query parameters from the URL. + * + * @param string $url + * @return string + */ + private function domainOnly(string $url): string { + $parsed_url = parse_url(trim($url)); + $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; + $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; + $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; + return "$scheme$host$port"; + } } diff --git a/lib/Listener/FilesScriptListener.php b/lib/Listener/FilesScriptListener.php new file mode 100644 index 000000000..36e7c9590 --- /dev/null +++ b/lib/Listener/FilesScriptListener.php @@ -0,0 +1,12 @@ +