|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2022 John Molakvoæ <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author John Molakvoæ <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace OCA\Files\Service; |
|
24
|
|
|
|
|
25
|
|
|
use OCA\Files\AppInfo\Application; |
|
26
|
|
|
use OCP\IConfig; |
|
27
|
|
|
use OCP\IUser; |
|
28
|
|
|
use OCP\IUserSession; |
|
29
|
|
|
|
|
30
|
|
|
class UserConfig { |
|
31
|
|
|
const ALLOWED_CONFIGS = [ |
|
32
|
|
|
[ |
|
33
|
|
|
// Whether to crop the files previews or not in the files list |
|
34
|
|
|
'key' => 'crop_image_previews', |
|
35
|
|
|
'default' => true, |
|
36
|
|
|
'allowed' => [true, false], |
|
37
|
|
|
], |
|
38
|
|
|
[ |
|
39
|
|
|
// Whether to show the hidden files or not in the files list |
|
40
|
|
|
'key' => 'show_hidden', |
|
41
|
|
|
'default' => false, |
|
42
|
|
|
'allowed' => [true, false], |
|
43
|
|
|
], |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
protected IConfig $config; |
|
47
|
|
|
protected ?IUser $user = null; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(IConfig $config, IUserSession $userSession) { |
|
50
|
|
|
$this->config = $config; |
|
51
|
|
|
$this->user = $userSession->getUser(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the list of all allowed user config keys |
|
56
|
|
|
* @return string[] |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getAllowedConfigKeys(): array { |
|
59
|
|
|
return array_map(function($config) { |
|
60
|
|
|
return $config['key']; |
|
61
|
|
|
}, self::ALLOWED_CONFIGS); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the list of allowed config values for a given key |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $key a valid config key |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
private function getAllowedConfigValues(string $key): array { |
|
71
|
|
|
foreach (self::ALLOWED_CONFIGS as $config) { |
|
72
|
|
|
if ($config['key'] === $key) { |
|
73
|
|
|
return $config['allowed']; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return []; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get the default config value for a given key |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $key a valid config key |
|
83
|
|
|
* @return string|bool |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getDefaultConfigValue(string $key) { |
|
86
|
|
|
foreach (self::ALLOWED_CONFIGS as $config) { |
|
87
|
|
|
if ($config['key'] === $key) { |
|
88
|
|
|
return $config['default']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return ''; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set a user config |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $key |
|
98
|
|
|
* @param string|bool $value |
|
99
|
|
|
* @throws \Exception |
|
100
|
|
|
* @throws \InvalidArgumentException |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setConfig(string $key, $value): void { |
|
103
|
|
|
if ($this->user === null) { |
|
104
|
|
|
throw new \Exception('No user logged in'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (!in_array($key, $this->getAllowedConfigKeys())) { |
|
108
|
|
|
throw new \InvalidArgumentException('Unknown config key'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (!in_array($value, $this->getAllowedConfigValues($key))) { |
|
112
|
|
|
throw new \InvalidArgumentException('Invalid config value'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (is_bool($value)) { |
|
116
|
|
|
$value = $value ? '1' : '0'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->config->setUserValue($this->user->getUID(), Application::APP_ID, $key, $value); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get the current user configs array |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getConfigs(): array { |
|
128
|
|
|
if ($this->user === null) { |
|
129
|
|
|
throw new \Exception('No user logged in'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$userId = $this->user->getUID(); |
|
133
|
|
|
$userConfigs = array_map(function(string $key) use ($userId) { |
|
134
|
|
|
$value = $this->config->getUserValue($userId, Application::APP_ID, $key, $this->getDefaultConfigValue($key)); |
|
135
|
|
|
// If the default is expected to be a boolean, we need to cast the value |
|
136
|
|
|
if (is_bool($this->getDefaultConfigValue($key))) { |
|
137
|
|
|
return $value === '1'; |
|
138
|
|
|
} |
|
139
|
|
|
return $value; |
|
140
|
|
|
}, $this->getAllowedConfigKeys()); |
|
141
|
|
|
|
|
142
|
|
|
return array_combine($this->getAllowedConfigKeys(), $userConfigs); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|