From 827b133534a7bf9cbedb84b895a6fa7df2ecbc80 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Thu, 11 Apr 2019 16:30:23 -0500 Subject: [PATCH] Allowing for displaying of app directly on website root --- .../Controllers/AppsPublicController.cs | 65 ++++++++++--------- BTCPayServer/Controllers/HomeController.cs | 24 ++++++- .../HostedServices/CssThemeManager.cs | 18 +++++ BTCPayServer/Hosting/Startup.cs | 2 +- BTCPayServer/Services/PoliciesSettings.cs | 10 +-- .../Crowdfund/MinimalCrowdfund.cshtml | 2 +- .../Views/AppsPublic/ViewCrowdfund.cshtml | 6 +- BTCPayServer/Views/Server/Policies.cshtml | 7 +- 8 files changed, 89 insertions(+), 45 deletions(-) diff --git a/BTCPayServer/Controllers/AppsPublicController.cs b/BTCPayServer/Controllers/AppsPublicController.cs index eee644826..037cf8593 100644 --- a/BTCPayServer/Controllers/AppsPublicController.cs +++ b/BTCPayServer/Controllers/AppsPublicController.cs @@ -32,7 +32,7 @@ namespace BTCPayServer.Controllers { public class AppsPublicController : Controller { - public AppsPublicController(AppService AppService, + public AppsPublicController(AppService AppService, InvoiceController invoiceController, UserManager userManager) { @@ -86,34 +86,34 @@ namespace BTCPayServer.Controllers AppId = appId }); } - - + + [HttpGet] [Route("/apps/{appId}/crowdfund")] [XFrameOptionsAttribute(XFrameOptionsAttribute.XFrameOptions.AllowAll)] public async Task ViewCrowdfund(string appId, string statusMessage) - { var app = await _AppService.GetApp(appId, AppType.Crowdfund, true); - + if (app == null) return NotFound(); var settings = app.GetSettings(); - + var isAdmin = await _AppService.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null; - - var hasEnoughSettingsToLoad = !string.IsNullOrEmpty(settings.TargetCurrency ); + + var hasEnoughSettingsToLoad = !string.IsNullOrEmpty(settings.TargetCurrency); if (!hasEnoughSettingsToLoad) { - if(!isAdmin) + if (!isAdmin) return NotFound(); return NotFound("A Target Currency must be set for this app in order to be loadable."); } var appInfo = (ViewCrowdfundViewModel)(await _AppService.GetAppInfo(appId)); appInfo.HubPath = AppHub.GetHubPath(this.Request); - if (settings.Enabled) return View(appInfo); - if(!isAdmin) + if (settings.Enabled) + return View(appInfo); + if (!isAdmin) return NotFound(); return View(appInfo); @@ -135,7 +135,8 @@ namespace BTCPayServer.Controllers var isAdmin = await _AppService.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null; - if (!settings.Enabled && !isAdmin) { + if (!settings.Enabled && !isAdmin) + { return NotFound("Crowdfund is not currently active"); } @@ -193,7 +194,7 @@ namespace BTCPayServer.Controllers if (request.RedirectToCheckout) { return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", - new {invoiceId = invoice.Data.Id}); + new { invoiceId = invoice.Data.Id }); } else { @@ -204,7 +205,7 @@ namespace BTCPayServer.Controllers { return BadRequest(e.Message); } - + } [HttpPost] @@ -257,28 +258,28 @@ namespace BTCPayServer.Controllers var store = await _AppService.GetStore(app); store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id)); var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest() - { - ItemCode = choice?.Id, - ItemDesc = title, - Currency = settings.Currency, - Price = price, - BuyerEmail = email, - OrderId = orderId, - NotificationURL = + { + ItemCode = choice?.Id, + ItemDesc = title, + Currency = settings.Currency, + Price = price, + BuyerEmail = email, + OrderId = orderId, + NotificationURL = string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl, - NotificationEmail = settings.NotificationEmail, - RedirectURL = redirectUrl ?? Request.GetDisplayUrl(), - FullNotifications = true, - ExtendedNotifications = true, - PosData = string.IsNullOrEmpty(posData) ? null : posData, - RedirectAutomatically = settings.RedirectAutomatically, - }, store, HttpContext.Request.GetAbsoluteRoot(), - new List() {AppService.GetAppInternalTag(appId)}, + NotificationEmail = settings.NotificationEmail, + RedirectURL = redirectUrl ?? Request.GetDisplayUrl(), + FullNotifications = true, + ExtendedNotifications = true, + PosData = string.IsNullOrEmpty(posData) ? null : posData, + RedirectAutomatically = settings.RedirectAutomatically, + }, store, HttpContext.Request.GetAbsoluteRoot(), + new List() { AppService.GetAppInternalTag(appId) }, cancellationToken); return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", new { invoiceId = invoice.Data.Id }); } - - + + private string GetUserId() { return _UserManager.GetUserId(User); diff --git a/BTCPayServer/Controllers/HomeController.cs b/BTCPayServer/Controllers/HomeController.cs index 01dcccbee..c255199b4 100644 --- a/BTCPayServer/Controllers/HomeController.cs +++ b/BTCPayServer/Controllers/HomeController.cs @@ -8,19 +8,39 @@ using System.Net.Http; using Newtonsoft.Json.Linq; using NBitcoin; using Newtonsoft.Json; +using BTCPayServer.Services; +using BTCPayServer.HostedServices; namespace BTCPayServer.Controllers { public class HomeController : Controller { + private readonly CssThemeManager _cachedServerSettings; + public IHttpClientFactory HttpClientFactory { get; } - public HomeController(IHttpClientFactory httpClientFactory) + public HomeController(IHttpClientFactory httpClientFactory, CssThemeManager cachedServerSettings) { HttpClientFactory = httpClientFactory; + _cachedServerSettings = cachedServerSettings; } - public IActionResult Index() + + public async Task Index() { + if (_cachedServerSettings.RootAppType == Services.Apps.AppType.Crowdfund) + { + var serviceProvider = HttpContext.RequestServices; + var controller = (AppsPublicController)serviceProvider.GetService(typeof(AppsPublicController)); + controller.Url = Url; + controller.ControllerContext = ControllerContext; + var res = await controller.ViewCrowdfund(_cachedServerSettings.RootAppId, null) as ViewResult; + if (res != null) + { + res.ViewName = "/Views/AppsPublic/ViewCrowdfund.cshtml"; + return res; // return + } + } + return View("Home"); } diff --git a/BTCPayServer/HostedServices/CssThemeManager.cs b/BTCPayServer/HostedServices/CssThemeManager.cs index 6b2c6b139..69dc56048 100644 --- a/BTCPayServer/HostedServices/CssThemeManager.cs +++ b/BTCPayServer/HostedServices/CssThemeManager.cs @@ -13,6 +13,7 @@ using BTCPayServer.Events; using BTCPayServer.Services; using Microsoft.AspNetCore.Mvc.Filters; using BTCPayServer.Security; +using BTCPayServer.Services.Apps; namespace BTCPayServer.HostedServices { @@ -44,13 +45,30 @@ namespace BTCPayServer.HostedServices get { return _creativeStartUri; } } + public bool ShowRegister { get; set; } public bool DiscourageSearchEngines { get; set; } + public AppType? RootAppType { get; set; } + public string RootAppId { get; set; } + internal void Update(PoliciesSettings data) { ShowRegister = !data.LockSubscription; DiscourageSearchEngines = data.DiscourageSearchEngines; + + RootAppType = null; + RootAppId = null; + try + { + var arr = data.DisplayAppOnRoot.Split('/'); + RootAppType = Enum.Parse(typeof(AppType), arr[0], true) as AppType?; + RootAppId = arr[1]; + } + catch + { + // ignore parsing errors + } } } diff --git a/BTCPayServer/Hosting/Startup.cs b/BTCPayServer/Hosting/Startup.cs index 70f23f147..03e44478f 100644 --- a/BTCPayServer/Hosting/Startup.cs +++ b/BTCPayServer/Hosting/Startup.cs @@ -78,7 +78,7 @@ namespace BTCPayServer.Hosting // StyleSrc = "'self' 'unsafe-inline'", // ScriptSrc = "'self' 'unsafe-inline'" //}); - }); + }).AddControllersAsServices(); services.TryAddScoped(); services.Configure(options => { diff --git a/BTCPayServer/Services/PoliciesSettings.cs b/BTCPayServer/Services/PoliciesSettings.cs index cc2766483..c523bb55e 100644 --- a/BTCPayServer/Services/PoliciesSettings.cs +++ b/BTCPayServer/Services/PoliciesSettings.cs @@ -10,17 +10,17 @@ namespace BTCPayServer.Services public class PoliciesSettings { [Display(Name = "Requires a confirmation mail for registering")] - public bool RequiresConfirmedEmail - { - get; set; - } + public bool RequiresConfirmedEmail { get; set; } [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] [Display(Name = "Disable registration")] public bool LockSubscription { get; set; } - + [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] [Display(Name = "Discourage search engines from indexing this site")] public bool DiscourageSearchEngines { get; set; } + + [Display(Name = "Display app on website root")] + public string DisplayAppOnRoot { get; set; } } } diff --git a/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml b/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml index 96620d921..ed9bea259 100644 --- a/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml +++ b/BTCPayServer/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml @@ -135,7 +135,7 @@
- @await Html.PartialAsync("Crowdfund/MinimalCrowdfund", Model) + @await Html.PartialAsync("/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml", Model) if (Model.AnimationsEnabled) { } - @await Html.PartialAsync("Crowdfund/VueCrowdfund", Model) + @await Html.PartialAsync("/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml", Model) } diff --git a/BTCPayServer/Views/Server/Policies.cshtml b/BTCPayServer/Views/Server/Policies.cshtml index 85af6780c..971957db1 100644 --- a/BTCPayServer/Views/Server/Policies.cshtml +++ b/BTCPayServer/Views/Server/Policies.cshtml @@ -21,11 +21,16 @@
-
+
+
+ + + +