This commit is contained in:
d11n 2022-11-26 05:01:00 +01:00 committed by GitHub
parent 887bea4328
commit 5c6db35c9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 44 deletions

View file

@ -24,7 +24,6 @@ using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Invoices.Export; using BTCPayServer.Services.Invoices.Export;
using BTCPayServer.Services.Rates; using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -19,10 +18,8 @@ using BTCPayServer.Services.PaymentRequests;
using BTCPayServer.Services.Rates; using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using PaymentRequestData = BTCPayServer.Data.PaymentRequestData; using PaymentRequestData = BTCPayServer.Data.PaymentRequestData;
using StoreData = BTCPayServer.Data.StoreData; using StoreData = BTCPayServer.Data.StoreData;
@ -42,7 +39,7 @@ namespace BTCPayServer.Controllers
private readonly InvoiceRepository _InvoiceRepository; private readonly InvoiceRepository _InvoiceRepository;
private readonly StoreRepository _storeRepository; private readonly StoreRepository _storeRepository;
public FormComponentProviders FormProviders { get; } private FormComponentProviders FormProviders { get; }
public UIPaymentRequestController( public UIPaymentRequestController(
UIInvoiceController invoiceController, UIInvoiceController invoiceController,
@ -207,8 +204,8 @@ namespace BTCPayServer.Controllers
break; break;
default: default:
// POST case: Handle form submit // POST case: Handle form submit
var formData = Form.Parse(Forms.UIFormsController.GetFormData(prFormId).Config); var formData = Form.Parse(UIFormsController.GetFormData(prFormId).Config);
formData.ApplyValuesFromForm(this.Request.Form); formData.ApplyValuesFromForm(Request.Form);
if (FormProviders.Validate(formData, ModelState)) if (FormProviders.Validate(formData, ModelState))
{ {
prBlob.FormResponse = JObject.FromObject(formData.GetValues()); prBlob.FormResponse = JObject.FromObject(formData.GetValues());
@ -224,13 +221,13 @@ namespace BTCPayServer.Controllers
AspController = "UIForms", AspController = "UIForms",
AspAction = "ViewPublicForm", AspAction = "ViewPublicForm",
RouteParameters = RouteParameters =
{ {
{ "formId", prFormId } { "formId", prFormId }
}, },
FormParameters = FormParameters =
{ {
{ "redirectUrl", Request.GetCurrentUrl() } { "redirectUrl", Request.GetCurrentUrl() }
} }
}); });
} }

View file

@ -1,40 +1,27 @@
#nullable enable #nullable enable
using System; using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Abstractions.Form; using BTCPayServer.Abstractions.Form;
using BTCPayServer.Abstractions.Models;
using BTCPayServer.Client; using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers; using BTCPayServer.Controllers;
using BTCPayServer.Data;
using BTCPayServer.Data.Data; using BTCPayServer.Data.Data;
using BTCPayServer.Forms.Models; using BTCPayServer.Forms.Models;
using BTCPayServer.Models; using BTCPayServer.Models;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Forms; namespace BTCPayServer.Forms;
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)] [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public class UIFormsController : Controller public class UIFormsController : Controller
{ {
public FormComponentProviders FormProviders { get; } private FormComponentProviders FormProviders { get; }
public UIFormsController(FormComponentProviders formProviders) public UIFormsController(FormComponentProviders formProviders)
{ {
FormProviders = 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] [AllowAnonymous]
[HttpGet("~/forms/{formId}")] [HttpGet("~/forms/{formId}")]
@ -43,6 +30,7 @@ public class UIFormsController : Controller
{ {
if (!IsValidRedirectUri(redirectUrl)) if (!IsValidRedirectUri(redirectUrl))
return BadRequest(); return BadRequest();
FormData? formData = string.IsNullOrEmpty(formId) ? null : GetFormData(formId); FormData? formData = string.IsNullOrEmpty(formId) ? null : GetFormData(formId);
if (formData == null) if (formData == null)
{ {
@ -56,22 +44,19 @@ public class UIFormsController : Controller
ViewResult GetFormView(FormData formData, string? redirectUrl) 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] [AllowAnonymous]
[HttpPost("~/forms/{formId}")] [HttpPost("~/forms/{formId}")]
public IActionResult SubmitForm( public IActionResult SubmitForm(string formId, string? redirectUrl, string? command)
string formId,
string? redirectUrl,
string? command,
[FromServices] StoreRepository storeRepository,
[FromServices] UIInvoiceController invoiceController)
{ {
if (!IsValidRedirectUri(redirectUrl)) if (!IsValidRedirectUri(redirectUrl))
return BadRequest(); return BadRequest();
var formData = GetFormData(formId); var formData = GetFormData(formId);
if (formData?.Config is null) if (formData?.Config is null)
return NotFound(); return NotFound();
if (command is not "Submit") if (command is not "Submit")
return GetFormView(formData, redirectUrl); return GetFormView(formData, redirectUrl);
@ -83,6 +68,7 @@ public class UIFormsController : Controller
var form = new MultiValueDictionary<string, string>(); var form = new MultiValueDictionary<string, string>();
foreach (var kv in Request.Form) foreach (var kv in Request.Form)
form.Add(kv.Key, kv.Value); form.Add(kv.Key, kv.Value);
// With redirect, the form comes from another entity that we need to send the data back to // With redirect, the form comes from another entity that we need to send the data back to
if (!string.IsNullOrEmpty(redirectUrl)) if (!string.IsNullOrEmpty(redirectUrl))
{ {
@ -116,4 +102,8 @@ public class UIFormsController : Controller
}; };
return form; 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));
} }

View file

@ -12,7 +12,7 @@
{ {
if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial)) if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial))
{ {
<partial name="@partial.View" for="@field"></partial> <partial name="@partial.View" for="@field"></partial>
} }
} }
</fieldset> </fieldset>

View file

@ -5,8 +5,8 @@
@foreach (var field in Model.Fields) @foreach (var field in Model.Fields)
{ {
if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial)) if (FormComponentProviders.TypeToComponentProvider.TryGetValue(field.Type, out var partial))
{ {
<partial name="@partial.View" for="@field"></partial> <partial name="@partial.View" for="@field"></partial>
} }
} }

View file

@ -416,7 +416,7 @@
<h3 class="mb-3 mt-4">Webhooks</h3> <h3 class="mb-3 mt-4">Webhooks</h3>
<div class="table-responsive-xl"> <div class="table-responsive-xl">
<table class="table table-hover table-responsive-md mb-5"> <table class="table table-hover table-responsive-md mb-5">
<thead class="thead-inverse"> <thead>
<tr> <tr>
<th>Status</th> <th>Status</th>
<th>ID</th> <th>ID</th>
@ -491,7 +491,7 @@
<h3 class="mb-3 mt-4">Refunds</h3> <h3 class="mb-3 mt-4">Refunds</h3>
<div class="table-responsive-xl"> <div class="table-responsive-xl">
<table class="table table-hover table-responsive-md mb-5"> <table class="table table-hover table-responsive-md mb-5">
<thead class="thead-inverse"> <thead>
<tr> <tr>
<th>Pull Payment</th> <th>Pull Payment</th>
<th>Amount</th> <th>Amount</th>
@ -526,9 +526,9 @@
</table> </table>
</div> </div>
} }
<h3 class="mb-0">Events</h3> <h3 class="mb-0 mt-5">Events</h3>
<table class="table table-hover"> <table class="table table-hover mt-3 mb-4">
<thead class="thead-inverse"> <thead>
<tr> <tr>
<th>Date</th> <th>Date</th>
<th>Message</th> <th>Message</th>