From 5c6db35c9b930aa8dbf76c3ce3f0ef095f80b624 Mon Sep 17 00:00:00 2001 From: d11n Date: Sat, 26 Nov 2022 05:01:00 +0100 Subject: [PATCH] Cleanups (#4351) --- .../Controllers/UIInvoiceController.UI.cs | 1 - .../Controllers/UIPaymentRequestController.cs | 21 ++++++------ BTCPayServer/Forms/UIFormsController.cs | 32 +++++++------------ .../Views/Shared/Forms/FieldSetElement.cshtml | 2 +- BTCPayServer/Views/Shared/_Form.cshtml | 8 ++--- BTCPayServer/Views/UIInvoice/Invoice.cshtml | 10 +++--- 6 files changed, 30 insertions(+), 44 deletions(-) diff --git a/BTCPayServer/Controllers/UIInvoiceController.UI.cs b/BTCPayServer/Controllers/UIInvoiceController.UI.cs index ff1b26a15..5cc9ccf23 100644 --- a/BTCPayServer/Controllers/UIInvoiceController.UI.cs +++ b/BTCPayServer/Controllers/UIInvoiceController.UI.cs @@ -24,7 +24,6 @@ using BTCPayServer.Services.Apps; using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Invoices.Export; using BTCPayServer.Services.Rates; -using BTCPayServer.Services.Stores; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; diff --git a/BTCPayServer/Controllers/UIPaymentRequestController.cs b/BTCPayServer/Controllers/UIPaymentRequestController.cs index dd44887a2..5336df842 100644 --- a/BTCPayServer/Controllers/UIPaymentRequestController.cs +++ b/BTCPayServer/Controllers/UIPaymentRequestController.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -19,10 +18,8 @@ using BTCPayServer.Services.PaymentRequests; using BTCPayServer.Services.Rates; using BTCPayServer.Services.Stores; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Routing; using Newtonsoft.Json.Linq; using PaymentRequestData = BTCPayServer.Data.PaymentRequestData; using StoreData = BTCPayServer.Data.StoreData; @@ -42,7 +39,7 @@ namespace BTCPayServer.Controllers private readonly InvoiceRepository _InvoiceRepository; private readonly StoreRepository _storeRepository; - public FormComponentProviders FormProviders { get; } + private FormComponentProviders FormProviders { get; } public UIPaymentRequestController( UIInvoiceController invoiceController, @@ -207,8 +204,8 @@ namespace BTCPayServer.Controllers break; default: // POST case: Handle form submit - var formData = Form.Parse(Forms.UIFormsController.GetFormData(prFormId).Config); - formData.ApplyValuesFromForm(this.Request.Form); + var formData = Form.Parse(UIFormsController.GetFormData(prFormId).Config); + formData.ApplyValuesFromForm(Request.Form); if (FormProviders.Validate(formData, ModelState)) { prBlob.FormResponse = JObject.FromObject(formData.GetValues()); @@ -224,13 +221,13 @@ namespace BTCPayServer.Controllers AspController = "UIForms", AspAction = "ViewPublicForm", RouteParameters = - { - { "formId", prFormId } - }, + { + { "formId", prFormId } + }, FormParameters = - { - { "redirectUrl", Request.GetCurrentUrl() } - } + { + { "redirectUrl", Request.GetCurrentUrl() } + } }); } diff --git a/BTCPayServer/Forms/UIFormsController.cs b/BTCPayServer/Forms/UIFormsController.cs index e2e087212..17a8f4c7e 100644 --- a/BTCPayServer/Forms/UIFormsController.cs +++ b/BTCPayServer/Forms/UIFormsController.cs @@ -1,40 +1,27 @@ #nullable enable using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Threading.Tasks; using BTCPayServer.Abstractions.Constants; -using BTCPayServer.Abstractions.Extensions; using BTCPayServer.Abstractions.Form; -using BTCPayServer.Abstractions.Models; using BTCPayServer.Client; -using BTCPayServer.Client.Models; using BTCPayServer.Controllers; -using BTCPayServer.Data; using BTCPayServer.Data.Data; using BTCPayServer.Forms.Models; using BTCPayServer.Models; using BTCPayServer.Services.Stores; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace BTCPayServer.Forms; [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] public class UIFormsController : Controller { - public FormComponentProviders FormProviders { get; } + private FormComponentProviders FormProviders { get; } public UIFormsController(FormComponentProviders formProviders) { FormProviders = formProviders; } - private bool IsValidRedirectUri(string? redirectUrl) => - !string.IsNullOrEmpty(redirectUrl) && Uri.TryCreate(redirectUrl, UriKind.RelativeOrAbsolute, out var uri) && - (Url.IsLocalUrl(redirectUrl) || uri.Host.Equals(Request.Host.Host.ToString())); [AllowAnonymous] [HttpGet("~/forms/{formId}")] @@ -43,6 +30,7 @@ public class UIFormsController : Controller { if (!IsValidRedirectUri(redirectUrl)) return BadRequest(); + FormData? formData = string.IsNullOrEmpty(formId) ? null : GetFormData(formId); if (formData == null) { @@ -56,22 +44,19 @@ public class UIFormsController : Controller ViewResult GetFormView(FormData formData, string? redirectUrl) { - return View("View", new FormViewModel() { FormData = formData, RedirectUrl = redirectUrl }); + return View("View", new FormViewModel { FormData = formData, RedirectUrl = redirectUrl }); } [AllowAnonymous] [HttpPost("~/forms/{formId}")] - public IActionResult SubmitForm( - string formId, - string? redirectUrl, - string? command, - [FromServices] StoreRepository storeRepository, - [FromServices] UIInvoiceController invoiceController) + public IActionResult SubmitForm(string formId, string? redirectUrl, string? command) { if (!IsValidRedirectUri(redirectUrl)) return BadRequest(); + var formData = GetFormData(formId); if (formData?.Config is null) return NotFound(); + if (command is not "Submit") return GetFormView(formData, redirectUrl); @@ -83,6 +68,7 @@ public class UIFormsController : Controller var form = new MultiValueDictionary(); foreach (var kv in Request.Form) form.Add(kv.Key, kv.Value); + // With redirect, the form comes from another entity that we need to send the data back to if (!string.IsNullOrEmpty(redirectUrl)) { @@ -116,4 +102,8 @@ public class UIFormsController : Controller }; return form; } + + private bool IsValidRedirectUri(string? redirectUrl) => + !string.IsNullOrEmpty(redirectUrl) && Uri.TryCreate(redirectUrl, UriKind.RelativeOrAbsolute, out var uri) && + (Url.IsLocalUrl(redirectUrl) || uri.Host.Equals(Request.Host.Host)); } diff --git a/BTCPayServer/Views/Shared/Forms/FieldSetElement.cshtml b/BTCPayServer/Views/Shared/Forms/FieldSetElement.cshtml index 27db6ce70..bc9ae5f7a 100644 --- a/BTCPayServer/Views/Shared/Forms/FieldSetElement.cshtml +++ b/BTCPayServer/Views/Shared/Forms/FieldSetElement.cshtml @@ -12,7 +12,7 @@ { if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial)) { - + } } diff --git a/BTCPayServer/Views/Shared/_Form.cshtml b/BTCPayServer/Views/Shared/_Form.cshtml index ae72c1372..9ca4ff6af 100644 --- a/BTCPayServer/Views/Shared/_Form.cshtml +++ b/BTCPayServer/Views/Shared/_Form.cshtml @@ -5,8 +5,8 @@ @foreach (var field in Model.Fields) { - if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial)) - { - - } + if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial)) + { + + } } diff --git a/BTCPayServer/Views/UIInvoice/Invoice.cshtml b/BTCPayServer/Views/UIInvoice/Invoice.cshtml index 2528bd331..8ea8bb937 100644 --- a/BTCPayServer/Views/UIInvoice/Invoice.cshtml +++ b/BTCPayServer/Views/UIInvoice/Invoice.cshtml @@ -416,7 +416,7 @@

Webhooks

- + @@ -491,7 +491,7 @@

Refunds

Status ID
- + @@ -526,9 +526,9 @@
Pull Payment Amount
} -

Events

- - +

Events

+
+
Date Message