diff options
author | John Molakvoæ <skjnldsv@protonmail.com> | 2022-04-29 12:54:25 +0300 |
---|---|---|
committer | John Molakvoæ <skjnldsv@protonmail.com> | 2022-04-30 14:40:27 +0300 |
commit | 24c5d994c7652f266f62563f11bab55defc41dae (patch) | |
tree | 584ac1f2b3cb3292690fe4c2e767bd6212601f6b /apps/theming/lib | |
parent | 77db6ced432953e2f36db28f7a981dbe997e7055 (diff) |
Allow to override the default theme
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/theming/lib')
-rw-r--r-- | apps/theming/lib/Controller/UserThemeController.php | 42 | ||||
-rw-r--r-- | apps/theming/lib/Service/ThemesService.php | 8 | ||||
-rw-r--r-- | apps/theming/lib/Settings/Personal.php | 10 |
3 files changed, 46 insertions, 14 deletions
diff --git a/apps/theming/lib/Controller/UserThemeController.php b/apps/theming/lib/Controller/UserThemeController.php index ec379d2e6fa..71d78db4b3d 100644 --- a/apps/theming/lib/Controller/UserThemeController.php +++ b/apps/theming/lib/Controller/UserThemeController.php @@ -30,9 +30,11 @@ declare(strict_types=1); */ namespace OCA\Theming\Controller; +use OCA\Theming\ITheme; use OCA\Theming\Service\ThemesService; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCS\OCSBadRequestException; +use OCP\AppFramework\OCS\OCSForbiddenException; use OCP\AppFramework\OCSController; use OCP\IConfig; use OCP\IRequest; @@ -71,17 +73,10 @@ class UserThemeController extends OCSController { * @throws OCSBadRequestException|PreConditionNotMetException */ public function enableTheme(string $themeId): DataResponse { - if ($themeId === '' || !$themeId) { - throw new OCSBadRequestException('Invalid theme id: ' . $themeId); - } + $theme = $this->validateTheme($themeId); - $themes = $this->themesService->getThemes(); - if (!isset($themes[$themeId])) { - throw new OCSBadRequestException('Invalid theme id: ' . $themeId); - } - // Enable selected theme - $this->themesService->enableTheme($themes[$themeId]); + $this->themesService->enableTheme($theme); return new DataResponse(); } @@ -95,6 +90,23 @@ class UserThemeController extends OCSController { * @throws OCSBadRequestException|PreConditionNotMetException */ public function disableTheme(string $themeId): DataResponse { + $theme = $this->validateTheme($themeId); + + // Enable selected theme + $this->themesService->disableTheme($theme); + return new DataResponse(); + } + + /** + * Validate and return the matching ITheme + * + * Disable theme + * + * @param string $themeId the theme ID + * @return ITheme + * @throws OCSBadRequestException|PreConditionNotMetException + */ + private function validateTheme(string $themeId): ITheme { if ($themeId === '' || !$themeId) { throw new OCSBadRequestException('Invalid theme id: ' . $themeId); } @@ -103,9 +115,13 @@ class UserThemeController extends OCSController { if (!isset($themes[$themeId])) { throw new OCSBadRequestException('Invalid theme id: ' . $themeId); } - - // Enable selected theme - $this->themesService->disableTheme($themes[$themeId]); - return new DataResponse(); + + // If trying to toggle another theme but this is enforced + if ($this->config->getSystemValueString('enforce_theme', '') !== '' + && $themes[$themeId]->getType() === ITheme::TYPE_THEME) { + throw new OCSForbiddenException('Theme switching is disabled'); + } + + return $themes[$themeId]; } } diff --git a/apps/theming/lib/Service/ThemesService.php b/apps/theming/lib/Service/ThemesService.php index 43977721e76..283b2e9c9ee 100644 --- a/apps/theming/lib/Service/ThemesService.php +++ b/apps/theming/lib/Service/ThemesService.php @@ -155,8 +155,14 @@ class ThemesService { return []; } + $enforcedTheme = $this->config->getSystemValueString('enforce_theme', ''); + $enabledThemes = json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]')); + if ($enforcedTheme !== '') { + return array_merge([$enforcedTheme], $enabledThemes); + } + try { - return json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]')); + return $enabledThemes; } catch (\Exception $e) { return []; } diff --git a/apps/theming/lib/Settings/Personal.php b/apps/theming/lib/Settings/Personal.php index 6dd865b9cf6..790c0fd7f39 100644 --- a/apps/theming/lib/Settings/Personal.php +++ b/apps/theming/lib/Settings/Personal.php @@ -25,6 +25,7 @@ */ namespace OCA\Theming\Settings; +use OCA\Theming\ITheme; use OCA\Theming\Service\ThemesService; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; @@ -54,6 +55,8 @@ class Personal implements ISettings { } public function getForm(): TemplateResponse { + $enforcedTheme = $this->config->getSystemValueString('enforce_theme', ''); + $themes = array_map(function($theme) { return [ 'id' => $theme->getId(), @@ -65,7 +68,14 @@ class Personal implements ISettings { ]; }, $this->themesService->getThemes()); + if ($enforcedTheme !== '') { + $themes = array_filter($themes, function($theme) use ($enforcedTheme) { + return $theme['type'] !== ITheme::TYPE_THEME || $theme['id'] === $enforcedTheme; + }); + } + $this->initialStateService->provideInitialState('themes', array_values($themes)); + $this->initialStateService->provideInitialState('enforceTheme', $enforcedTheme); Util::addScript($this->appName, 'theming-settings'); return new TemplateResponse($this->appName, 'settings-personal'); |