From 7672152579447d1cc465a41d1b90004045236c29 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 14 Sep 2022 16:39:00 +0200 Subject: Add a global setting to disable keyboard shortcuts Signed-off-by: Joas Schilling --- apps/theming/lib/AppInfo/Application.php | 5 ++ .../lib/Listener/BeforePreferenceListener.php | 56 ++++++++++++++++++++++ .../Listener/BeforeTemplateRenderedListener.php | 13 +++++ apps/theming/src/UserThemes.vue | 51 +++++++++++++++++++- dist/theming-theming-settings.js | 4 +- dist/theming-theming-settings.js.map | 2 +- 6 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 apps/theming/lib/Listener/BeforePreferenceListener.php diff --git a/apps/theming/lib/AppInfo/Application.php b/apps/theming/lib/AppInfo/Application.php index 0f567b4d0e4..48bf42252c7 100644 --- a/apps/theming/lib/AppInfo/Application.php +++ b/apps/theming/lib/AppInfo/Application.php @@ -25,12 +25,15 @@ namespace OCA\Theming\AppInfo; use OCA\Theming\Capabilities; +use OCA\Theming\Listener\BeforePreferenceListener; use OCA\Theming\Listener\BeforeTemplateRenderedListener; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\Config\BeforePreferenceDeletedEvent; +use OCP\Config\BeforePreferenceSetEvent; class Application extends App implements IBootstrap { public const APP_ID = 'theming'; @@ -42,6 +45,8 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerCapability(Capabilities::class); $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); + $context->registerEventListener(BeforePreferenceSetEvent::class, BeforePreferenceListener::class); + $context->registerEventListener(BeforePreferenceDeletedEvent::class, BeforePreferenceListener::class); } public function boot(IBootContext $context): void { diff --git a/apps/theming/lib/Listener/BeforePreferenceListener.php b/apps/theming/lib/Listener/BeforePreferenceListener.php new file mode 100644 index 00000000000..a1add86e600 --- /dev/null +++ b/apps/theming/lib/Listener/BeforePreferenceListener.php @@ -0,0 +1,56 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Theming\Listener; + +use OCA\Theming\AppInfo\Application; +use OCP\Config\BeforePreferenceDeletedEvent; +use OCP\Config\BeforePreferenceSetEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +class BeforePreferenceListener implements IEventListener { + public function handle(Event $event): void { + if (!$event instanceof BeforePreferenceSetEvent + && !$event instanceof BeforePreferenceDeletedEvent) { + return; + } + + if ($event->getAppId() !== Application::APP_ID) { + return; + } + + if ($event->getConfigKey() !== 'shortcuts_disabled') { + return; + } + + if ($event instanceof BeforePreferenceSetEvent) { + $event->setValid($event->getConfigValue() === 'yes'); + return; + } + + $event->setValid(true); + } +} diff --git a/apps/theming/lib/Listener/BeforeTemplateRenderedListener.php b/apps/theming/lib/Listener/BeforeTemplateRenderedListener.php index d6e00b927ae..1d28a1d4399 100644 --- a/apps/theming/lib/Listener/BeforeTemplateRenderedListener.php +++ b/apps/theming/lib/Listener/BeforeTemplateRenderedListener.php @@ -29,6 +29,8 @@ use OCA\Theming\AppInfo\Application; use OCA\Theming\Service\BackgroundService; use OCA\Theming\Service\JSDataService; use OCA\Theming\Service\ThemeInjectionService; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; @@ -64,6 +66,17 @@ class BeforeTemplateRenderedListener implements IEventListener { fn () => $this->container->get(JSDataService::class), ); + /** @var BeforeTemplateRenderedEvent $event */ + if ($event->getResponse()->getRenderAs() === TemplateResponse::RENDER_AS_USER) { + $this->initialState->provideLazyInitialState('shortcutsDisabled', function () { + if ($this->userSession->getUser()) { + $uid = $this->userSession->getUser()->getUID(); + return $this->config->getUserValue($uid, Application::APP_ID, 'shortcuts_disabled', 'no') === 'yes'; + } + return false; + }); + } + $this->themeInjectionService->injectHeaders(); $user = $this->userSession->getUser(); diff --git a/apps/theming/src/UserThemes.vue b/apps/theming/src/UserThemes.vue index c886394136a..da4495158f3 100644 --- a/apps/theming/src/UserThemes.vue +++ b/apps/theming/src/UserThemes.vue @@ -23,7 +23,9 @@