Skip to content

Commit 17bc35e

Browse files
Merge pull request #19915 from nextcloud/external-storage-password-placeholders
Use placeholder values for password fields in external storage webui
2 parents a54c4b6 + 0d112d7 commit 17bc35e

File tree

8 files changed

+65
-37
lines changed

8 files changed

+65
-37
lines changed

apps/files_external/lib/Controller/GlobalStoragesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function create(
113113
$this->updateStorageStatus($newStorage);
114114

115115
return new DataResponse(
116-
$newStorage,
116+
$this->formatStorageForUI($newStorage),
117117
Http::STATUS_CREATED
118118
);
119119
}
@@ -180,7 +180,7 @@ public function update(
180180
$this->updateStorageStatus($storage, $testOnly);
181181

182182
return new DataResponse(
183-
$storage,
183+
$this->formatStorageForUI($storage),
184184
Http::STATUS_OK
185185
);
186186

apps/files_external/lib/Controller/StoragesController.php

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
use OCA\Files_External\Lib\Auth\AuthMechanism;
3333
use OCA\Files_External\Lib\Backend\Backend;
34+
use OCA\Files_External\Lib\DefinitionParameter;
3435
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
3536
use OCA\Files_External\Lib\StorageConfig;
3637
use OCA\Files_External\NotFoundException;
@@ -146,19 +147,19 @@ protected function validate(StorageConfig $storage) {
146147
$mountPoint = $storage->getMountPoint();
147148
if ($mountPoint === '') {
148149
return new DataResponse(
149-
array(
150-
'message' => (string)$this->l10n->t('Invalid mount point')
151-
),
150+
[
151+
'message' => (string)$this->l10n->t('Invalid mount point'),
152+
],
152153
Http::STATUS_UNPROCESSABLE_ENTITY
153154
);
154155
}
155156

156157
if ($storage->getBackendOption('objectstore')) {
157158
// objectstore must not be sent from client side
158159
return new DataResponse(
159-
array(
160-
'message' => (string)$this->l10n->t('Objectstore forbidden')
161-
),
160+
[
161+
'message' => (string)$this->l10n->t('Objectstore forbidden'),
162+
],
162163
Http::STATUS_UNPROCESSABLE_ENTITY
163164
);
164165
}
@@ -170,52 +171,52 @@ protected function validate(StorageConfig $storage) {
170171
if ($backend->checkDependencies()) {
171172
// invalid backend
172173
return new DataResponse(
173-
array(
174+
[
174175
'message' => (string)$this->l10n->t('Invalid storage backend "%s"', [
175-
$backend->getIdentifier()
176-
])
177-
),
176+
$backend->getIdentifier(),
177+
]),
178+
],
178179
Http::STATUS_UNPROCESSABLE_ENTITY
179180
);
180181
}
181182

182183
if (!$backend->isVisibleFor($this->service->getVisibilityType())) {
183184
// not permitted to use backend
184185
return new DataResponse(
185-
array(
186+
[
186187
'message' => (string)$this->l10n->t('Not permitted to use backend "%s"', [
187-
$backend->getIdentifier()
188-
])
189-
),
188+
$backend->getIdentifier(),
189+
]),
190+
],
190191
Http::STATUS_UNPROCESSABLE_ENTITY
191192
);
192193
}
193194
if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) {
194195
// not permitted to use auth mechanism
195196
return new DataResponse(
196-
array(
197+
[
197198
'message' => (string)$this->l10n->t('Not permitted to use authentication mechanism "%s"', [
198-
$authMechanism->getIdentifier()
199-
])
200-
),
199+
$authMechanism->getIdentifier(),
200+
]),
201+
],
201202
Http::STATUS_UNPROCESSABLE_ENTITY
202203
);
203204
}
204205

205206
if (!$backend->validateStorage($storage)) {
206207
// unsatisfied parameters
207208
return new DataResponse(
208-
array(
209-
'message' => (string)$this->l10n->t('Unsatisfied backend parameters')
210-
),
209+
[
210+
'message' => (string)$this->l10n->t('Unsatisfied backend parameters'),
211+
],
211212
Http::STATUS_UNPROCESSABLE_ENTITY
212213
);
213214
}
214215
if (!$authMechanism->validateStorage($storage)) {
215216
// unsatisfied parameters
216217
return new DataResponse(
217218
[
218-
'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters')
219+
'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters'),
219220
],
220221
Http::STATUS_UNPROCESSABLE_ENTITY
221222
);
@@ -272,7 +273,7 @@ protected function updateStorageStatus(StorageConfig &$storage, $testOnly = true
272273
// FIXME: convert storage exceptions to StorageNotAvailableException
273274
$storage->setStatus(
274275
StorageNotAvailableException::STATUS_ERROR,
275-
get_class($e).': '.$e->getMessage()
276+
get_class($e) . ': ' . $e->getMessage()
276277
);
277278
}
278279
}
@@ -283,14 +284,37 @@ protected function updateStorageStatus(StorageConfig &$storage, $testOnly = true
283284
* @return DataResponse
284285
*/
285286
public function index() {
286-
$storages = $this->service->getStorages();
287+
$storages = $this->formatStoragesForUI($this->service->getStorages());
287288

288289
return new DataResponse(
289290
$storages,
290291
Http::STATUS_OK
291292
);
292293
}
293294

295+
protected function formatStoragesForUI(array $storages): array {
296+
return array_map(function ($storage) {
297+
return $this->formatStorageForUI($storage);
298+
}, $storages);
299+
}
300+
301+
protected function formatStorageForUI(StorageConfig $storage): StorageConfig {
302+
/** @var DefinitionParameter[] $parameters */
303+
$parameters = array_merge($storage->getBackend()->getParameters(), $storage->getAuthMechanism()->getParameters());
304+
305+
$options = $storage->getBackendOptions();
306+
foreach ($options as $key => $value) {
307+
foreach ($parameters as $parameter) {
308+
if ($parameter->getName() === $key && $parameter->getType() === DefinitionParameter::VALUE_PASSWORD) {
309+
$storage->setBackendOption($key, DefinitionParameter::UNMODIFIED_PLACEHOLDER);
310+
break;
311+
}
312+
}
313+
}
314+
315+
return $storage;
316+
}
317+
294318
/**
295319
* Get an external storage entry.
296320
*
@@ -307,14 +331,14 @@ public function show($id, $testOnly = true) {
307331
} catch (NotFoundException $e) {
308332
return new DataResponse(
309333
[
310-
'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id))
334+
'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]),
311335
],
312336
Http::STATUS_NOT_FOUND
313337
);
314338
}
315339

316340
return new DataResponse(
317-
$storage,
341+
$this->formatStorageForUI($storage),
318342
Http::STATUS_OK
319343
);
320344
}
@@ -332,7 +356,7 @@ public function destroy($id) {
332356
} catch (NotFoundException $e) {
333357
return new DataResponse(
334358
[
335-
'message' => (string)$this->l10n->t('Storage with ID "%d" not found', array($id))
359+
'message' => (string)$this->l10n->t('Storage with ID "%d" not found', [$id]),
336360
],
337361
Http::STATUS_NOT_FOUND
338362
);

apps/files_external/lib/Controller/UserGlobalStoragesController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function __construct(
8585
* @NoAdminRequired
8686
*/
8787
public function index() {
88-
$storages = $this->service->getUniqueStorages();
88+
$storages = $this->formatStoragesForUI($this->service->getUniqueStorages());
8989

9090
// remove configuration data, this must be kept private
9191
foreach ($storages as $storage) {
@@ -133,7 +133,7 @@ public function show($id, $testOnly = true) {
133133
$this->sanitizeStorage($storage);
134134

135135
return new DataResponse(
136-
$storage,
136+
$this->formatStorageForUI($storage),
137137
Http::STATUS_OK
138138
);
139139
}
@@ -182,7 +182,7 @@ public function update(
182182
$this->sanitizeStorage($storage);
183183

184184
return new DataResponse(
185-
$storage,
185+
$this->formatStorageForUI($storage),
186186
Http::STATUS_OK
187187
);
188188

apps/files_external/lib/Controller/UserStoragesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function create(
148148
$this->updateStorageStatus($newStorage);
149149

150150
return new DataResponse(
151-
$newStorage,
151+
$this->formatStorageForUI($newStorage),
152152
Http::STATUS_CREATED
153153
);
154154
}
@@ -208,7 +208,7 @@ public function update(
208208
$this->updateStorageStatus($storage, $testOnly);
209209

210210
return new DataResponse(
211-
$storage,
211+
$this->formatStorageForUI($storage),
212212
Http::STATUS_OK
213213
);
214214

apps/files_external/lib/Lib/Auth/AuthMechanism.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
* Object can affect storage mounting
5252
*/
5353
class AuthMechanism implements \JsonSerializable {
54-
5554
/** Standard authentication schemes */
5655
const SCHEME_NULL = 'null';
5756
const SCHEME_BUILTIN = 'builtin';

apps/files_external/lib/Lib/DefinitionParameter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
* Parameter for an external storage definition
2828
*/
2929
class DefinitionParameter implements \JsonSerializable {
30+
// placeholder value for password fields, when the client updates a storage configuration
31+
// placeholder values are ignored and the field is left unmodified
32+
const UNMODIFIED_PLACEHOLDER = '__unmodified__';
3033

3134
/** Value constants */
3235
const VALUE_TEXT = 0;

apps/files_external/lib/Lib/FrontendDefinitionTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,4 @@ public function validateStorageDefinition(StorageConfig $storage) {
154154
}
155155
return true;
156156
}
157-
158157
}

apps/files_external/lib/Service/StoragesService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCA\Files_External\Lib\Auth\InvalidAuth;
3737
use OCA\Files_External\Lib\Backend\Backend;
3838
use OCA\Files_External\Lib\Backend\InvalidBackend;
39+
use OCA\Files_External\Lib\DefinitionParameter;
3940
use OCA\Files_External\Lib\StorageConfig;
4041
use OCA\Files_External\NotFoundException;
4142
use OCP\Files\Config\IUserMountCache;
@@ -427,7 +428,9 @@ public function updateStorage(StorageConfig $updatedStorage) {
427428
$changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions());
428429

429430
foreach ($changedConfig as $key => $value) {
430-
$this->dbConfig->setConfig($id, $key, $value);
431+
if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) {
432+
$this->dbConfig->setConfig($id, $key, $value);
433+
}
431434
}
432435
foreach ($changedOptions as $key => $value) {
433436
$this->dbConfig->setOption($id, $key, $value);

0 commit comments

Comments
 (0)