Greenfield: Rename enabledOnly to enabled?

Allows the filter to work in both directions instead
This commit is contained in:
Kukks 2021-07-26 11:12:44 +02:00 committed by Andrew Camilleri
parent 4d538c61b1
commit f4df850d25
9 changed files with 94 additions and 41 deletions

View file

@ -9,13 +9,19 @@ namespace BTCPayServer.Client
public partial class BTCPayServerClient public partial class BTCPayServerClient
{ {
public virtual async Task<IEnumerable<LightningNetworkPaymentMethodData>> public virtual async Task<IEnumerable<LightningNetworkPaymentMethodData>>
GetStoreLightningNetworkPaymentMethods(string storeId, bool enabledOnly = false, GetStoreLightningNetworkPaymentMethods(string storeId, bool? enabled = null,
CancellationToken token = default) CancellationToken token = default)
{ {
var query = new Dictionary<string, object>();
if (enabled != null)
{
query.Add(nameof(enabled), enabled);
}
var response = var response =
await _httpClient.SendAsync( await _httpClient.SendAsync(
CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/LightningNetwork", CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/LightningNetwork",
new Dictionary<string, object>() {{nameof(enabledOnly), enabledOnly}}), token); query), token);
return await HandleResponse<IEnumerable<LightningNetworkPaymentMethodData>>(response); return await HandleResponse<IEnumerable<LightningNetworkPaymentMethodData>>(response);
} }

View file

@ -9,13 +9,19 @@ namespace BTCPayServer.Client
public partial class BTCPayServerClient public partial class BTCPayServerClient
{ {
public virtual async Task<IEnumerable<OnChainPaymentMethodData>> GetStoreOnChainPaymentMethods(string storeId, public virtual async Task<IEnumerable<OnChainPaymentMethodData>> GetStoreOnChainPaymentMethods(string storeId,
bool enabledOnly = false, bool? enabled = null,
CancellationToken token = default) CancellationToken token = default)
{ {
var query = new Dictionary<string, object>();
if (enabled != null)
{
query.Add(nameof(enabled), enabled);
}
var response = var response =
await _httpClient.SendAsync( await _httpClient.SendAsync(
CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/Onchain", CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/Onchain",
new Dictionary<string, object>() {{nameof(enabledOnly), enabledOnly}}), token); query), token);
return await HandleResponse<IEnumerable<OnChainPaymentMethodData>>(response); return await HandleResponse<IEnumerable<OnChainPaymentMethodData>>(response);
} }

View file

@ -8,13 +8,19 @@ namespace BTCPayServer.Client
public partial class BTCPayServerClient public partial class BTCPayServerClient
{ {
public virtual async Task<Dictionary<string, GenericPaymentMethodData>> GetStorePaymentMethods(string storeId, public virtual async Task<Dictionary<string, GenericPaymentMethodData>> GetStorePaymentMethods(string storeId,
bool enabledOnly = false, bool? enabled = null,
CancellationToken token = default) CancellationToken token = default)
{ {
var query = new Dictionary<string, object>();
if (enabled != null)
{
query.Add(nameof(enabled), enabled);
}
var response = var response =
await _httpClient.SendAsync( await _httpClient.SendAsync(
CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods", CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods",
new Dictionary<string, object>() {{nameof(enabledOnly), enabledOnly}}), token); query), token);
return await HandleResponse<Dictionary<string, GenericPaymentMethodData>>(response); return await HandleResponse<Dictionary<string, GenericPaymentMethodData>>(response);
} }
} }

View file

@ -42,7 +42,8 @@ namespace BTCPayServer.Controllers.GreenField
_cssThemeManager = cssThemeManager; _cssThemeManager = cssThemeManager;
} }
public static IEnumerable<LightningNetworkPaymentMethodData> GetLightningPaymentMethods(StoreData store, BTCPayNetworkProvider networkProvider, bool enabledOnly = false) public static IEnumerable<LightningNetworkPaymentMethodData> GetLightningPaymentMethods(StoreData store,
BTCPayNetworkProvider networkProvider, bool? enabled)
{ {
var blob = store.GetStoreBlob(); var blob = store.GetStoreBlob();
var excludedPaymentMethods = blob.GetExcludedPaymentMethods(); var excludedPaymentMethods = blob.GetExcludedPaymentMethods();
@ -53,20 +54,21 @@ namespace BTCPayServer.Controllers.GreenField
.Select(paymentMethod => .Select(paymentMethod =>
new LightningNetworkPaymentMethodData( new LightningNetworkPaymentMethodData(
paymentMethod.PaymentId.CryptoCode, paymentMethod.PaymentId.CryptoCode,
paymentMethod.GetExternalLightningUrl()?.ToString() ?? paymentMethod.GetDisplayableConnectionString(), paymentMethod.GetExternalLightningUrl()?.ToString() ??
paymentMethod.GetDisplayableConnectionString(),
!excludedPaymentMethods.Match(paymentMethod.PaymentId) !excludedPaymentMethods.Match(paymentMethod.PaymentId)
) )
) )
.Where((result) => !enabledOnly || result.Enabled) .Where((result) => enabled is null || enabled == result.Enabled)
.ToList(); .ToList();
} }
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/LightningNetwork")] [HttpGet("~/api/v1/stores/{storeId}/payment-methods/LightningNetwork")]
public ActionResult<IEnumerable<LightningNetworkPaymentMethodData>> GetLightningPaymentMethods( public ActionResult<IEnumerable<LightningNetworkPaymentMethodData>> GetLightningPaymentMethods(
[FromQuery] bool enabledOnly = false) [FromQuery] bool? enabled)
{ {
return Ok(GetLightningPaymentMethods(Store, _btcPayNetworkProvider, enabledOnly)); return Ok(GetLightningPaymentMethods(Store, _btcPayNetworkProvider, enabled));
} }
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
@ -83,6 +85,7 @@ namespace BTCPayServer.Controllers.GreenField
{ {
return NotFound(); return NotFound();
} }
return Ok(method); return Ok(method);
} }
@ -132,9 +135,11 @@ namespace BTCPayServer.Controllers.GreenField
{ {
if (!await CanUseInternalLightning()) if (!await CanUseInternalLightning())
{ {
ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), $"You are not authorized to use the internal lightning node"); ModelState.AddModelError(nameof(paymentMethodData.ConnectionString),
$"You are not authorized to use the internal lightning node");
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
} }
paymentMethod = new Payments.Lightning.LightningSupportedPaymentMethod() paymentMethod = new Payments.Lightning.LightningSupportedPaymentMethod()
{ {
CryptoCode = paymentMethodId.CryptoCode CryptoCode = paymentMethodId.CryptoCode
@ -149,17 +154,21 @@ namespace BTCPayServer.Controllers.GreenField
ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), $"Invalid URL ({error})"); ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), $"Invalid URL ({error})");
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
} }
if (connectionString.ConnectionType == LightningConnectionType.LndGRPC) if (connectionString.ConnectionType == LightningConnectionType.LndGRPC)
{ {
ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), ModelState.AddModelError(nameof(paymentMethodData.ConnectionString),
$"BTCPay does not support gRPC connections"); $"BTCPay does not support gRPC connections");
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
} }
if (!await CanManageServer() && !connectionString.IsSafe()) if (!await CanManageServer() && !connectionString.IsSafe())
{ {
ModelState.AddModelError(nameof(paymentMethodData.ConnectionString), $"You do not have 'btcpay.server.canmodifyserversettings' rights, so the connection string should not contain 'cookiefilepath', 'macaroondirectorypath', 'macaroonfilepath', and should not point to a local ip or to a dns name ending with '.internal', '.local', '.lan' or '.'."); ModelState.AddModelError(nameof(paymentMethodData.ConnectionString),
$"You do not have 'btcpay.server.canmodifyserversettings' rights, so the connection string should not contain 'cookiefilepath', 'macaroondirectorypath', 'macaroonfilepath', and should not point to a local ip or to a dns name ending with '.internal', '.local', '.lan' or '.'.");
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
} }
paymentMethod = new Payments.Lightning.LightningSupportedPaymentMethod() paymentMethod = new Payments.Lightning.LightningSupportedPaymentMethod()
{ {
CryptoCode = paymentMethodId.CryptoCode CryptoCode = paymentMethodId.CryptoCode
@ -177,7 +186,8 @@ namespace BTCPayServer.Controllers.GreenField
return Ok(GetExistingLightningLikePaymentMethod(cryptoCode, store)); return Ok(GetExistingLightningLikePaymentMethod(cryptoCode, store));
} }
private LightningNetworkPaymentMethodData GetExistingLightningLikePaymentMethod(string cryptoCode, StoreData store = null) private LightningNetworkPaymentMethodData GetExistingLightningLikePaymentMethod(string cryptoCode,
StoreData store = null)
{ {
store ??= Store; store ??= Store;
var storeBlob = store.GetStoreBlob(); var storeBlob = store.GetStoreBlob();
@ -207,6 +217,7 @@ namespace BTCPayServer.Controllers.GreenField
(await _authorizationService.AuthorizeAsync(User, null, (await _authorizationService.AuthorizeAsync(User, null,
new PolicyRequirement(Policies.CanUseInternalLightningNode))).Succeeded; new PolicyRequirement(Policies.CanUseInternalLightningNode))).Succeeded;
} }
private async Task<bool> CanManageServer() private async Task<bool> CanManageServer()
{ {
return return

View file

@ -35,7 +35,8 @@ namespace BTCPayServer.Controllers.GreenField
_walletProvider = walletProvider; _walletProvider = walletProvider;
} }
public static IEnumerable<OnChainPaymentMethodData> GetOnChainPaymentMethods(StoreData store, BTCPayNetworkProvider networkProvider, bool enabledOnly = false) public static IEnumerable<OnChainPaymentMethodData> GetOnChainPaymentMethods(StoreData store,
BTCPayNetworkProvider networkProvider, bool? enabled)
{ {
var blob = store.GetStoreBlob(); var blob = store.GetStoreBlob();
var excludedPaymentMethods = blob.GetExcludedPaymentMethods(); var excludedPaymentMethods = blob.GetExcludedPaymentMethods();
@ -46,16 +47,16 @@ namespace BTCPayServer.Controllers.GreenField
.Select(strategy => .Select(strategy =>
new OnChainPaymentMethodData(strategy.PaymentId.CryptoCode, new OnChainPaymentMethodData(strategy.PaymentId.CryptoCode,
strategy.AccountDerivation.ToString(), !excludedPaymentMethods.Match(strategy.PaymentId))) strategy.AccountDerivation.ToString(), !excludedPaymentMethods.Match(strategy.PaymentId)))
.Where((result) => !enabledOnly || result.Enabled) .Where((result) => enabled is null || enabled == result.Enabled)
.ToList(); .ToList();
} }
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain")] [HttpGet("~/api/v1/stores/{storeId}/payment-methods/onchain")]
public ActionResult<IEnumerable<OnChainPaymentMethodData>> GetOnChainPaymentMethods( public ActionResult<IEnumerable<OnChainPaymentMethodData>> GetOnChainPaymentMethods(
[FromQuery] bool enabledOnly = false) [FromQuery] bool? enabled)
{ {
return Ok(GetOnChainPaymentMethods(Store, _btcPayNetworkProvider, enabledOnly)); return Ok(GetOnChainPaymentMethods(Store, _btcPayNetworkProvider, enabled));
} }
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
@ -72,6 +73,7 @@ namespace BTCPayServer.Controllers.GreenField
{ {
return NotFound(); return NotFound();
} }
return Ok(method); return Ok(method);
} }
@ -91,6 +93,7 @@ namespace BTCPayServer.Controllers.GreenField
{ {
return NotFound(); return NotFound();
} }
try try
{ {
var strategy = DerivationSchemeSettings.Parse(paymentMethod.DerivationScheme, network); var strategy = DerivationSchemeSettings.Parse(paymentMethod.DerivationScheme, network);
@ -118,7 +121,6 @@ namespace BTCPayServer.Controllers.GreenField
"Invalid Derivation Scheme"); "Invalid Derivation Scheme");
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
} }
} }
@ -132,11 +134,13 @@ namespace BTCPayServer.Controllers.GreenField
{ {
return NotFound(); return NotFound();
} }
if (string.IsNullOrEmpty(paymentMethodData?.DerivationScheme)) if (string.IsNullOrEmpty(paymentMethodData?.DerivationScheme))
{ {
ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme), ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme),
"Missing derivationScheme"); "Missing derivationScheme");
} }
if (!ModelState.IsValid) if (!ModelState.IsValid)
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
DerivationSchemeSettings strategy; DerivationSchemeSettings strategy;
@ -207,6 +211,7 @@ namespace BTCPayServer.Controllers.GreenField
ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme), ModelState.AddModelError(nameof(OnChainPaymentMethodData.DerivationScheme),
"Missing derivationScheme"); "Missing derivationScheme");
} }
if (!ModelState.IsValid) if (!ModelState.IsValid)
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
@ -229,6 +234,7 @@ namespace BTCPayServer.Controllers.GreenField
signing.AccountKeyPath = null; signing.AccountKeyPath = null;
signing.RootFingerprint = null; signing.RootFingerprint = null;
} }
store.SetSupportedPaymentMethod(id, strategy); store.SetSupportedPaymentMethod(id, strategy);
storeBlob.SetExcluded(id, !paymentMethodData.Enabled); storeBlob.SetExcluded(id, !paymentMethodData.Enabled);
store.SetStoreBlob(storeBlob); store.SetStoreBlob(storeBlob);

View file

@ -26,18 +26,18 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-methods")] [HttpGet("~/api/v1/stores/{storeId}/payment-methods")]
public ActionResult<Dictionary<string, GenericPaymentMethodData>> GetStorePaymentMethods( public ActionResult<Dictionary<string, GenericPaymentMethodData>> GetStorePaymentMethods(
[FromQuery] bool enabledOnly = false [FromQuery] bool? enabled)
)
{ {
var storeBlob = Store.GetStoreBlob(); var storeBlob = Store.GetStoreBlob();
var excludedPaymentMethods = storeBlob.GetExcludedPaymentMethods(); var excludedPaymentMethods = storeBlob.GetExcludedPaymentMethods();
return Ok(Store.GetSupportedPaymentMethods(_btcPayNetworkProvider) return Ok(Store.GetSupportedPaymentMethods(_btcPayNetworkProvider)
.Where(method => !enabledOnly || !excludedPaymentMethods.Match(method.PaymentId)) .Where(method =>
enabled is null || (enabled is false && excludedPaymentMethods.Match(method.PaymentId)))
.ToDictionary( .ToDictionary(
method => method.PaymentId.ToStringNormalized(), method => method.PaymentId.ToStringNormalized(),
method => new GenericPaymentMethodData() method => new GenericPaymentMethodData()
{ {
Enabled = enabledOnly || !excludedPaymentMethods.Match(method.PaymentId), Enabled = enabled.GetValueOrDefault(!excludedPaymentMethods.Match(method.PaymentId)),
Data = method.PaymentId.PaymentType.GetGreenfieldData(method) Data = method.PaymentId.PaymentType.GetGreenfieldData(method)
})); }));
} }

View file

@ -19,10 +19,10 @@
} }
}, },
{ {
"name": "enabledOnly", "name": "enabled",
"in": "query", "in": "query",
"required": false, "required": false,
"description": "Fetch payment methods that are enable only", "description": "Fetch payment methods that are enabled/disabled only",
"schema": { "schema": {
"type": "boolean" "type": "boolean"
} }

View file

@ -15,6 +15,15 @@
"schema": { "schema": {
"type": "string" "type": "string"
} }
},
{
"name": "enabled",
"in": "query",
"required": false,
"description": "Fetch payment methods that are enabled/disabled only",
"schema": {
"type": "boolean"
}
} }
], ],
"description": "View information about the stores' configured Lightning Network payment methods", "description": "View information about the stores' configured Lightning Network payment methods",

View file

@ -17,6 +17,15 @@
"schema": { "schema": {
"type": "string" "type": "string"
} }
},
{
"name": "enabled",
"in": "query",
"required": false,
"description": "Fetch payment methods that are enabled/disabled only",
"schema": {
"type": "boolean"
}
} }
], ],
"responses": { "responses": {