Skip to content

Commit f673671

Browse files
committed
feat(files/user-config): make configs admin-configurable
Signed-off-by: Kai Henseler <[email protected]>
1 parent 45386eb commit f673671

2 files changed

Lines changed: 68 additions & 7 deletions

File tree

apps/files/lib/Service/UserConfig.php

Lines changed: 12 additions & 1 deletion
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;
@@ -50,12 +51,15 @@ class UserConfig {
5051
],
5152
];
5253
protected ?IUser $user = null;
54+
protected IAppConfig $appConfig;
5355

5456
public function __construct(
5557
protected IConfig $config,
5658
IUserSession $userSession,
59+
IAppConfig $appConfig
5760
) {
5861
$this->user = $userSession->getUser();
62+
$this->appConfig = $appConfig;
5963
}
6064

6165
/**
@@ -146,6 +150,13 @@ public function getConfigs(): array {
146150
return $value;
147151
}, $this->getAllowedConfigKeys());
148152

149-
return array_combine($this->getAllowedConfigKeys(), $userConfigs);
153+
$userConfigsMerged = array_combine($this->getAllowedConfigKeys(), $userConfigs);
154+
155+
// override user configs with app configs
156+
$configs = array_map(function (string $key) use ($userConfigsMerged) {
157+
return $this->appConfig->getAppValueBool($key, $userConfigsMerged[$key]);
158+
}, $this->getAllowedConfigKeys());
159+
160+
return array_combine($this->getAllowedConfigKeys(), $configs);
150161
}
151162
}

apps/files/tests/Service/UserConfigTest.php

Lines changed: 56 additions & 6 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,31 +106,31 @@ 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

115121
public function testThrowsInvalidArgumentExceptionForInvalidConfigValue(): void {
116122
$this->expectException(\InvalidArgumentException::class);
117123
$this->expectExceptionMessage('Invalid config value');
118124

119-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
125+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
120126
$userConfig->setConfig('crop_image_previews', 'foo');
121127
}
122128

123129
public function testSetsConfigSuccessfully(): void {
124130
$this->configMock->expects($this->once())
125131
->method('setUserValue')
126132
->with($this->userUID, Application::APP_ID, 'crop_image_previews', '1');
127-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
133+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
128134
$userConfig->setConfig('crop_image_previews', true);
129135
}
130136

@@ -135,7 +141,13 @@ public function testGetsConfigsWithDefaultValuesSuccessfully(): void {
135141
return $default;
136142
});
137143

138-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
144+
// pass the default app settings unchanged
145+
$this->appConfigMock->method('getAppValueBool')
146+
->willReturnCallback(function ($key, $default) {
147+
return $default;
148+
});
149+
150+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
139151
$configs = $userConfig->getConfigs();
140152
$this->assertEquals([
141153
'crop_image_previews' => true,
@@ -162,7 +174,13 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
162174
return $default;
163175
});
164176

165-
$userConfig = new UserConfig($this->configMock, $this->userSessionMock);
177+
// pass the default app settings unchanged
178+
$this->appConfigMock->method('getAppValueBool')
179+
->willReturnCallback(function ($key, $default) {
180+
return $default;
181+
});
182+
183+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
166184
$configs = $userConfig->getConfigs();
167185
$this->assertEquals([
168186
'crop_image_previews' => false,
@@ -173,4 +191,36 @@ public function testGetsConfigsOverrideWithUserValuesSuccessfully(): void {
173191
'folder_tree' => true,
174192
], $configs);
175193
}
194+
195+
public function testGetsConfigsOverrideWithAppsValuesSuccessfully(): void {
196+
$this->userSessionMock->method('getUser')->willReturn($this->userMock);
197+
198+
// set all user values to true
199+
$this->configMock->method('getUserValue')
200+
->willReturnCallback(function () {
201+
return true;
202+
});
203+
204+
// emulate override by the app config values
205+
$this->appConfigMock->method('getAppValueBool')
206+
->willReturnCallback(function ($key, $default) {
207+
if ($key === 'crop_image_previews') {
208+
return false;
209+
} elseif ($key === 'show_hidden') {
210+
return false;
211+
}
212+
return $default;
213+
});
214+
215+
$userConfig = new UserConfig($this->configMock, $this->userSessionMock, $this->appConfigMock);
216+
$configs = $userConfig->getConfigs();
217+
$this->assertEquals([
218+
'crop_image_previews' => false,
219+
'show_hidden' => false,
220+
'sort_favorites_first' => true,
221+
'sort_folders_first' => true,
222+
'grid_view' => true,
223+
'folder_tree' => true,
224+
], $configs);
225+
}
176226
}

0 commit comments

Comments
 (0)