@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | |
189 | 189 | private function filterTrace(array $trace) { |
190 | 190 | $sensitiveValues = []; |
191 | - $trace = array_map(function (array $traceLine) use (&$sensitiveValues) { |
|
191 | + $trace = array_map(function(array $traceLine) use (&$sensitiveValues) { |
|
192 | 192 | $className = $traceLine['class'] ?? ''; |
193 | 193 | if ($className && isset($this->methodsWithSensitiveParametersByClass[$className]) |
194 | 194 | && in_array($traceLine['function'], $this->methodsWithSensitiveParametersByClass[$className], true)) { |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | } |
202 | 202 | return $traceLine; |
203 | 203 | }, $trace); |
204 | - return array_map(function (array $traceLine) use ($sensitiveValues) { |
|
204 | + return array_map(function(array $traceLine) use ($sensitiveValues) { |
|
205 | 205 | if (isset($traceLine['args'])) { |
206 | 206 | $traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues); |
207 | 207 | } |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | } |
224 | 224 | |
225 | 225 | private function encodeTrace($trace) { |
226 | - $trace = array_map(function (array $line) { |
|
226 | + $trace = array_map(function(array $line) { |
|
227 | 227 | if (isset($line['args'])) { |
228 | 228 | $line['args'] = array_map([$this, 'encodeArg'], $line['args']); |
229 | 229 | } |
@@ -241,9 +241,9 @@ discard block |
||
241 | 241 | ]; |
242 | 242 | } |
243 | 243 | |
244 | - $objectInfo = [ '__class__' => get_class($arg) ]; |
|
244 | + $objectInfo = ['__class__' => get_class($arg)]; |
|
245 | 245 | $objectVars = get_object_vars($arg); |
246 | - return array_map(function ($arg) use ($nestingLevel) { |
|
246 | + return array_map(function($arg) use ($nestingLevel) { |
|
247 | 247 | return $this->encodeArg($arg, $nestingLevel - 1); |
248 | 248 | }, array_merge($objectInfo, $objectVars)); |
249 | 249 | } |
@@ -254,14 +254,14 @@ discard block |
||
254 | 254 | } |
255 | 255 | |
256 | 256 | // Only log the first 5 elements of an array unless we are on debug |
257 | - if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) { |
|
257 | + if ((int) $this->systemConfig->getValue('loglevel', 2) !== 0) { |
|
258 | 258 | $elemCount = count($arg); |
259 | 259 | if ($elemCount > 5) { |
260 | 260 | $arg = array_slice($arg, 0, 5); |
261 | - $arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries'; |
|
261 | + $arg[] = 'And '.($elemCount - 5).' more entries, set log level to debug to see all entries'; |
|
262 | 262 | } |
263 | 263 | } |
264 | - return array_map(function ($e) use ($nestingLevel) { |
|
264 | + return array_map(function($e) use ($nestingLevel) { |
|
265 | 265 | return $this->encodeArg($e, $nestingLevel - 1); |
266 | 266 | }, $arg); |
267 | 267 | } |
@@ -22,287 +22,287 @@ |
||
22 | 22 | use OCP\HintException; |
23 | 23 | |
24 | 24 | class ExceptionSerializer { |
25 | - public const SENSITIVE_VALUE_PLACEHOLDER = '*** sensitive parameters replaced ***'; |
|
26 | - |
|
27 | - public const methodsWithSensitiveParameters = [ |
|
28 | - // Session/User |
|
29 | - 'completeLogin', |
|
30 | - 'login', |
|
31 | - 'checkPassword', |
|
32 | - 'checkPasswordNoLogging', |
|
33 | - 'loginWithPassword', |
|
34 | - 'updatePrivateKeyPassword', |
|
35 | - 'validateUserPass', |
|
36 | - 'loginWithToken', |
|
37 | - '{closure}', |
|
38 | - '{closure:*', |
|
39 | - 'createSessionToken', |
|
40 | - |
|
41 | - // Provisioning |
|
42 | - 'addUser', |
|
43 | - |
|
44 | - // TokenProvider |
|
45 | - 'getToken', |
|
46 | - 'isTokenPassword', |
|
47 | - 'getPassword', |
|
48 | - 'decryptPassword', |
|
49 | - 'logClientIn', |
|
50 | - 'generateToken', |
|
51 | - 'validateToken', |
|
52 | - |
|
53 | - // TwoFactorAuth |
|
54 | - 'solveChallenge', |
|
55 | - 'verifyChallenge', |
|
56 | - |
|
57 | - // ICrypto |
|
58 | - 'calculateHMAC', |
|
59 | - 'encrypt', |
|
60 | - 'decrypt', |
|
61 | - |
|
62 | - // LoginController |
|
63 | - 'tryLogin', |
|
64 | - 'confirmPassword', |
|
65 | - |
|
66 | - // LDAP |
|
67 | - 'bind', |
|
68 | - 'areCredentialsValid', |
|
69 | - 'invokeLDAPMethod', |
|
70 | - |
|
71 | - // Encryption |
|
72 | - 'storeKeyPair', |
|
73 | - 'setupUser', |
|
74 | - 'checkSignature', |
|
75 | - |
|
76 | - // files_external: OCA\Files_External\MountConfig |
|
77 | - 'getBackendStatus', |
|
78 | - |
|
79 | - // files_external: UserStoragesController |
|
80 | - 'update', |
|
81 | - |
|
82 | - // Preview providers, don't log big data strings |
|
83 | - 'imagecreatefromstring', |
|
84 | - |
|
85 | - // text: PublicSessionController, SessionController and ApiService |
|
86 | - 'create', |
|
87 | - 'close', |
|
88 | - 'push', |
|
89 | - 'sync', |
|
90 | - 'updateSession', |
|
91 | - 'mention', |
|
92 | - 'loginSessionUser', |
|
93 | - |
|
94 | - ]; |
|
95 | - |
|
96 | - public function __construct( |
|
97 | - private SystemConfig $systemConfig, |
|
98 | - ) { |
|
99 | - } |
|
100 | - |
|
101 | - protected array $methodsWithSensitiveParametersByClass = [ |
|
102 | - SetupController::class => [ |
|
103 | - 'run', |
|
104 | - 'display', |
|
105 | - 'loadAutoConfig', |
|
106 | - ], |
|
107 | - Setup::class => [ |
|
108 | - 'install' |
|
109 | - ], |
|
110 | - Key::class => [ |
|
111 | - '__construct' |
|
112 | - ], |
|
113 | - Client::class => [ |
|
114 | - 'request', |
|
115 | - 'delete', |
|
116 | - 'deleteAsync', |
|
117 | - 'get', |
|
118 | - 'getAsync', |
|
119 | - 'head', |
|
120 | - 'headAsync', |
|
121 | - 'options', |
|
122 | - 'optionsAsync', |
|
123 | - 'patch', |
|
124 | - 'post', |
|
125 | - 'postAsync', |
|
126 | - 'put', |
|
127 | - 'putAsync', |
|
128 | - ], |
|
129 | - \Redis::class => [ |
|
130 | - 'auth' |
|
131 | - ], |
|
132 | - \RedisCluster::class => [ |
|
133 | - '__construct' |
|
134 | - ], |
|
135 | - Crypt::class => [ |
|
136 | - 'symmetricEncryptFileContent', |
|
137 | - 'encrypt', |
|
138 | - 'generatePasswordHash', |
|
139 | - 'encryptPrivateKey', |
|
140 | - 'decryptPrivateKey', |
|
141 | - 'isValidPrivateKey', |
|
142 | - 'symmetricDecryptFileContent', |
|
143 | - 'checkSignature', |
|
144 | - 'createSignature', |
|
145 | - 'decrypt', |
|
146 | - 'multiKeyDecrypt', |
|
147 | - 'multiKeyEncrypt', |
|
148 | - ], |
|
149 | - RecoveryController::class => [ |
|
150 | - 'adminRecovery', |
|
151 | - 'changeRecoveryPassword' |
|
152 | - ], |
|
153 | - SettingsController::class => [ |
|
154 | - 'updatePrivateKeyPassword', |
|
155 | - ], |
|
156 | - Encryption::class => [ |
|
157 | - 'encrypt', |
|
158 | - 'decrypt', |
|
159 | - ], |
|
160 | - KeyManager::class => [ |
|
161 | - 'checkRecoveryPassword', |
|
162 | - 'storeKeyPair', |
|
163 | - 'setRecoveryKey', |
|
164 | - 'setPrivateKey', |
|
165 | - 'setFileKey', |
|
166 | - 'setAllFileKeys', |
|
167 | - ], |
|
168 | - Session::class => [ |
|
169 | - 'setPrivateKey', |
|
170 | - 'prepareDecryptAll', |
|
171 | - ], |
|
172 | - \OCA\Encryption\Users\Setup::class => [ |
|
173 | - 'setupUser', |
|
174 | - ], |
|
175 | - UserEventsListener::class => [ |
|
176 | - 'handle', |
|
177 | - 'onUserCreated', |
|
178 | - 'onUserLogin', |
|
179 | - 'onBeforePasswordUpdated', |
|
180 | - 'onPasswordUpdated', |
|
181 | - 'onPasswordReset', |
|
182 | - ], |
|
183 | - PassphraseService::class => [ |
|
184 | - 'setPassphraseForUser', |
|
185 | - ], |
|
186 | - ]; |
|
187 | - |
|
188 | - private function editTrace(array &$sensitiveValues, array $traceLine): array { |
|
189 | - if (isset($traceLine['args'])) { |
|
190 | - $sensitiveValues = array_merge($sensitiveValues, $traceLine['args']); |
|
191 | - } |
|
192 | - $traceLine['args'] = [self::SENSITIVE_VALUE_PLACEHOLDER]; |
|
193 | - return $traceLine; |
|
194 | - } |
|
195 | - |
|
196 | - private function filterTrace(array $trace) { |
|
197 | - $sensitiveValues = []; |
|
198 | - $trace = array_map(function (array $traceLine) use (&$sensitiveValues) { |
|
199 | - $className = $traceLine['class'] ?? ''; |
|
200 | - if ($className && isset($this->methodsWithSensitiveParametersByClass[$className]) |
|
201 | - && in_array($traceLine['function'], $this->methodsWithSensitiveParametersByClass[$className], true)) { |
|
202 | - return $this->editTrace($sensitiveValues, $traceLine); |
|
203 | - } |
|
204 | - foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) { |
|
205 | - if (str_contains($traceLine['function'], $sensitiveMethod) |
|
206 | - || (str_ends_with($sensitiveMethod, '*') |
|
207 | - && str_starts_with($traceLine['function'], substr($sensitiveMethod, 0, -1)))) { |
|
208 | - return $this->editTrace($sensitiveValues, $traceLine); |
|
209 | - } |
|
210 | - } |
|
211 | - return $traceLine; |
|
212 | - }, $trace); |
|
213 | - return array_map(function (array $traceLine) use ($sensitiveValues) { |
|
214 | - if (isset($traceLine['args'])) { |
|
215 | - $traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues); |
|
216 | - } |
|
217 | - return $traceLine; |
|
218 | - }, $trace); |
|
219 | - } |
|
220 | - |
|
221 | - private function removeValuesFromArgs($args, $values): array { |
|
222 | - $workArgs = []; |
|
223 | - foreach ($args as $key => $arg) { |
|
224 | - if (in_array($arg, $values, true)) { |
|
225 | - $arg = self::SENSITIVE_VALUE_PLACEHOLDER; |
|
226 | - } elseif (is_array($arg)) { |
|
227 | - $arg = $this->removeValuesFromArgs($arg, $values); |
|
228 | - } |
|
229 | - $workArgs[$key] = $arg; |
|
230 | - } |
|
231 | - return $workArgs; |
|
232 | - } |
|
233 | - |
|
234 | - private function encodeTrace($trace) { |
|
235 | - $trace = array_map(function (array $line) { |
|
236 | - if (isset($line['args'])) { |
|
237 | - $line['args'] = array_map([$this, 'encodeArg'], $line['args']); |
|
238 | - } |
|
239 | - return $line; |
|
240 | - }, $trace); |
|
241 | - return $this->filterTrace($trace); |
|
242 | - } |
|
243 | - |
|
244 | - private function encodeArg($arg, $nestingLevel = 5) { |
|
245 | - if (is_object($arg)) { |
|
246 | - if ($nestingLevel === 0) { |
|
247 | - return [ |
|
248 | - '__class__' => get_class($arg), |
|
249 | - '__properties__' => 'Encoding skipped as the maximum nesting level was reached', |
|
250 | - ]; |
|
251 | - } |
|
252 | - |
|
253 | - $objectInfo = [ '__class__' => get_class($arg) ]; |
|
254 | - $objectVars = get_object_vars($arg); |
|
255 | - return array_map(function ($arg) use ($nestingLevel) { |
|
256 | - return $this->encodeArg($arg, $nestingLevel - 1); |
|
257 | - }, array_merge($objectInfo, $objectVars)); |
|
258 | - } |
|
259 | - |
|
260 | - if (is_array($arg)) { |
|
261 | - if ($nestingLevel === 0) { |
|
262 | - return ['Encoding skipped as the maximum nesting level was reached']; |
|
263 | - } |
|
264 | - |
|
265 | - // Only log the first 5 elements of an array unless we are on debug |
|
266 | - if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) { |
|
267 | - $elemCount = count($arg); |
|
268 | - if ($elemCount > 5) { |
|
269 | - $arg = array_slice($arg, 0, 5); |
|
270 | - $arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries'; |
|
271 | - } |
|
272 | - } |
|
273 | - return array_map(function ($e) use ($nestingLevel) { |
|
274 | - return $this->encodeArg($e, $nestingLevel - 1); |
|
275 | - }, $arg); |
|
276 | - } |
|
277 | - |
|
278 | - return $arg; |
|
279 | - } |
|
280 | - |
|
281 | - public function serializeException(\Throwable $exception): array { |
|
282 | - $data = [ |
|
283 | - 'Exception' => get_class($exception), |
|
284 | - 'Message' => $exception->getMessage(), |
|
285 | - 'Code' => $exception->getCode(), |
|
286 | - 'Trace' => $this->encodeTrace($exception->getTrace()), |
|
287 | - 'File' => $exception->getFile(), |
|
288 | - 'Line' => $exception->getLine(), |
|
289 | - ]; |
|
290 | - |
|
291 | - if ($exception instanceof HintException) { |
|
292 | - $data['Hint'] = $exception->getHint(); |
|
293 | - } |
|
294 | - |
|
295 | - if ($exception->getPrevious()) { |
|
296 | - $data['Previous'] = $this->serializeException($exception->getPrevious()); |
|
297 | - } |
|
298 | - |
|
299 | - return $data; |
|
300 | - } |
|
301 | - |
|
302 | - public function enlistSensitiveMethods(string $class, array $methods): void { |
|
303 | - if (!isset($this->methodsWithSensitiveParametersByClass[$class])) { |
|
304 | - $this->methodsWithSensitiveParametersByClass[$class] = []; |
|
305 | - } |
|
306 | - $this->methodsWithSensitiveParametersByClass[$class] = array_merge($this->methodsWithSensitiveParametersByClass[$class], $methods); |
|
307 | - } |
|
25 | + public const SENSITIVE_VALUE_PLACEHOLDER = '*** sensitive parameters replaced ***'; |
|
26 | + |
|
27 | + public const methodsWithSensitiveParameters = [ |
|
28 | + // Session/User |
|
29 | + 'completeLogin', |
|
30 | + 'login', |
|
31 | + 'checkPassword', |
|
32 | + 'checkPasswordNoLogging', |
|
33 | + 'loginWithPassword', |
|
34 | + 'updatePrivateKeyPassword', |
|
35 | + 'validateUserPass', |
|
36 | + 'loginWithToken', |
|
37 | + '{closure}', |
|
38 | + '{closure:*', |
|
39 | + 'createSessionToken', |
|
40 | + |
|
41 | + // Provisioning |
|
42 | + 'addUser', |
|
43 | + |
|
44 | + // TokenProvider |
|
45 | + 'getToken', |
|
46 | + 'isTokenPassword', |
|
47 | + 'getPassword', |
|
48 | + 'decryptPassword', |
|
49 | + 'logClientIn', |
|
50 | + 'generateToken', |
|
51 | + 'validateToken', |
|
52 | + |
|
53 | + // TwoFactorAuth |
|
54 | + 'solveChallenge', |
|
55 | + 'verifyChallenge', |
|
56 | + |
|
57 | + // ICrypto |
|
58 | + 'calculateHMAC', |
|
59 | + 'encrypt', |
|
60 | + 'decrypt', |
|
61 | + |
|
62 | + // LoginController |
|
63 | + 'tryLogin', |
|
64 | + 'confirmPassword', |
|
65 | + |
|
66 | + // LDAP |
|
67 | + 'bind', |
|
68 | + 'areCredentialsValid', |
|
69 | + 'invokeLDAPMethod', |
|
70 | + |
|
71 | + // Encryption |
|
72 | + 'storeKeyPair', |
|
73 | + 'setupUser', |
|
74 | + 'checkSignature', |
|
75 | + |
|
76 | + // files_external: OCA\Files_External\MountConfig |
|
77 | + 'getBackendStatus', |
|
78 | + |
|
79 | + // files_external: UserStoragesController |
|
80 | + 'update', |
|
81 | + |
|
82 | + // Preview providers, don't log big data strings |
|
83 | + 'imagecreatefromstring', |
|
84 | + |
|
85 | + // text: PublicSessionController, SessionController and ApiService |
|
86 | + 'create', |
|
87 | + 'close', |
|
88 | + 'push', |
|
89 | + 'sync', |
|
90 | + 'updateSession', |
|
91 | + 'mention', |
|
92 | + 'loginSessionUser', |
|
93 | + |
|
94 | + ]; |
|
95 | + |
|
96 | + public function __construct( |
|
97 | + private SystemConfig $systemConfig, |
|
98 | + ) { |
|
99 | + } |
|
100 | + |
|
101 | + protected array $methodsWithSensitiveParametersByClass = [ |
|
102 | + SetupController::class => [ |
|
103 | + 'run', |
|
104 | + 'display', |
|
105 | + 'loadAutoConfig', |
|
106 | + ], |
|
107 | + Setup::class => [ |
|
108 | + 'install' |
|
109 | + ], |
|
110 | + Key::class => [ |
|
111 | + '__construct' |
|
112 | + ], |
|
113 | + Client::class => [ |
|
114 | + 'request', |
|
115 | + 'delete', |
|
116 | + 'deleteAsync', |
|
117 | + 'get', |
|
118 | + 'getAsync', |
|
119 | + 'head', |
|
120 | + 'headAsync', |
|
121 | + 'options', |
|
122 | + 'optionsAsync', |
|
123 | + 'patch', |
|
124 | + 'post', |
|
125 | + 'postAsync', |
|
126 | + 'put', |
|
127 | + 'putAsync', |
|
128 | + ], |
|
129 | + \Redis::class => [ |
|
130 | + 'auth' |
|
131 | + ], |
|
132 | + \RedisCluster::class => [ |
|
133 | + '__construct' |
|
134 | + ], |
|
135 | + Crypt::class => [ |
|
136 | + 'symmetricEncryptFileContent', |
|
137 | + 'encrypt', |
|
138 | + 'generatePasswordHash', |
|
139 | + 'encryptPrivateKey', |
|
140 | + 'decryptPrivateKey', |
|
141 | + 'isValidPrivateKey', |
|
142 | + 'symmetricDecryptFileContent', |
|
143 | + 'checkSignature', |
|
144 | + 'createSignature', |
|
145 | + 'decrypt', |
|
146 | + 'multiKeyDecrypt', |
|
147 | + 'multiKeyEncrypt', |
|
148 | + ], |
|
149 | + RecoveryController::class => [ |
|
150 | + 'adminRecovery', |
|
151 | + 'changeRecoveryPassword' |
|
152 | + ], |
|
153 | + SettingsController::class => [ |
|
154 | + 'updatePrivateKeyPassword', |
|
155 | + ], |
|
156 | + Encryption::class => [ |
|
157 | + 'encrypt', |
|
158 | + 'decrypt', |
|
159 | + ], |
|
160 | + KeyManager::class => [ |
|
161 | + 'checkRecoveryPassword', |
|
162 | + 'storeKeyPair', |
|
163 | + 'setRecoveryKey', |
|
164 | + 'setPrivateKey', |
|
165 | + 'setFileKey', |
|
166 | + 'setAllFileKeys', |
|
167 | + ], |
|
168 | + Session::class => [ |
|
169 | + 'setPrivateKey', |
|
170 | + 'prepareDecryptAll', |
|
171 | + ], |
|
172 | + \OCA\Encryption\Users\Setup::class => [ |
|
173 | + 'setupUser', |
|
174 | + ], |
|
175 | + UserEventsListener::class => [ |
|
176 | + 'handle', |
|
177 | + 'onUserCreated', |
|
178 | + 'onUserLogin', |
|
179 | + 'onBeforePasswordUpdated', |
|
180 | + 'onPasswordUpdated', |
|
181 | + 'onPasswordReset', |
|
182 | + ], |
|
183 | + PassphraseService::class => [ |
|
184 | + 'setPassphraseForUser', |
|
185 | + ], |
|
186 | + ]; |
|
187 | + |
|
188 | + private function editTrace(array &$sensitiveValues, array $traceLine): array { |
|
189 | + if (isset($traceLine['args'])) { |
|
190 | + $sensitiveValues = array_merge($sensitiveValues, $traceLine['args']); |
|
191 | + } |
|
192 | + $traceLine['args'] = [self::SENSITIVE_VALUE_PLACEHOLDER]; |
|
193 | + return $traceLine; |
|
194 | + } |
|
195 | + |
|
196 | + private function filterTrace(array $trace) { |
|
197 | + $sensitiveValues = []; |
|
198 | + $trace = array_map(function (array $traceLine) use (&$sensitiveValues) { |
|
199 | + $className = $traceLine['class'] ?? ''; |
|
200 | + if ($className && isset($this->methodsWithSensitiveParametersByClass[$className]) |
|
201 | + && in_array($traceLine['function'], $this->methodsWithSensitiveParametersByClass[$className], true)) { |
|
202 | + return $this->editTrace($sensitiveValues, $traceLine); |
|
203 | + } |
|
204 | + foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) { |
|
205 | + if (str_contains($traceLine['function'], $sensitiveMethod) |
|
206 | + || (str_ends_with($sensitiveMethod, '*') |
|
207 | + && str_starts_with($traceLine['function'], substr($sensitiveMethod, 0, -1)))) { |
|
208 | + return $this->editTrace($sensitiveValues, $traceLine); |
|
209 | + } |
|
210 | + } |
|
211 | + return $traceLine; |
|
212 | + }, $trace); |
|
213 | + return array_map(function (array $traceLine) use ($sensitiveValues) { |
|
214 | + if (isset($traceLine['args'])) { |
|
215 | + $traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues); |
|
216 | + } |
|
217 | + return $traceLine; |
|
218 | + }, $trace); |
|
219 | + } |
|
220 | + |
|
221 | + private function removeValuesFromArgs($args, $values): array { |
|
222 | + $workArgs = []; |
|
223 | + foreach ($args as $key => $arg) { |
|
224 | + if (in_array($arg, $values, true)) { |
|
225 | + $arg = self::SENSITIVE_VALUE_PLACEHOLDER; |
|
226 | + } elseif (is_array($arg)) { |
|
227 | + $arg = $this->removeValuesFromArgs($arg, $values); |
|
228 | + } |
|
229 | + $workArgs[$key] = $arg; |
|
230 | + } |
|
231 | + return $workArgs; |
|
232 | + } |
|
233 | + |
|
234 | + private function encodeTrace($trace) { |
|
235 | + $trace = array_map(function (array $line) { |
|
236 | + if (isset($line['args'])) { |
|
237 | + $line['args'] = array_map([$this, 'encodeArg'], $line['args']); |
|
238 | + } |
|
239 | + return $line; |
|
240 | + }, $trace); |
|
241 | + return $this->filterTrace($trace); |
|
242 | + } |
|
243 | + |
|
244 | + private function encodeArg($arg, $nestingLevel = 5) { |
|
245 | + if (is_object($arg)) { |
|
246 | + if ($nestingLevel === 0) { |
|
247 | + return [ |
|
248 | + '__class__' => get_class($arg), |
|
249 | + '__properties__' => 'Encoding skipped as the maximum nesting level was reached', |
|
250 | + ]; |
|
251 | + } |
|
252 | + |
|
253 | + $objectInfo = [ '__class__' => get_class($arg) ]; |
|
254 | + $objectVars = get_object_vars($arg); |
|
255 | + return array_map(function ($arg) use ($nestingLevel) { |
|
256 | + return $this->encodeArg($arg, $nestingLevel - 1); |
|
257 | + }, array_merge($objectInfo, $objectVars)); |
|
258 | + } |
|
259 | + |
|
260 | + if (is_array($arg)) { |
|
261 | + if ($nestingLevel === 0) { |
|
262 | + return ['Encoding skipped as the maximum nesting level was reached']; |
|
263 | + } |
|
264 | + |
|
265 | + // Only log the first 5 elements of an array unless we are on debug |
|
266 | + if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) { |
|
267 | + $elemCount = count($arg); |
|
268 | + if ($elemCount > 5) { |
|
269 | + $arg = array_slice($arg, 0, 5); |
|
270 | + $arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries'; |
|
271 | + } |
|
272 | + } |
|
273 | + return array_map(function ($e) use ($nestingLevel) { |
|
274 | + return $this->encodeArg($e, $nestingLevel - 1); |
|
275 | + }, $arg); |
|
276 | + } |
|
277 | + |
|
278 | + return $arg; |
|
279 | + } |
|
280 | + |
|
281 | + public function serializeException(\Throwable $exception): array { |
|
282 | + $data = [ |
|
283 | + 'Exception' => get_class($exception), |
|
284 | + 'Message' => $exception->getMessage(), |
|
285 | + 'Code' => $exception->getCode(), |
|
286 | + 'Trace' => $this->encodeTrace($exception->getTrace()), |
|
287 | + 'File' => $exception->getFile(), |
|
288 | + 'Line' => $exception->getLine(), |
|
289 | + ]; |
|
290 | + |
|
291 | + if ($exception instanceof HintException) { |
|
292 | + $data['Hint'] = $exception->getHint(); |
|
293 | + } |
|
294 | + |
|
295 | + if ($exception->getPrevious()) { |
|
296 | + $data['Previous'] = $this->serializeException($exception->getPrevious()); |
|
297 | + } |
|
298 | + |
|
299 | + return $data; |
|
300 | + } |
|
301 | + |
|
302 | + public function enlistSensitiveMethods(string $class, array $methods): void { |
|
303 | + if (!isset($this->methodsWithSensitiveParametersByClass[$class])) { |
|
304 | + $this->methodsWithSensitiveParametersByClass[$class] = []; |
|
305 | + } |
|
306 | + $this->methodsWithSensitiveParametersByClass[$class] = array_merge($this->methodsWithSensitiveParametersByClass[$class], $methods); |
|
307 | + } |
|
308 | 308 | } |
@@ -167,7 +167,7 @@ |
||
167 | 167 | * @return bool |
168 | 168 | */ |
169 | 169 | public function isFlagSet(int $flag): bool { |
170 | - return (bool)($this->flags & $flag); |
|
170 | + return (bool) ($this->flags & $flag); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -10,207 +10,207 @@ |
||
10 | 10 | * Parameter for an external storage definition |
11 | 11 | */ |
12 | 12 | class DefinitionParameter implements \JsonSerializable { |
13 | - // placeholder value for password fields, when the client updates a storage configuration |
|
14 | - // placeholder values are ignored and the field is left unmodified |
|
15 | - public const UNMODIFIED_PLACEHOLDER = '__unmodified__'; |
|
16 | - |
|
17 | - /** Value constants */ |
|
18 | - public const VALUE_TEXT = 0; |
|
19 | - public const VALUE_BOOLEAN = 1; |
|
20 | - public const VALUE_PASSWORD = 2; |
|
21 | - |
|
22 | - /** Flag constants */ |
|
23 | - public const FLAG_NONE = 0; |
|
24 | - public const FLAG_OPTIONAL = 1; |
|
25 | - public const FLAG_USER_PROVIDED = 2; |
|
26 | - public const FLAG_HIDDEN = 4; |
|
27 | - |
|
28 | - /** @var string human-readable parameter tooltip */ |
|
29 | - private string $tooltip = ''; |
|
30 | - |
|
31 | - /** @var int value type, see self::VALUE_* constants */ |
|
32 | - private int $type = self::VALUE_TEXT; |
|
33 | - |
|
34 | - /** @var int flags, see self::FLAG_* constants */ |
|
35 | - private int $flags = self::FLAG_NONE; |
|
36 | - |
|
37 | - /** |
|
38 | - * @param string $name parameter name |
|
39 | - * @param string $text parameter description |
|
40 | - * @param mixed $defaultValue default value |
|
41 | - */ |
|
42 | - public function __construct( |
|
43 | - private string $name, |
|
44 | - private string $text, |
|
45 | - private $defaultValue = null, |
|
46 | - ) { |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getName(): string { |
|
53 | - return $this->name; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function getText(): string { |
|
60 | - return $this->text; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Get value type |
|
65 | - * |
|
66 | - * @return int |
|
67 | - */ |
|
68 | - public function getType(): int { |
|
69 | - return $this->type; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Set value type |
|
74 | - * |
|
75 | - * @param int $type |
|
76 | - * @return self |
|
77 | - */ |
|
78 | - public function setType(int $type) { |
|
79 | - $this->type = $type; |
|
80 | - return $this; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @return mixed default value |
|
85 | - */ |
|
86 | - public function getDefaultValue() { |
|
87 | - return $this->defaultValue; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param mixed $defaultValue default value |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - public function setDefaultValue($defaultValue) { |
|
95 | - $this->defaultValue = $defaultValue; |
|
96 | - return $this; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function getTypeName(): string { |
|
103 | - switch ($this->type) { |
|
104 | - case self::VALUE_BOOLEAN: |
|
105 | - return 'boolean'; |
|
106 | - case self::VALUE_TEXT: |
|
107 | - return 'text'; |
|
108 | - case self::VALUE_PASSWORD: |
|
109 | - return 'password'; |
|
110 | - default: |
|
111 | - return 'unknown'; |
|
112 | - } |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @return int |
|
117 | - */ |
|
118 | - public function getFlags(): int { |
|
119 | - return $this->flags; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param int $flags |
|
124 | - * @return self |
|
125 | - */ |
|
126 | - public function setFlags(int $flags) { |
|
127 | - $this->flags = $flags; |
|
128 | - return $this; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @param int $flag |
|
133 | - * @return self |
|
134 | - */ |
|
135 | - public function setFlag(int $flag) { |
|
136 | - $this->flags |= $flag; |
|
137 | - return $this; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param int $flag |
|
142 | - * @return bool |
|
143 | - */ |
|
144 | - public function isFlagSet(int $flag): bool { |
|
145 | - return (bool)($this->flags & $flag); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function getTooltip(): string { |
|
152 | - return $this->tooltip; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @param string $tooltip |
|
157 | - * @return self |
|
158 | - */ |
|
159 | - public function setTooltip(string $tooltip) { |
|
160 | - $this->tooltip = $tooltip; |
|
161 | - return $this; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Serialize into JSON for client-side JS |
|
166 | - */ |
|
167 | - public function jsonSerialize(): array { |
|
168 | - $result = [ |
|
169 | - 'value' => $this->getText(), |
|
170 | - 'flags' => $this->getFlags(), |
|
171 | - 'type' => $this->getType(), |
|
172 | - 'tooltip' => $this->getTooltip(), |
|
173 | - ]; |
|
174 | - $defaultValue = $this->getDefaultValue(); |
|
175 | - if ($defaultValue) { |
|
176 | - $result['defaultValue'] = $defaultValue; |
|
177 | - } |
|
178 | - return $result; |
|
179 | - } |
|
180 | - |
|
181 | - public function isOptional(): bool { |
|
182 | - return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Validate a parameter value against this |
|
187 | - * Convert type as necessary |
|
188 | - * |
|
189 | - * @param mixed $value Value to check |
|
190 | - * @return bool success |
|
191 | - */ |
|
192 | - public function validateValue(&$value): bool { |
|
193 | - switch ($this->getType()) { |
|
194 | - case self::VALUE_BOOLEAN: |
|
195 | - if (!is_bool($value)) { |
|
196 | - switch ($value) { |
|
197 | - case 'true': |
|
198 | - $value = true; |
|
199 | - break; |
|
200 | - case 'false': |
|
201 | - $value = false; |
|
202 | - break; |
|
203 | - default: |
|
204 | - return false; |
|
205 | - } |
|
206 | - } |
|
207 | - break; |
|
208 | - default: |
|
209 | - if (!$value && !$this->isOptional()) { |
|
210 | - return false; |
|
211 | - } |
|
212 | - break; |
|
213 | - } |
|
214 | - return true; |
|
215 | - } |
|
13 | + // placeholder value for password fields, when the client updates a storage configuration |
|
14 | + // placeholder values are ignored and the field is left unmodified |
|
15 | + public const UNMODIFIED_PLACEHOLDER = '__unmodified__'; |
|
16 | + |
|
17 | + /** Value constants */ |
|
18 | + public const VALUE_TEXT = 0; |
|
19 | + public const VALUE_BOOLEAN = 1; |
|
20 | + public const VALUE_PASSWORD = 2; |
|
21 | + |
|
22 | + /** Flag constants */ |
|
23 | + public const FLAG_NONE = 0; |
|
24 | + public const FLAG_OPTIONAL = 1; |
|
25 | + public const FLAG_USER_PROVIDED = 2; |
|
26 | + public const FLAG_HIDDEN = 4; |
|
27 | + |
|
28 | + /** @var string human-readable parameter tooltip */ |
|
29 | + private string $tooltip = ''; |
|
30 | + |
|
31 | + /** @var int value type, see self::VALUE_* constants */ |
|
32 | + private int $type = self::VALUE_TEXT; |
|
33 | + |
|
34 | + /** @var int flags, see self::FLAG_* constants */ |
|
35 | + private int $flags = self::FLAG_NONE; |
|
36 | + |
|
37 | + /** |
|
38 | + * @param string $name parameter name |
|
39 | + * @param string $text parameter description |
|
40 | + * @param mixed $defaultValue default value |
|
41 | + */ |
|
42 | + public function __construct( |
|
43 | + private string $name, |
|
44 | + private string $text, |
|
45 | + private $defaultValue = null, |
|
46 | + ) { |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getName(): string { |
|
53 | + return $this->name; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function getText(): string { |
|
60 | + return $this->text; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Get value type |
|
65 | + * |
|
66 | + * @return int |
|
67 | + */ |
|
68 | + public function getType(): int { |
|
69 | + return $this->type; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Set value type |
|
74 | + * |
|
75 | + * @param int $type |
|
76 | + * @return self |
|
77 | + */ |
|
78 | + public function setType(int $type) { |
|
79 | + $this->type = $type; |
|
80 | + return $this; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @return mixed default value |
|
85 | + */ |
|
86 | + public function getDefaultValue() { |
|
87 | + return $this->defaultValue; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param mixed $defaultValue default value |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + public function setDefaultValue($defaultValue) { |
|
95 | + $this->defaultValue = $defaultValue; |
|
96 | + return $this; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function getTypeName(): string { |
|
103 | + switch ($this->type) { |
|
104 | + case self::VALUE_BOOLEAN: |
|
105 | + return 'boolean'; |
|
106 | + case self::VALUE_TEXT: |
|
107 | + return 'text'; |
|
108 | + case self::VALUE_PASSWORD: |
|
109 | + return 'password'; |
|
110 | + default: |
|
111 | + return 'unknown'; |
|
112 | + } |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @return int |
|
117 | + */ |
|
118 | + public function getFlags(): int { |
|
119 | + return $this->flags; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param int $flags |
|
124 | + * @return self |
|
125 | + */ |
|
126 | + public function setFlags(int $flags) { |
|
127 | + $this->flags = $flags; |
|
128 | + return $this; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @param int $flag |
|
133 | + * @return self |
|
134 | + */ |
|
135 | + public function setFlag(int $flag) { |
|
136 | + $this->flags |= $flag; |
|
137 | + return $this; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param int $flag |
|
142 | + * @return bool |
|
143 | + */ |
|
144 | + public function isFlagSet(int $flag): bool { |
|
145 | + return (bool)($this->flags & $flag); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function getTooltip(): string { |
|
152 | + return $this->tooltip; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @param string $tooltip |
|
157 | + * @return self |
|
158 | + */ |
|
159 | + public function setTooltip(string $tooltip) { |
|
160 | + $this->tooltip = $tooltip; |
|
161 | + return $this; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Serialize into JSON for client-side JS |
|
166 | + */ |
|
167 | + public function jsonSerialize(): array { |
|
168 | + $result = [ |
|
169 | + 'value' => $this->getText(), |
|
170 | + 'flags' => $this->getFlags(), |
|
171 | + 'type' => $this->getType(), |
|
172 | + 'tooltip' => $this->getTooltip(), |
|
173 | + ]; |
|
174 | + $defaultValue = $this->getDefaultValue(); |
|
175 | + if ($defaultValue) { |
|
176 | + $result['defaultValue'] = $defaultValue; |
|
177 | + } |
|
178 | + return $result; |
|
179 | + } |
|
180 | + |
|
181 | + public function isOptional(): bool { |
|
182 | + return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Validate a parameter value against this |
|
187 | + * Convert type as necessary |
|
188 | + * |
|
189 | + * @param mixed $value Value to check |
|
190 | + * @return bool success |
|
191 | + */ |
|
192 | + public function validateValue(&$value): bool { |
|
193 | + switch ($this->getType()) { |
|
194 | + case self::VALUE_BOOLEAN: |
|
195 | + if (!is_bool($value)) { |
|
196 | + switch ($value) { |
|
197 | + case 'true': |
|
198 | + $value = true; |
|
199 | + break; |
|
200 | + case 'false': |
|
201 | + $value = false; |
|
202 | + break; |
|
203 | + default: |
|
204 | + return false; |
|
205 | + } |
|
206 | + } |
|
207 | + break; |
|
208 | + default: |
|
209 | + if (!$value && !$this->isOptional()) { |
|
210 | + return false; |
|
211 | + } |
|
212 | + break; |
|
213 | + } |
|
214 | + return true; |
|
215 | + } |
|
216 | 216 | } |
@@ -30,24 +30,24 @@ |
||
30 | 30 | use OCP\IL10N; |
31 | 31 | |
32 | 32 | class DAV extends Backend { |
33 | - use LegacyDependencyCheckPolyfill; |
|
33 | + use LegacyDependencyCheckPolyfill; |
|
34 | 34 | |
35 | - public function __construct(IL10N $l, Password $legacyAuth) { |
|
36 | - $this |
|
37 | - ->setIdentifier('dav') |
|
38 | - ->addIdentifierAlias('\OC\Files\Storage\DAV') // legacy compat |
|
39 | - ->setStorageClass('\OC\Files\Storage\DAV') |
|
40 | - ->setText($l->t('WebDAV')) |
|
41 | - ->addParameters([ |
|
42 | - new DefinitionParameter('host', $l->t('URL')), |
|
43 | - (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
44 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
45 | - (new DefinitionParameter('secure', $l->t('Secure https://'))) |
|
46 | - ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
47 | - ->setDefaultValue(true), |
|
48 | - ]) |
|
49 | - ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
50 | - ->setLegacyAuthMechanism($legacyAuth) |
|
51 | - ; |
|
52 | - } |
|
35 | + public function __construct(IL10N $l, Password $legacyAuth) { |
|
36 | + $this |
|
37 | + ->setIdentifier('dav') |
|
38 | + ->addIdentifierAlias('\OC\Files\Storage\DAV') // legacy compat |
|
39 | + ->setStorageClass('\OC\Files\Storage\DAV') |
|
40 | + ->setText($l->t('WebDAV')) |
|
41 | + ->addParameters([ |
|
42 | + new DefinitionParameter('host', $l->t('URL')), |
|
43 | + (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
44 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
45 | + (new DefinitionParameter('secure', $l->t('Secure https://'))) |
|
46 | + ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
47 | + ->setDefaultValue(true), |
|
48 | + ]) |
|
49 | + ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
50 | + ->setLegacyAuthMechanism($legacyAuth) |
|
51 | + ; |
|
52 | + } |
|
53 | 53 | } |
@@ -30,22 +30,22 @@ |
||
30 | 30 | use OCP\IL10N; |
31 | 31 | |
32 | 32 | class OwnCloud extends Backend { |
33 | - public function __construct(IL10N $l, Password $legacyAuth) { |
|
34 | - $this |
|
35 | - ->setIdentifier('owncloud') |
|
36 | - ->addIdentifierAlias('\OC\Files\Storage\OwnCloud') // legacy compat |
|
37 | - ->setStorageClass('\OCA\Files_External\Lib\Storage\OwnCloud') |
|
38 | - ->setText($l->t('Nextcloud')) |
|
39 | - ->addParameters([ |
|
40 | - new DefinitionParameter('host', $l->t('URL')), |
|
41 | - (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
42 | - ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
43 | - (new DefinitionParameter('secure', $l->t('Secure https://'))) |
|
44 | - ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
45 | - ->setDefaultValue(true), |
|
46 | - ]) |
|
47 | - ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
48 | - ->setLegacyAuthMechanism($legacyAuth) |
|
49 | - ; |
|
50 | - } |
|
33 | + public function __construct(IL10N $l, Password $legacyAuth) { |
|
34 | + $this |
|
35 | + ->setIdentifier('owncloud') |
|
36 | + ->addIdentifierAlias('\OC\Files\Storage\OwnCloud') // legacy compat |
|
37 | + ->setStorageClass('\OCA\Files_External\Lib\Storage\OwnCloud') |
|
38 | + ->setText($l->t('Nextcloud')) |
|
39 | + ->addParameters([ |
|
40 | + new DefinitionParameter('host', $l->t('URL')), |
|
41 | + (new DefinitionParameter('root', $l->t('Remote subfolder'))) |
|
42 | + ->setFlag(DefinitionParameter::FLAG_OPTIONAL), |
|
43 | + (new DefinitionParameter('secure', $l->t('Secure https://'))) |
|
44 | + ->setType(DefinitionParameter::VALUE_BOOLEAN) |
|
45 | + ->setDefaultValue(true), |
|
46 | + ]) |
|
47 | + ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) |
|
48 | + ->setLegacyAuthMechanism($legacyAuth) |
|
49 | + ; |
|
50 | + } |
|
51 | 51 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | $uid = $input->getArgument('uid'); |
118 | 118 | $user = $this->userManager->get($uid); |
119 | 119 | if (!$user) { |
120 | - throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.'); |
|
120 | + throw new \InvalidArgumentException('The user "'.$uid.'" does not exist.'); |
|
121 | 121 | } |
122 | 122 | // normalize uid |
123 | 123 | $input->setArgument('uid', $user->getUID()); |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | try { |
156 | 156 | $this->checkInput($input); |
157 | 157 | } catch (\InvalidArgumentException $e) { |
158 | - $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
158 | + $output->writeln('<error>'.$e->getMessage().'</error>'); |
|
159 | 159 | return 1; |
160 | 160 | } |
161 | 161 | |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $value = $this->config->getUserValue($uid, $app, $key, null); |
168 | 168 | if ($input->getArgument('value') !== null) { |
169 | 169 | if ($input->hasParameterOption('--update-only') && $value === null) { |
170 | - $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
170 | + $output->writeln('<error>The setting does not exist for user "'.$uid.'".</error>'); |
|
171 | 171 | return 1; |
172 | 172 | } |
173 | 173 | |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | return 0; |
198 | 198 | } elseif ($input->hasParameterOption('--delete')) { |
199 | 199 | if ($input->hasParameterOption('--error-if-not-exists') && $value === null) { |
200 | - $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
200 | + $output->writeln('<error>The setting does not exist for user "'.$uid.'".</error>'); |
|
201 | 201 | return 1; |
202 | 202 | } |
203 | 203 | |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | $output->writeln($user->getDisplayName()); |
230 | 230 | return 0; |
231 | 231 | } |
232 | - $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
232 | + $output->writeln('<error>The setting does not exist for user "'.$uid.'".</error>'); |
|
233 | 233 | return 1; |
234 | 234 | } |
235 | 235 | } else { |
@@ -18,247 +18,247 @@ |
||
18 | 18 | use Symfony\Component\Console\Output\OutputInterface; |
19 | 19 | |
20 | 20 | class Setting extends Base { |
21 | - public function __construct( |
|
22 | - protected IUserManager $userManager, |
|
23 | - protected IConfig $config, |
|
24 | - ) { |
|
25 | - parent::__construct(); |
|
26 | - } |
|
21 | + public function __construct( |
|
22 | + protected IUserManager $userManager, |
|
23 | + protected IConfig $config, |
|
24 | + ) { |
|
25 | + parent::__construct(); |
|
26 | + } |
|
27 | 27 | |
28 | - protected function configure() { |
|
29 | - parent::configure(); |
|
30 | - $this |
|
31 | - ->setName('user:setting') |
|
32 | - ->setDescription('Read and modify user settings') |
|
33 | - ->addArgument( |
|
34 | - 'uid', |
|
35 | - InputArgument::REQUIRED, |
|
36 | - 'Account ID used to login' |
|
37 | - ) |
|
38 | - ->addArgument( |
|
39 | - 'app', |
|
40 | - InputArgument::OPTIONAL, |
|
41 | - 'Restrict the settings to a given app', |
|
42 | - '' |
|
43 | - ) |
|
44 | - ->addArgument( |
|
45 | - 'key', |
|
46 | - InputArgument::OPTIONAL, |
|
47 | - 'Setting key to set, get or delete', |
|
48 | - '' |
|
49 | - ) |
|
50 | - ->addOption( |
|
51 | - 'ignore-missing-user', |
|
52 | - null, |
|
53 | - InputOption::VALUE_NONE, |
|
54 | - 'Use this option to ignore errors when the user does not exist' |
|
55 | - ) |
|
28 | + protected function configure() { |
|
29 | + parent::configure(); |
|
30 | + $this |
|
31 | + ->setName('user:setting') |
|
32 | + ->setDescription('Read and modify user settings') |
|
33 | + ->addArgument( |
|
34 | + 'uid', |
|
35 | + InputArgument::REQUIRED, |
|
36 | + 'Account ID used to login' |
|
37 | + ) |
|
38 | + ->addArgument( |
|
39 | + 'app', |
|
40 | + InputArgument::OPTIONAL, |
|
41 | + 'Restrict the settings to a given app', |
|
42 | + '' |
|
43 | + ) |
|
44 | + ->addArgument( |
|
45 | + 'key', |
|
46 | + InputArgument::OPTIONAL, |
|
47 | + 'Setting key to set, get or delete', |
|
48 | + '' |
|
49 | + ) |
|
50 | + ->addOption( |
|
51 | + 'ignore-missing-user', |
|
52 | + null, |
|
53 | + InputOption::VALUE_NONE, |
|
54 | + 'Use this option to ignore errors when the user does not exist' |
|
55 | + ) |
|
56 | 56 | |
57 | - // Get |
|
58 | - ->addOption( |
|
59 | - 'default-value', |
|
60 | - null, |
|
61 | - InputOption::VALUE_REQUIRED, |
|
62 | - '(Only applicable on get) If no default value is set and the config does not exist, the command will exit with 1' |
|
63 | - ) |
|
57 | + // Get |
|
58 | + ->addOption( |
|
59 | + 'default-value', |
|
60 | + null, |
|
61 | + InputOption::VALUE_REQUIRED, |
|
62 | + '(Only applicable on get) If no default value is set and the config does not exist, the command will exit with 1' |
|
63 | + ) |
|
64 | 64 | |
65 | - // Set |
|
66 | - ->addArgument( |
|
67 | - 'value', |
|
68 | - InputArgument::OPTIONAL, |
|
69 | - 'The new value of the setting', |
|
70 | - null |
|
71 | - ) |
|
72 | - ->addOption( |
|
73 | - 'update-only', |
|
74 | - null, |
|
75 | - InputOption::VALUE_NONE, |
|
76 | - 'Only updates the value, if it is not set before, it is not being added' |
|
77 | - ) |
|
65 | + // Set |
|
66 | + ->addArgument( |
|
67 | + 'value', |
|
68 | + InputArgument::OPTIONAL, |
|
69 | + 'The new value of the setting', |
|
70 | + null |
|
71 | + ) |
|
72 | + ->addOption( |
|
73 | + 'update-only', |
|
74 | + null, |
|
75 | + InputOption::VALUE_NONE, |
|
76 | + 'Only updates the value, if it is not set before, it is not being added' |
|
77 | + ) |
|
78 | 78 | |
79 | - // Delete |
|
80 | - ->addOption( |
|
81 | - 'delete', |
|
82 | - null, |
|
83 | - InputOption::VALUE_NONE, |
|
84 | - 'Specify this option to delete the config' |
|
85 | - ) |
|
86 | - ->addOption( |
|
87 | - 'error-if-not-exists', |
|
88 | - null, |
|
89 | - InputOption::VALUE_NONE, |
|
90 | - 'Checks whether the setting exists before deleting it' |
|
91 | - ) |
|
92 | - ; |
|
93 | - } |
|
79 | + // Delete |
|
80 | + ->addOption( |
|
81 | + 'delete', |
|
82 | + null, |
|
83 | + InputOption::VALUE_NONE, |
|
84 | + 'Specify this option to delete the config' |
|
85 | + ) |
|
86 | + ->addOption( |
|
87 | + 'error-if-not-exists', |
|
88 | + null, |
|
89 | + InputOption::VALUE_NONE, |
|
90 | + 'Checks whether the setting exists before deleting it' |
|
91 | + ) |
|
92 | + ; |
|
93 | + } |
|
94 | 94 | |
95 | - protected function checkInput(InputInterface $input) { |
|
96 | - if (!$input->getOption('ignore-missing-user')) { |
|
97 | - $uid = $input->getArgument('uid'); |
|
98 | - $user = $this->userManager->get($uid); |
|
99 | - if (!$user) { |
|
100 | - throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.'); |
|
101 | - } |
|
102 | - // normalize uid |
|
103 | - $input->setArgument('uid', $user->getUID()); |
|
104 | - } |
|
95 | + protected function checkInput(InputInterface $input) { |
|
96 | + if (!$input->getOption('ignore-missing-user')) { |
|
97 | + $uid = $input->getArgument('uid'); |
|
98 | + $user = $this->userManager->get($uid); |
|
99 | + if (!$user) { |
|
100 | + throw new \InvalidArgumentException('The user "' . $uid . '" does not exist.'); |
|
101 | + } |
|
102 | + // normalize uid |
|
103 | + $input->setArgument('uid', $user->getUID()); |
|
104 | + } |
|
105 | 105 | |
106 | - if ($input->getArgument('key') === '' && $input->hasParameterOption('--default-value')) { |
|
107 | - throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.'); |
|
108 | - } |
|
106 | + if ($input->getArgument('key') === '' && $input->hasParameterOption('--default-value')) { |
|
107 | + throw new \InvalidArgumentException('The "default-value" option can only be used when specifying a key.'); |
|
108 | + } |
|
109 | 109 | |
110 | - if ($input->getArgument('key') === '' && $input->getArgument('value') !== null) { |
|
111 | - throw new \InvalidArgumentException('The value argument can only be used when specifying a key.'); |
|
112 | - } |
|
113 | - if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) { |
|
114 | - throw new \InvalidArgumentException('The value argument can not be used together with "default-value".'); |
|
115 | - } |
|
116 | - if ($input->getOption('update-only') && $input->getArgument('value') === null) { |
|
117 | - throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".'); |
|
118 | - } |
|
110 | + if ($input->getArgument('key') === '' && $input->getArgument('value') !== null) { |
|
111 | + throw new \InvalidArgumentException('The value argument can only be used when specifying a key.'); |
|
112 | + } |
|
113 | + if ($input->getArgument('value') !== null && $input->hasParameterOption('--default-value')) { |
|
114 | + throw new \InvalidArgumentException('The value argument can not be used together with "default-value".'); |
|
115 | + } |
|
116 | + if ($input->getOption('update-only') && $input->getArgument('value') === null) { |
|
117 | + throw new \InvalidArgumentException('The "update-only" option can only be used together with "value".'); |
|
118 | + } |
|
119 | 119 | |
120 | - if ($input->getArgument('key') === '' && $input->getOption('delete')) { |
|
121 | - throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.'); |
|
122 | - } |
|
123 | - if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) { |
|
124 | - throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".'); |
|
125 | - } |
|
126 | - if ($input->getOption('delete') && $input->getArgument('value') !== null) { |
|
127 | - throw new \InvalidArgumentException('The "delete" option can not be used together with "value".'); |
|
128 | - } |
|
129 | - if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) { |
|
130 | - throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".'); |
|
131 | - } |
|
132 | - } |
|
120 | + if ($input->getArgument('key') === '' && $input->getOption('delete')) { |
|
121 | + throw new \InvalidArgumentException('The "delete" option can only be used when specifying a key.'); |
|
122 | + } |
|
123 | + if ($input->getOption('delete') && $input->hasParameterOption('--default-value')) { |
|
124 | + throw new \InvalidArgumentException('The "delete" option can not be used together with "default-value".'); |
|
125 | + } |
|
126 | + if ($input->getOption('delete') && $input->getArgument('value') !== null) { |
|
127 | + throw new \InvalidArgumentException('The "delete" option can not be used together with "value".'); |
|
128 | + } |
|
129 | + if ($input->getOption('error-if-not-exists') && !$input->getOption('delete')) { |
|
130 | + throw new \InvalidArgumentException('The "error-if-not-exists" option can only be used together with "delete".'); |
|
131 | + } |
|
132 | + } |
|
133 | 133 | |
134 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
135 | - try { |
|
136 | - $this->checkInput($input); |
|
137 | - } catch (\InvalidArgumentException $e) { |
|
138 | - $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
139 | - return 1; |
|
140 | - } |
|
134 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
135 | + try { |
|
136 | + $this->checkInput($input); |
|
137 | + } catch (\InvalidArgumentException $e) { |
|
138 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
139 | + return 1; |
|
140 | + } |
|
141 | 141 | |
142 | - $uid = $input->getArgument('uid'); |
|
143 | - $app = $input->getArgument('app'); |
|
144 | - $key = $input->getArgument('key'); |
|
142 | + $uid = $input->getArgument('uid'); |
|
143 | + $app = $input->getArgument('app'); |
|
144 | + $key = $input->getArgument('key'); |
|
145 | 145 | |
146 | - if ($key !== '') { |
|
147 | - $value = $this->config->getUserValue($uid, $app, $key, null); |
|
148 | - if ($input->getArgument('value') !== null) { |
|
149 | - if ($input->hasParameterOption('--update-only') && $value === null) { |
|
150 | - $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
151 | - return 1; |
|
152 | - } |
|
146 | + if ($key !== '') { |
|
147 | + $value = $this->config->getUserValue($uid, $app, $key, null); |
|
148 | + if ($input->getArgument('value') !== null) { |
|
149 | + if ($input->hasParameterOption('--update-only') && $value === null) { |
|
150 | + $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
151 | + return 1; |
|
152 | + } |
|
153 | 153 | |
154 | - if ($app === 'settings' && in_array($key, ['email', 'display_name'])) { |
|
155 | - $user = $this->userManager->get($uid); |
|
156 | - if ($user instanceof IUser) { |
|
157 | - if ($key === 'email') { |
|
158 | - $email = $input->getArgument('value'); |
|
159 | - $user->setSystemEMailAddress(mb_strtolower(trim($email))); |
|
160 | - } elseif ($key === 'display_name') { |
|
161 | - if (!$user->setDisplayName($input->getArgument('value'))) { |
|
162 | - if ($user->getDisplayName() === $input->getArgument('value')) { |
|
163 | - $output->writeln('<error>New and old display name are the same</error>'); |
|
164 | - } elseif ($input->getArgument('value') === '') { |
|
165 | - $output->writeln('<error>New display name can\'t be empty</error>'); |
|
166 | - } else { |
|
167 | - $output->writeln('<error>Could not set display name</error>'); |
|
168 | - } |
|
169 | - return 1; |
|
170 | - } |
|
171 | - } |
|
172 | - // setEmailAddress and setDisplayName both internally set the value |
|
173 | - return 0; |
|
174 | - } |
|
175 | - } |
|
154 | + if ($app === 'settings' && in_array($key, ['email', 'display_name'])) { |
|
155 | + $user = $this->userManager->get($uid); |
|
156 | + if ($user instanceof IUser) { |
|
157 | + if ($key === 'email') { |
|
158 | + $email = $input->getArgument('value'); |
|
159 | + $user->setSystemEMailAddress(mb_strtolower(trim($email))); |
|
160 | + } elseif ($key === 'display_name') { |
|
161 | + if (!$user->setDisplayName($input->getArgument('value'))) { |
|
162 | + if ($user->getDisplayName() === $input->getArgument('value')) { |
|
163 | + $output->writeln('<error>New and old display name are the same</error>'); |
|
164 | + } elseif ($input->getArgument('value') === '') { |
|
165 | + $output->writeln('<error>New display name can\'t be empty</error>'); |
|
166 | + } else { |
|
167 | + $output->writeln('<error>Could not set display name</error>'); |
|
168 | + } |
|
169 | + return 1; |
|
170 | + } |
|
171 | + } |
|
172 | + // setEmailAddress and setDisplayName both internally set the value |
|
173 | + return 0; |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - $this->config->setUserValue($uid, $app, $key, $input->getArgument('value')); |
|
178 | - return 0; |
|
179 | - } elseif ($input->hasParameterOption('--delete')) { |
|
180 | - if ($input->hasParameterOption('--error-if-not-exists') && $value === null) { |
|
181 | - $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
182 | - return 1; |
|
183 | - } |
|
177 | + $this->config->setUserValue($uid, $app, $key, $input->getArgument('value')); |
|
178 | + return 0; |
|
179 | + } elseif ($input->hasParameterOption('--delete')) { |
|
180 | + if ($input->hasParameterOption('--error-if-not-exists') && $value === null) { |
|
181 | + $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
182 | + return 1; |
|
183 | + } |
|
184 | 184 | |
185 | - if ($app === 'settings' && in_array($key, ['email', 'display_name'])) { |
|
186 | - $user = $this->userManager->get($uid); |
|
187 | - if ($user instanceof IUser) { |
|
188 | - if ($key === 'email') { |
|
189 | - $user->setEMailAddress(''); |
|
190 | - // setEmailAddress already deletes the value |
|
191 | - return 0; |
|
192 | - } elseif ($key === 'display_name') { |
|
193 | - $output->writeln('<error>Display name can\'t be deleted.</error>'); |
|
194 | - return 1; |
|
195 | - } |
|
196 | - } |
|
197 | - } |
|
185 | + if ($app === 'settings' && in_array($key, ['email', 'display_name'])) { |
|
186 | + $user = $this->userManager->get($uid); |
|
187 | + if ($user instanceof IUser) { |
|
188 | + if ($key === 'email') { |
|
189 | + $user->setEMailAddress(''); |
|
190 | + // setEmailAddress already deletes the value |
|
191 | + return 0; |
|
192 | + } elseif ($key === 'display_name') { |
|
193 | + $output->writeln('<error>Display name can\'t be deleted.</error>'); |
|
194 | + return 1; |
|
195 | + } |
|
196 | + } |
|
197 | + } |
|
198 | 198 | |
199 | - $this->config->deleteUserValue($uid, $app, $key); |
|
200 | - return 0; |
|
201 | - } elseif ($value !== null) { |
|
202 | - $output->writeln($value); |
|
203 | - return 0; |
|
204 | - } elseif ($input->hasParameterOption('--default-value')) { |
|
205 | - $output->writeln($input->getOption('default-value')); |
|
206 | - return 0; |
|
207 | - } else { |
|
208 | - if ($app === 'settings' && $key === 'display_name') { |
|
209 | - $user = $this->userManager->get($uid); |
|
210 | - $output->writeln($user->getDisplayName()); |
|
211 | - return 0; |
|
212 | - } |
|
213 | - $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
214 | - return 1; |
|
215 | - } |
|
216 | - } else { |
|
217 | - $settings = $this->getUserSettings($uid, $app); |
|
218 | - $this->writeArrayInOutputFormat($input, $output, $settings); |
|
219 | - return 0; |
|
220 | - } |
|
221 | - } |
|
199 | + $this->config->deleteUserValue($uid, $app, $key); |
|
200 | + return 0; |
|
201 | + } elseif ($value !== null) { |
|
202 | + $output->writeln($value); |
|
203 | + return 0; |
|
204 | + } elseif ($input->hasParameterOption('--default-value')) { |
|
205 | + $output->writeln($input->getOption('default-value')); |
|
206 | + return 0; |
|
207 | + } else { |
|
208 | + if ($app === 'settings' && $key === 'display_name') { |
|
209 | + $user = $this->userManager->get($uid); |
|
210 | + $output->writeln($user->getDisplayName()); |
|
211 | + return 0; |
|
212 | + } |
|
213 | + $output->writeln('<error>The setting does not exist for user "' . $uid . '".</error>'); |
|
214 | + return 1; |
|
215 | + } |
|
216 | + } else { |
|
217 | + $settings = $this->getUserSettings($uid, $app); |
|
218 | + $this->writeArrayInOutputFormat($input, $output, $settings); |
|
219 | + return 0; |
|
220 | + } |
|
221 | + } |
|
222 | 222 | |
223 | - protected function getUserSettings(string $uid, string $app): array { |
|
224 | - $settings = $this->config->getAllUserValues($uid); |
|
225 | - if ($app !== '') { |
|
226 | - if (isset($settings[$app])) { |
|
227 | - $settings = [$app => $settings[$app]]; |
|
228 | - } else { |
|
229 | - $settings = []; |
|
230 | - } |
|
231 | - } |
|
223 | + protected function getUserSettings(string $uid, string $app): array { |
|
224 | + $settings = $this->config->getAllUserValues($uid); |
|
225 | + if ($app !== '') { |
|
226 | + if (isset($settings[$app])) { |
|
227 | + $settings = [$app => $settings[$app]]; |
|
228 | + } else { |
|
229 | + $settings = []; |
|
230 | + } |
|
231 | + } |
|
232 | 232 | |
233 | - $user = $this->userManager->get($uid); |
|
234 | - if ($user !== null) { |
|
235 | - // Only add the display name if the user exists |
|
236 | - $settings['settings']['display_name'] = $user->getDisplayName(); |
|
237 | - } |
|
233 | + $user = $this->userManager->get($uid); |
|
234 | + if ($user !== null) { |
|
235 | + // Only add the display name if the user exists |
|
236 | + $settings['settings']['display_name'] = $user->getDisplayName(); |
|
237 | + } |
|
238 | 238 | |
239 | - return $settings; |
|
240 | - } |
|
239 | + return $settings; |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * @param string $argumentName |
|
244 | - * @param CompletionContext $context |
|
245 | - * @return string[] |
|
246 | - */ |
|
247 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
248 | - if ($argumentName === 'uid') { |
|
249 | - return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord())); |
|
250 | - } |
|
251 | - if ($argumentName === 'app') { |
|
252 | - $userId = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
253 | - $settings = $this->getUserSettings($userId, ''); |
|
254 | - return array_keys($settings); |
|
255 | - } |
|
256 | - if ($argumentName === 'key') { |
|
257 | - $userId = $context->getWordAtIndex($context->getWordIndex() - 2); |
|
258 | - $app = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
259 | - $settings = $this->getUserSettings($userId, $app); |
|
260 | - return array_keys($settings[$app]); |
|
261 | - } |
|
262 | - return []; |
|
263 | - } |
|
242 | + /** |
|
243 | + * @param string $argumentName |
|
244 | + * @param CompletionContext $context |
|
245 | + * @return string[] |
|
246 | + */ |
|
247 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
248 | + if ($argumentName === 'uid') { |
|
249 | + return array_map(static fn (IUser $user) => $user->getUID(), $this->userManager->search($context->getCurrentWord())); |
|
250 | + } |
|
251 | + if ($argumentName === 'app') { |
|
252 | + $userId = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
253 | + $settings = $this->getUserSettings($userId, ''); |
|
254 | + return array_keys($settings); |
|
255 | + } |
|
256 | + if ($argumentName === 'key') { |
|
257 | + $userId = $context->getWordAtIndex($context->getWordIndex() - 2); |
|
258 | + $app = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
259 | + $settings = $this->getUserSettings($userId, $app); |
|
260 | + return array_keys($settings[$app]); |
|
261 | + } |
|
262 | + return []; |
|
263 | + } |
|
264 | 264 | } |
@@ -69,8 +69,8 @@ |
||
69 | 69 | 'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', |
70 | 70 | 'statuscode' => $this->getOCSStatus(), |
71 | 71 | 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage ?? '', |
72 | - 'totalitems' => (string)($this->itemsCount ?? ''), |
|
73 | - 'itemsperpage' => (string)($this->itemsPerPage ?? ''), |
|
72 | + 'totalitems' => (string) ($this->itemsCount ?? ''), |
|
73 | + 'itemsperpage' => (string) ($this->itemsPerPage ?? ''), |
|
74 | 74 | ]; |
75 | 75 | |
76 | 76 | return $this->renderResult($meta); |
@@ -17,51 +17,51 @@ |
||
17 | 17 | * @template-extends BaseResponse<Http::STATUS_*, DataResponseType, array<string, mixed>> |
18 | 18 | */ |
19 | 19 | class V1Response extends BaseResponse { |
20 | - /** |
|
21 | - * The V1 endpoint has very limited http status codes basically everything |
|
22 | - * is status 200 except 401 |
|
23 | - * |
|
24 | - * @return Http::STATUS_* |
|
25 | - */ |
|
26 | - public function getStatus() { |
|
27 | - $status = parent::getStatus(); |
|
28 | - if ($status === OCSController::RESPOND_UNAUTHORISED) { |
|
29 | - return Http::STATUS_UNAUTHORIZED; |
|
30 | - } |
|
20 | + /** |
|
21 | + * The V1 endpoint has very limited http status codes basically everything |
|
22 | + * is status 200 except 401 |
|
23 | + * |
|
24 | + * @return Http::STATUS_* |
|
25 | + */ |
|
26 | + public function getStatus() { |
|
27 | + $status = parent::getStatus(); |
|
28 | + if ($status === OCSController::RESPOND_UNAUTHORISED) { |
|
29 | + return Http::STATUS_UNAUTHORIZED; |
|
30 | + } |
|
31 | 31 | |
32 | - return Http::STATUS_OK; |
|
33 | - } |
|
32 | + return Http::STATUS_OK; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * In v1 all OK is 100 |
|
37 | - * |
|
38 | - * @return int |
|
39 | - */ |
|
40 | - public function getOCSStatus() { |
|
41 | - $status = parent::getOCSStatus(); |
|
35 | + /** |
|
36 | + * In v1 all OK is 100 |
|
37 | + * |
|
38 | + * @return int |
|
39 | + */ |
|
40 | + public function getOCSStatus() { |
|
41 | + $status = parent::getOCSStatus(); |
|
42 | 42 | |
43 | - if ($status === Http::STATUS_OK) { |
|
44 | - return 100; |
|
45 | - } |
|
43 | + if ($status === Http::STATUS_OK) { |
|
44 | + return 100; |
|
45 | + } |
|
46 | 46 | |
47 | - return $status; |
|
48 | - } |
|
47 | + return $status; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Construct the meta part of the response |
|
52 | - * And then late the base class render |
|
53 | - * |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function render() { |
|
57 | - $meta = [ |
|
58 | - 'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', |
|
59 | - 'statuscode' => $this->getOCSStatus(), |
|
60 | - 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage ?? '', |
|
61 | - 'totalitems' => (string)($this->itemsCount ?? ''), |
|
62 | - 'itemsperpage' => (string)($this->itemsPerPage ?? ''), |
|
63 | - ]; |
|
50 | + /** |
|
51 | + * Construct the meta part of the response |
|
52 | + * And then late the base class render |
|
53 | + * |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function render() { |
|
57 | + $meta = [ |
|
58 | + 'status' => $this->getOCSStatus() === 100 ? 'ok' : 'failure', |
|
59 | + 'statuscode' => $this->getOCSStatus(), |
|
60 | + 'message' => $this->getOCSStatus() === 100 ? 'OK' : $this->statusMessage ?? '', |
|
61 | + 'totalitems' => (string)($this->itemsCount ?? ''), |
|
62 | + 'itemsperpage' => (string)($this->itemsPerPage ?? ''), |
|
63 | + ]; |
|
64 | 64 | |
65 | - return $this->renderResult($meta); |
|
66 | - } |
|
65 | + return $this->renderResult($meta); |
|
66 | + } |
|
67 | 67 | } |
@@ -34,14 +34,14 @@ |
||
34 | 34 | * @method string getKnownUser() |
35 | 35 | */ |
36 | 36 | class KnownUser extends Entity { |
37 | - /** @var string */ |
|
38 | - protected $knownTo; |
|
37 | + /** @var string */ |
|
38 | + protected $knownTo; |
|
39 | 39 | |
40 | - /** @var string */ |
|
41 | - protected $knownUser; |
|
40 | + /** @var string */ |
|
41 | + protected $knownUser; |
|
42 | 42 | |
43 | - public function __construct() { |
|
44 | - $this->addType('knownTo', 'string'); |
|
45 | - $this->addType('knownUser', 'string'); |
|
46 | - } |
|
43 | + public function __construct() { |
|
44 | + $this->addType('knownTo', 'string'); |
|
45 | + $this->addType('knownUser', 'string'); |
|
46 | + } |
|
47 | 47 | } |
@@ -39,123 +39,123 @@ |
||
39 | 39 | use OCP\IRequest; |
40 | 40 | |
41 | 41 | class OCSMiddleware extends Middleware { |
42 | - /** @var IRequest */ |
|
43 | - private $request; |
|
44 | - |
|
45 | - /** @var int */ |
|
46 | - private $ocsVersion; |
|
47 | - |
|
48 | - /** |
|
49 | - * @param IRequest $request |
|
50 | - */ |
|
51 | - public function __construct(IRequest $request) { |
|
52 | - $this->request = $request; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param Controller $controller |
|
57 | - * @param string $methodName |
|
58 | - */ |
|
59 | - public function beforeController($controller, $methodName) { |
|
60 | - if ($controller instanceof OCSController) { |
|
61 | - if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) { |
|
62 | - $this->ocsVersion = 2; |
|
63 | - } else { |
|
64 | - $this->ocsVersion = 1; |
|
65 | - } |
|
66 | - $controller->setOCSVersion($this->ocsVersion); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param Controller $controller |
|
72 | - * @param string $methodName |
|
73 | - * @param \Exception $exception |
|
74 | - * @throws \Exception |
|
75 | - * @return BaseResponse |
|
76 | - */ |
|
77 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
78 | - if ($controller instanceof OCSController && $exception instanceof OCSException) { |
|
79 | - $code = $exception->getCode(); |
|
80 | - if ($code === 0) { |
|
81 | - $code = \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR; |
|
82 | - } |
|
83 | - |
|
84 | - return $this->buildNewResponse($controller, $code, $exception->getMessage()); |
|
85 | - } |
|
86 | - |
|
87 | - throw $exception; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param Controller $controller |
|
92 | - * @param string $methodName |
|
93 | - * @param Response $response |
|
94 | - * @return \OCP\AppFramework\Http\Response |
|
95 | - */ |
|
96 | - public function afterController($controller, $methodName, Response $response) { |
|
97 | - /* |
|
42 | + /** @var IRequest */ |
|
43 | + private $request; |
|
44 | + |
|
45 | + /** @var int */ |
|
46 | + private $ocsVersion; |
|
47 | + |
|
48 | + /** |
|
49 | + * @param IRequest $request |
|
50 | + */ |
|
51 | + public function __construct(IRequest $request) { |
|
52 | + $this->request = $request; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param Controller $controller |
|
57 | + * @param string $methodName |
|
58 | + */ |
|
59 | + public function beforeController($controller, $methodName) { |
|
60 | + if ($controller instanceof OCSController) { |
|
61 | + if (substr_compare($this->request->getScriptName(), '/ocs/v2.php', -strlen('/ocs/v2.php')) === 0) { |
|
62 | + $this->ocsVersion = 2; |
|
63 | + } else { |
|
64 | + $this->ocsVersion = 1; |
|
65 | + } |
|
66 | + $controller->setOCSVersion($this->ocsVersion); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param Controller $controller |
|
72 | + * @param string $methodName |
|
73 | + * @param \Exception $exception |
|
74 | + * @throws \Exception |
|
75 | + * @return BaseResponse |
|
76 | + */ |
|
77 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
78 | + if ($controller instanceof OCSController && $exception instanceof OCSException) { |
|
79 | + $code = $exception->getCode(); |
|
80 | + if ($code === 0) { |
|
81 | + $code = \OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR; |
|
82 | + } |
|
83 | + |
|
84 | + return $this->buildNewResponse($controller, $code, $exception->getMessage()); |
|
85 | + } |
|
86 | + |
|
87 | + throw $exception; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param Controller $controller |
|
92 | + * @param string $methodName |
|
93 | + * @param Response $response |
|
94 | + * @return \OCP\AppFramework\Http\Response |
|
95 | + */ |
|
96 | + public function afterController($controller, $methodName, Response $response) { |
|
97 | + /* |
|
98 | 98 | * If a different middleware has detected that a request unauthorized or forbidden |
99 | 99 | * we need to catch the response and convert it to a proper OCS response. |
100 | 100 | */ |
101 | - if ($controller instanceof OCSController && !($response instanceof BaseResponse)) { |
|
102 | - if ($response->getStatus() === Http::STATUS_UNAUTHORIZED) { |
|
103 | - $message = ''; |
|
104 | - if ($response instanceof JSONResponse) { |
|
105 | - /** @var DataResponse $response */ |
|
106 | - $message = $response->getData()['message']; |
|
107 | - } |
|
108 | - |
|
109 | - return $this->buildNewResponse($controller, OCSController::RESPOND_UNAUTHORISED, $message); |
|
110 | - } |
|
111 | - if ($response->getStatus() === Http::STATUS_FORBIDDEN) { |
|
112 | - $message = ''; |
|
113 | - if ($response instanceof JSONResponse) { |
|
114 | - /** @var DataResponse $response */ |
|
115 | - $message = $response->getData()['message']; |
|
116 | - } |
|
117 | - |
|
118 | - return $this->buildNewResponse($controller, Http::STATUS_FORBIDDEN, $message); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - return $response; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param Controller $controller |
|
127 | - * @param int $code |
|
128 | - * @param string $message |
|
129 | - * @return V1Response|V2Response |
|
130 | - */ |
|
131 | - private function buildNewResponse(Controller $controller, $code, $message) { |
|
132 | - $format = $this->getFormat($controller); |
|
133 | - |
|
134 | - $data = new DataResponse(); |
|
135 | - $data->setStatus($code); |
|
136 | - if ($this->ocsVersion === 1) { |
|
137 | - $response = new V1Response($data, $format, $message); |
|
138 | - } else { |
|
139 | - $response = new V2Response($data, $format, $message); |
|
140 | - } |
|
141 | - |
|
142 | - return $response; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param Controller $controller |
|
147 | - * @return string |
|
148 | - */ |
|
149 | - private function getFormat(Controller $controller) { |
|
150 | - // get format from the url format or request format parameter |
|
151 | - $format = $this->request->getParam('format'); |
|
152 | - |
|
153 | - // if none is given try the first Accept header |
|
154 | - if ($format === null) { |
|
155 | - $headers = $this->request->getHeader('Accept'); |
|
156 | - $format = $controller->getResponderByHTTPHeader($headers, 'xml'); |
|
157 | - } |
|
158 | - |
|
159 | - return $format; |
|
160 | - } |
|
101 | + if ($controller instanceof OCSController && !($response instanceof BaseResponse)) { |
|
102 | + if ($response->getStatus() === Http::STATUS_UNAUTHORIZED) { |
|
103 | + $message = ''; |
|
104 | + if ($response instanceof JSONResponse) { |
|
105 | + /** @var DataResponse $response */ |
|
106 | + $message = $response->getData()['message']; |
|
107 | + } |
|
108 | + |
|
109 | + return $this->buildNewResponse($controller, OCSController::RESPOND_UNAUTHORISED, $message); |
|
110 | + } |
|
111 | + if ($response->getStatus() === Http::STATUS_FORBIDDEN) { |
|
112 | + $message = ''; |
|
113 | + if ($response instanceof JSONResponse) { |
|
114 | + /** @var DataResponse $response */ |
|
115 | + $message = $response->getData()['message']; |
|
116 | + } |
|
117 | + |
|
118 | + return $this->buildNewResponse($controller, Http::STATUS_FORBIDDEN, $message); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + return $response; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param Controller $controller |
|
127 | + * @param int $code |
|
128 | + * @param string $message |
|
129 | + * @return V1Response|V2Response |
|
130 | + */ |
|
131 | + private function buildNewResponse(Controller $controller, $code, $message) { |
|
132 | + $format = $this->getFormat($controller); |
|
133 | + |
|
134 | + $data = new DataResponse(); |
|
135 | + $data->setStatus($code); |
|
136 | + if ($this->ocsVersion === 1) { |
|
137 | + $response = new V1Response($data, $format, $message); |
|
138 | + } else { |
|
139 | + $response = new V2Response($data, $format, $message); |
|
140 | + } |
|
141 | + |
|
142 | + return $response; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param Controller $controller |
|
147 | + * @return string |
|
148 | + */ |
|
149 | + private function getFormat(Controller $controller) { |
|
150 | + // get format from the url format or request format parameter |
|
151 | + $format = $this->request->getParam('format'); |
|
152 | + |
|
153 | + // if none is given try the first Accept header |
|
154 | + if ($format === null) { |
|
155 | + $headers = $this->request->getHeader('Accept'); |
|
156 | + $format = $controller->getResponderByHTTPHeader($headers, 'xml'); |
|
157 | + } |
|
158 | + |
|
159 | + return $format; |
|
160 | + } |
|
161 | 161 | } |
@@ -31,22 +31,22 @@ |
||
31 | 31 | use OCP\IServerContainer; |
32 | 32 | |
33 | 33 | class BootContext implements IBootContext { |
34 | - /** @var IAppContainer */ |
|
35 | - private $appContainer; |
|
34 | + /** @var IAppContainer */ |
|
35 | + private $appContainer; |
|
36 | 36 | |
37 | - public function __construct(IAppContainer $appContainer) { |
|
38 | - $this->appContainer = $appContainer; |
|
39 | - } |
|
37 | + public function __construct(IAppContainer $appContainer) { |
|
38 | + $this->appContainer = $appContainer; |
|
39 | + } |
|
40 | 40 | |
41 | - public function getAppContainer(): IAppContainer { |
|
42 | - return $this->appContainer; |
|
43 | - } |
|
41 | + public function getAppContainer(): IAppContainer { |
|
42 | + return $this->appContainer; |
|
43 | + } |
|
44 | 44 | |
45 | - public function getServerContainer(): IServerContainer { |
|
46 | - return $this->appContainer->get(IServerContainer::class); |
|
47 | - } |
|
45 | + public function getServerContainer(): IServerContainer { |
|
46 | + return $this->appContainer->get(IServerContainer::class); |
|
47 | + } |
|
48 | 48 | |
49 | - public function injectFn(callable $fn) { |
|
50 | - return (new FunctionInjector($this->appContainer))->injectFn($fn); |
|
51 | - } |
|
49 | + public function injectFn(callable $fn) { |
|
50 | + return (new FunctionInjector($this->appContainer))->injectFn($fn); |
|
51 | + } |
|
52 | 52 | } |