Skip to content

Commit

Permalink
Merge pull request #36089 from nextcloud/enh/noid/ext-storage-default…
Browse files Browse the repository at this point in the history
…-values

Ext storage configs default value support + enable SSL by default
  • Loading branch information
PVince81 authored Jan 16, 2023
2 parents 60eac3f + 4e179e0 commit f0b9b6e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
8 changes: 8 additions & 0 deletions apps/files_external/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,14 @@ MountConfigListView.prototype = _.extend({
newElement = $('<input type="text" class="'+classes.join(' ')+'" data-parameter="'+parameter+'" placeholder="'+ trimmedPlaceholder+'" />');
}

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);
}
Expand Down
3 changes: 2 additions & 1 deletion apps/files_external/lib/Lib/Backend/AmazonS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function __construct(IL10N $l, AccessKey $legacyAuth) {
(new DefinitionParameter('storageClass', $l->t('Storage Class')))
->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')))
Expand Down
3 changes: 2 additions & 1 deletion apps/files_external/lib/Lib/Backend/DAV.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function __construct(IL10N $l, Password $legacyAuth) {
(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)
Expand Down
3 changes: 2 additions & 1 deletion apps/files_external/lib/Lib/Backend/FTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function __construct(IL10N $l, Password $legacyAuth) {
(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)
Expand Down
3 changes: 2 additions & 1 deletion apps/files_external/lib/Lib/Backend/OwnCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function __construct(IL10N $l, Password $legacyAuth) {
(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)
Expand Down
66 changes: 46 additions & 20 deletions apps/files_external/lib/Lib/DefinitionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -85,7 +90,7 @@ public function getText() {
*
* @return int
*/
public function getType() {
public function getType(): int {
return $this->type;
}

Expand All @@ -95,15 +100,31 @@ public function getType() {
* @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';
Expand All @@ -119,15 +140,15 @@ public function getTypeName() {
/**
* @return int
*/
public function getFlags() {
public function getFlags(): int {
return $this->flags;
}

/**
* @param int $flags
* @return self
*/
public function setFlags($flags) {
public function setFlags(int $flags) {
$this->flags = $flags;
return $this;
}
Expand All @@ -136,7 +157,7 @@ public function setFlags($flags) {
* @param int $flag
* @return self
*/
public function setFlag($flag) {
public function setFlag(int $flag) {
$this->flags |= $flag;
return $this;
}
Expand All @@ -145,7 +166,7 @@ public function setFlag($flag) {
* @param int $flag
* @return bool
*/
public function isFlagSet($flag) {
public function isFlagSet(int $flag): bool {
return (bool)($this->flags & $flag);
}

Expand All @@ -169,15 +190,20 @@ public function setTooltip(string $tooltip) {
* 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);
}

Expand All @@ -188,7 +214,7 @@ public function isOptional() {
* @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)) {
Expand Down
3 changes: 3 additions & 0 deletions apps/files_external/tests/DefinitionParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ public function testJsonSerialization() {
], $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);
$param->setFlag(Param::FLAG_OPTIONAL);
$param->setDefaultValue(null);
$this->assertEquals([
'value' => 'bar',
'flags' => Param::FLAG_OPTIONAL,
Expand Down

0 comments on commit f0b9b6e

Please sign in to comment.