Skip to content

Commit 5b5ee91

Browse files
committed
feat(files/user-config): make configs admin-configurable
Signed-off-by: Kai Henseler <kai.henseler@strato.de>
1 parent efa9c9d commit 5b5ee91

2 files changed

Lines changed: 69 additions & 7 deletions

File tree

apps/files/lib/Service/UserConfig.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace OCA\Files\Service;
77

88
use OCA\Files\AppInfo\Application;
9+
use OCP\AppFramework\Services\IAppConfig;
910
use OCP\IConfig;
1011
use OCP\IUser;
1112
use OCP\IUserSession;
@@ -54,6 +55,7 @@ class UserConfig {
5455
public function __construct(
5556
protected IConfig $config,
5657
IUserSession $userSession,
58+
protected IAppConfig $appConfig,
5759
) {
5860
$this->user = $userSession->getUser();
5961
}
@@ -143,9 +145,11 @@ public function getConfigs(): array {
143145

144146
$userId = $this->user->getUID();
145147
$userConfigs = array_map(function (string $key) use ($userId) {
146-
$value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key));
147-
// If the default is expected to be a boolean, we need to cast the value
148-
if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
148+
$value = $this->config->getUserValue($userId, Application::APP_ID, $key, null);
149+
if ($value === null) {
150+
$value = $this->appConfig->getAppValueBool($key, $this->getDefaultConfigValue($key));
151+
} else if (is_bool($this->getDefaultConfigValue($key)) && is_string($value)) {
152+
// If the default value is expected to be a boolean, we need to cast the value
149153
return $value === '1';
150154
}
151155
return $value;

apps/files/tests/Service/UserConfigTest.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use OCA\Files\AppInfo\Application;
1111
use OCA\Files\Service\UserConfig;
12+
use OCP\AppFramework\Services\IAppConfig;
1213
use OCP\IConfig;
1314
use OCP\IUser;
1415
use OCP\IUserSession;
@@ -34,6 +35,9 @@ class UserConfigTest extends \Test\TestCase {
3435
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
3536
private $userSessionMock;
3637

38+
/** @var IAppConfig|\PHPUnit\Framework\MockObject\MockObject */
39+
private $appConfigMock;
40+
3741
/**
3842
* @var UserConfig|\PHPUnit\Framework\MockObject\MockObject
3943
*/
@@ -42,6 +46,7 @@ class UserConfigTest extends \Test\TestCase {
4246
protected function setUp(): void {
4347
parent::setUp();
4448
$this->configMock = $this->createMock(IConfig::class);
49+
$this->appConfigMock = $this->createMock(IAppConfig::class);
4550

4651
$this->userUID = static::getUniqueID('user_id-');
4752
\OC::$server->getUserManager()->createUser($this->userUID, 'test');
@@ -67,6 +72,7 @@ protected function getUserConfigService(array $methods = []) {
6772
->setConstructorArgs([
6873
$this->configMock,
6974
$this->userSessionMock,
75+
$this->appConfigMock,
7076
])
7177
->setMethods($methods)
7278
->getMock();
@@ -100,15 +106,15 @@ public function testThrowsExceptionWhenNoUserLoggedInForSetConfig(): void {
100106
$this->expectException(\Exception::class);
101107
$this->expectExceptionMessage('No user logged in');
102108

103-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
109+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
104110
$userConfig->setConfig('crop_image_previews', true);
105111
}
106112

107113
public function testThrowsInvalidArgumentExceptionForUnknownConfigKey(): void {
108114
$this->expectException(\InvalidArgumentException::class);
109115
$this->expectExceptionMessage('Unknown config key');
110116

111-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
117+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
112118
$userConfig->setConfig('unknown_key', true);
113119
}
114120

@@ -125,6 +131,14 @@ public static function validBoolConfigValues(): array {
125131
];
126132
}
127133

134+
public function testThrowsInvalidArgumentExceptionForInvalidConfigValue(): void {
135+
$this->expectException(\InvalidArgumentException::class);
136+
$this->expectExceptionMessage('Invalid config value');
137+
138+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
139+
$userConfig->setConfig('crop_image_previews', 'foo');
140+
}
141+
128142
/**
129143
* @dataProvider validBoolConfigValues
130144
*/
@@ -144,7 +158,13 @@ public function testGetsConfigsWithDefaultValuesSuccessfully(): void {
144158
return $default;
145159
});
146160

147-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
161+
// pass the default app settings unchanged
162+
$this->appConfigMock->method('getAppValueBool')
163+
->willReturnCallback(function ($key, $default) {
164+
return $default;
165+
});
166+
167+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
148168
$configs = $userConfig->getConfigs();
149169
$this->assertEquals([
150170
'crop_image_previews' => true,
@@ -171,7 +191,13 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
171191
return $default;
172192
});
173193

174-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
194+
// pass the default app settings unchanged
195+
$this->appConfigMock->method('getAppValueBool')
196+
->willReturnCallback(function ($key, $default) {
197+
return $default;
198+
});
199+
200+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
175201
$configs = $userConfig->getConfigs();
176202
$this->assertEquals([
177203
'crop_image_previews' => false,
@@ -182,4 +208,36 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
182208
'folder_tree' => true,
183209
], $configs);
184210
}
211+
212+
public function testGetsConfigsOverrideWithAppsValuesSuccessfully(): void {
213+
$this->userSessionMock->method('getUser')->willReturn($this->userMock);
214+
215+
// set all user values to true
216+
$this->configMock->method('getUserValue')
217+
->willReturnCallback(function () {
218+
return true;
219+
});
220+
221+
// emulate override by the app config values
222+
$this->appConfigMock->method('getAppValueBool')
223+
->willReturnCallback(function ($key, $default) {
224+
if ($key === 'crop_image_previews') {
225+
return false;
226+
} elseif ($key === 'show_hidden') {
227+
return false;
228+
}
229+
return $default;
230+
});
231+
232+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
233+
$configs = $userConfig->getConfigs();
234+
$this->assertEquals([
235+
'crop_image_previews' => false,
236+
'show_hidden' => false,
237+
'sort_favorites_first' => true,
238+
'sort_folders_first' => true,
239+
'grid_view' => true,
240+
'folder_tree' => true,
241+
], $configs);
242+
}
185243
}

0 commit comments

Comments
 (0)