From d5223bc90d8821dd2a0820184fbf38376b2b5d63 Mon Sep 17 00:00:00 2001 From: Kai Henseler Date: Mon, 2 Dec 2024 14:41:34 +0100 Subject: [PATCH] feat(files/user-config): make configs admin-configurable Signed-off-by: Kai Henseler --- apps/files/lib/Service/UserConfig.php | 11 +++- apps/files/tests/Service/UserConfigTest.php | 62 +++++++++++++++++++-- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/apps/files/lib/Service/UserConfig.php b/apps/files/lib/Service/UserConfig.php index 1cec11c67b452..c16640d867f61 100644 --- a/apps/files/lib/Service/UserConfig.php +++ b/apps/files/lib/Service/UserConfig.php @@ -6,6 +6,7 @@ namespace OCA\Files\Service; use OCA\Files\AppInfo\Application; +use OCP\AppFramework\Services\IAppConfig; use OCP\IConfig; use OCP\IUser; use OCP\IUserSession; @@ -54,6 +55,7 @@ class UserConfig { public function __construct( protected IConfig $config, IUserSession $userSession, + protected IAppConfig $appConfig, ) { $this->user = $userSession->getUser(); } @@ -146,6 +148,13 @@ public function getConfigs(): array { return $value; }, $this->getAllowedConfigKeys()); - return array_combine($this->getAllowedConfigKeys(), $userConfigs); + $userConfigsMerged = array_combine($this->getAllowedConfigKeys(), $userConfigs); + + // override user configs with app configs + $configs = array_map(function (string $key) use ($userConfigsMerged) { + return $this->appConfig->getAppValueBool($key, $userConfigsMerged[$key]); + }, $this->getAllowedConfigKeys()); + + return array_combine($this->getAllowedConfigKeys(), $configs); } } diff --git a/apps/files/tests/Service/UserConfigTest.php b/apps/files/tests/Service/UserConfigTest.php index 8c3bdfd0ec3f0..fb078568ed4fc 100644 --- a/apps/files/tests/Service/UserConfigTest.php +++ b/apps/files/tests/Service/UserConfigTest.php @@ -9,6 +9,7 @@ use OCA\Files\AppInfo\Application; use OCA\Files\Service\UserConfig; +use OCP\AppFramework\Services\IAppConfig; use OCP\IConfig; use OCP\IUser; use OCP\IUserSession; @@ -34,6 +35,9 @@ class UserConfigTest extends \Test\TestCase { /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ private $userSessionMock; + /** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */ + private $appConfigMock; + /** * @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */ @@ -42,6 +46,7 @@ class UserConfigTest extends \Test\TestCase { protected function setUp(): void { parent::setUp(); $this->configMock = $this->createMock(IConfig::class); + $this->appConfigMock = $this->createMock(IAppConfig::class); $this->userUID = static::getUniqueID('user_id-'); \OC::$server->getUserManager()->createUser($this->userUID, 'test'); @@ -67,6 +72,7 @@ protected function getUserConfigService(array $methods = []) { ->setConstructorArgs([ $this->configMock, $this->userSessionMock, + $this->appConfigMock, ]) ->setMethods($methods) ->getMock(); @@ -100,7 +106,7 @@ public function testThrowsExceptionWhenNoUserLoggedInForSetConfig(): void { $this->expectException(\Exception::class); $this->expectExceptionMessage('No user logged in'); - $userConfig = new UserConfig($this->configMock, $this->userSessionMock); + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); $userConfig->setConfig('crop_image_previews', true); } @@ -108,7 +114,7 @@ public function testThrowsInvalidArgumentExceptionForUnknownConfigKey(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Unknown config key'); - $userConfig = new UserConfig($this->configMock, $this->userSessionMock); + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); $userConfig->setConfig('unknown_key', true); } @@ -116,7 +122,7 @@ public function testThrowsInvalidArgumentExceptionForInvalidConfigValue(): void $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Invalid config value'); - $userConfig = new UserConfig($this->configMock, $this->userSessionMock); + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); $userConfig->setConfig('crop_image_previews', 'foo'); } @@ -124,7 +130,7 @@ public function testSetsConfigSuccessfully(): void { $this->configMock->expects($this->once()) ->method('setUserValue') ->with($this->userUID, Application::APP_ID, 'crop_image_previews', '1'); - $userConfig = new UserConfig($this->configMock, $this->userSessionMock); + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); $userConfig->setConfig('crop_image_previews', true); } @@ -135,7 +141,13 @@ public function testGetsConfigsWithDefaultValuesSuccessfully(): void { return $default; }); - $userConfig = new UserConfig($this->configMock, $this->userSessionMock); + // pass the default app settings unchanged + $this->appConfigMock->method('getAppValueBool') + ->willReturnCallback(function ($key, $default) { + return $default; + }); + + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); $configs = $userConfig->getConfigs(); $this->assertEquals([ 'crop_image_previews' => true, @@ -162,7 +174,13 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void { return $default; }); - $userConfig = new UserConfig($this->configMock, $this->userSessionMock); + // pass the default app settings unchanged + $this->appConfigMock->method('getAppValueBool') + ->willReturnCallback(function ($key, $default) { + return $default; + }); + + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); $configs = $userConfig->getConfigs(); $this->assertEquals([ 'crop_image_previews' => false, @@ -173,4 +191,36 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void { 'folder_tree' => true, ], $configs); } + + public function testGetsConfigsOverrideWithAppsValuesSuccessfully(): void { + $this->userSessionMock->method('getUser')->willReturn($this->userMock); + + // set all user values to true + $this->configMock->method('getUserValue') + ->willReturnCallback(function () { + return true; + }); + + // emulate override by the app config values + $this->appConfigMock->method('getAppValueBool') + ->willReturnCallback(function ($key, $default) { + if ($key === 'crop_image_previews') { + return false; + } elseif ($key === 'show_hidden') { + return false; + } + return $default; + }); + + $userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock); + $configs = $userConfig->getConfigs(); + $this->assertEquals([ + 'crop_image_previews' => false, + 'show_hidden' => false, + 'sort_favorites_first' => true, + 'sort_folders_first' => true, + 'grid_view' => true, + 'folder_tree' => true, + ], $configs); + } }