From a0f70988f426aa3c327c95537f3e62bae2461707 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 11 Jan 2023 14:39:39 +0100 Subject: Extend ext storage params to contain default value Extend the external storage configuration parameters definition to allow to specify a default value Signed-off-by: Vincent Petry --- apps/files_external/js/settings.js | 8 +++ .../files_external/lib/Lib/DefinitionParameter.php | 66 +++++++++++++++------- .../tests/DefinitionParameterTest.php | 2 + 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/apps/files_external/js/settings.js b/apps/files_external/js/settings.js index 765850e2ef2..5228b958b67 100644 --- a/apps/files_external/js/settings.js +++ b/apps/files_external/js/settings.js @@ -1054,6 +1054,14 @@ MountConfigListView.prototype = _.extend({ newElement = $(''); } + if (placeholder.defaultValue) { + if (placeholder.type === MountConfigListView.ParameterTypes.BOOLEAN) { + newElement.find('input').prop('checked', placeholder.defaultValue); + } else { + newElement.val(placeholder.defaultValue); + } + } + if (placeholder.tooltip) { newElement.attr('title', placeholder.tooltip); } diff --git a/apps/files_external/lib/Lib/DefinitionParameter.php b/apps/files_external/lib/Lib/DefinitionParameter.php index 6b081d5a089..ce0a37b4e9b 100644 --- a/apps/files_external/lib/Lib/DefinitionParameter.php +++ b/apps/files_external/lib/Lib/DefinitionParameter.php @@ -43,40 +43,45 @@ class DefinitionParameter implements \JsonSerializable { public const FLAG_USER_PROVIDED = 2; /** @var string name of parameter */ - private $name; + private string $name; /** @var string human-readable parameter text */ - private $text; + private string $text; /** @var string human-readable parameter tooltip */ - private $tooltip = ''; + private string $tooltip = ''; /** @var int value type, see self::VALUE_* constants */ - private $type = self::VALUE_TEXT; + private int $type = self::VALUE_TEXT; /** @var int flags, see self::FLAG_* constants */ - private $flags = self::FLAG_NONE; + private int $flags = self::FLAG_NONE; + + /** @var mixed */ + private $defaultValue; /** - * @param string $name - * @param string $text + * @param string $name parameter name + * @param string $text parameter description + * @param mixed $defaultValue default value */ - public function __construct($name, $text) { + public function __construct(string $name, string $text, $defaultValue = null) { $this->name = $name; $this->text = $text; + $this->defaultValue = $defaultValue; } /** * @return string */ - public function getName() { + public function getName(): string { return $this->name; } /** * @return string */ - public function getText() { + public function getText(): string { return $this->text; } @@ -85,7 +90,7 @@ class DefinitionParameter implements \JsonSerializable { * * @return int */ - public function getType() { + public function getType(): int { return $this->type; } @@ -95,15 +100,31 @@ class DefinitionParameter implements \JsonSerializable { * @param int $type * @return self */ - public function setType($type) { + public function setType(int $type) { $this->type = $type; return $this; } + /** + * @return mixed default value + */ + public function getDefaultValue() { + return $this->defaultValue; + } + + /** + * @param mixed $defaultValue default value + * @return self + */ + public function setDefaultValue($defaultValue) { + $this->defaultValue = $defaultValue; + return $this; + } + /** * @return string */ - public function getTypeName() { + public function getTypeName(): string { switch ($this->type) { case self::VALUE_BOOLEAN: return 'boolean'; @@ -119,7 +140,7 @@ class DefinitionParameter implements \JsonSerializable { /** * @return int */ - public function getFlags() { + public function getFlags(): int { return $this->flags; } @@ -127,7 +148,7 @@ class DefinitionParameter implements \JsonSerializable { * @param int $flags * @return self */ - public function setFlags($flags) { + public function setFlags(int $flags) { $this->flags = $flags; return $this; } @@ -136,7 +157,7 @@ class DefinitionParameter implements \JsonSerializable { * @param int $flag * @return self */ - public function setFlag($flag) { + public function setFlag(int $flag) { $this->flags |= $flag; return $this; } @@ -145,7 +166,7 @@ class DefinitionParameter implements \JsonSerializable { * @param int $flag * @return bool */ - public function isFlagSet($flag) { + public function isFlagSet(int $flag): bool { return (bool)($this->flags & $flag); } @@ -169,15 +190,20 @@ class DefinitionParameter implements \JsonSerializable { * Serialize into JSON for client-side JS */ public function jsonSerialize(): array { - return [ + $result = [ 'value' => $this->getText(), 'flags' => $this->getFlags(), 'type' => $this->getType(), 'tooltip' => $this->getTooltip(), ]; + $defaultValue = $this->getDefaultValue(); + if ($defaultValue) { + $result['defaultValue'] = $defaultValue; + } + return $result; } - public function isOptional() { + public function isOptional(): bool { return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED); } @@ -188,7 +214,7 @@ class DefinitionParameter implements \JsonSerializable { * @param mixed $value Value to check * @return bool success */ - public function validateValue(&$value) { + public function validateValue(&$value): bool { switch ($this->getType()) { case self::VALUE_BOOLEAN: if (!is_bool($value)) { diff --git a/apps/files_external/tests/DefinitionParameterTest.php b/apps/files_external/tests/DefinitionParameterTest.php index 00df3e0aee1..72e8c013730 100644 --- a/apps/files_external/tests/DefinitionParameterTest.php +++ b/apps/files_external/tests/DefinitionParameterTest.php @@ -36,11 +36,13 @@ class DefinitionParameterTest extends \Test\TestCase { ], $param->jsonSerialize()); $param->setType(Param::VALUE_BOOLEAN); + $param->setDefaultValue(true); $this->assertEquals([ 'value' => 'bar', 'flags' => 0, 'type' => Param::VALUE_BOOLEAN, 'tooltip' => '', + 'defaultValue' => true, ], $param->jsonSerialize()); $param->setType(Param::VALUE_PASSWORD); -- cgit v1.2.3 From 4e179e0e07c848e3dcb31d1214a2516181e47d45 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 11 Jan 2023 14:40:10 +0100 Subject: Enable SSL by default in ext storage configs Signed-off-by: Vincent Petry --- apps/files_external/lib/Lib/Backend/AmazonS3.php | 3 ++- apps/files_external/lib/Lib/Backend/DAV.php | 3 ++- apps/files_external/lib/Lib/Backend/FTP.php | 3 ++- apps/files_external/lib/Lib/Backend/OwnCloud.php | 3 ++- apps/files_external/tests/DefinitionParameterTest.php | 1 + 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/files_external/lib/Lib/Backend/AmazonS3.php b/apps/files_external/lib/Lib/Backend/AmazonS3.php index 26da263f094..aae0def2b46 100644 --- a/apps/files_external/lib/Lib/Backend/AmazonS3.php +++ b/apps/files_external/lib/Lib/Backend/AmazonS3.php @@ -48,7 +48,8 @@ class AmazonS3 extends Backend { (new DefinitionParameter('region', $l->t('Region'))) ->setFlag(DefinitionParameter::FLAG_OPTIONAL), (new DefinitionParameter('use_ssl', $l->t('Enable SSL'))) - ->setType(DefinitionParameter::VALUE_BOOLEAN), + ->setType(DefinitionParameter::VALUE_BOOLEAN) + ->setDefaultValue(true), (new DefinitionParameter('use_path_style', $l->t('Enable Path Style'))) ->setType(DefinitionParameter::VALUE_BOOLEAN), (new DefinitionParameter('legacy_auth', $l->t('Legacy (v2) authentication'))) diff --git a/apps/files_external/lib/Lib/Backend/DAV.php b/apps/files_external/lib/Lib/Backend/DAV.php index cf16677334d..71c97e639ff 100644 --- a/apps/files_external/lib/Lib/Backend/DAV.php +++ b/apps/files_external/lib/Lib/Backend/DAV.php @@ -43,7 +43,8 @@ class DAV extends Backend { (new DefinitionParameter('root', $l->t('Remote subfolder'))) ->setFlag(DefinitionParameter::FLAG_OPTIONAL), (new DefinitionParameter('secure', $l->t('Secure https://'))) - ->setType(DefinitionParameter::VALUE_BOOLEAN), + ->setType(DefinitionParameter::VALUE_BOOLEAN) + ->setDefaultValue(true), ]) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->setLegacyAuthMechanism($legacyAuth) diff --git a/apps/files_external/lib/Lib/Backend/FTP.php b/apps/files_external/lib/Lib/Backend/FTP.php index 587f3e68535..d7c6e3bebd6 100644 --- a/apps/files_external/lib/Lib/Backend/FTP.php +++ b/apps/files_external/lib/Lib/Backend/FTP.php @@ -43,7 +43,8 @@ class FTP extends Backend { (new DefinitionParameter('root', $l->t('Remote subfolder'))) ->setFlag(DefinitionParameter::FLAG_OPTIONAL), (new DefinitionParameter('secure', $l->t('Secure ftps://'))) - ->setType(DefinitionParameter::VALUE_BOOLEAN), + ->setType(DefinitionParameter::VALUE_BOOLEAN) + ->setDefaultValue(true), ]) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->setLegacyAuthMechanism($legacyAuth) diff --git a/apps/files_external/lib/Lib/Backend/OwnCloud.php b/apps/files_external/lib/Lib/Backend/OwnCloud.php index 8b33b98b5c4..97297b6a977 100644 --- a/apps/files_external/lib/Lib/Backend/OwnCloud.php +++ b/apps/files_external/lib/Lib/Backend/OwnCloud.php @@ -41,7 +41,8 @@ class OwnCloud extends Backend { (new DefinitionParameter('root', $l->t('Remote subfolder'))) ->setFlag(DefinitionParameter::FLAG_OPTIONAL), (new DefinitionParameter('secure', $l->t('Secure https://'))) - ->setType(DefinitionParameter::VALUE_BOOLEAN), + ->setType(DefinitionParameter::VALUE_BOOLEAN) + ->setDefaultValue(true), ]) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->setLegacyAuthMechanism($legacyAuth) diff --git a/apps/files_external/tests/DefinitionParameterTest.php b/apps/files_external/tests/DefinitionParameterTest.php index 72e8c013730..04d5f6762c5 100644 --- a/apps/files_external/tests/DefinitionParameterTest.php +++ b/apps/files_external/tests/DefinitionParameterTest.php @@ -47,6 +47,7 @@ class DefinitionParameterTest extends \Test\TestCase { $param->setType(Param::VALUE_PASSWORD); $param->setFlag(Param::FLAG_OPTIONAL); + $param->setDefaultValue(null); $this->assertEquals([ 'value' => 'bar', 'flags' => Param::FLAG_OPTIONAL, -- cgit v1.2.3