From 827b133534a7bf9cbedb84b895a6fa7df2ecbc80 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Thu, 11 Apr 2019 16:30:23 -0500 Subject: [PATCH 1/2] 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 @@
-
+
+
+ + + +
From 13e330fa657730c714a3c786172b607e1021a3dc Mon Sep 17 00:00:00 2001 From: rockstardev Date: Fri, 12 Apr 2019 00:13:14 -0500 Subject: [PATCH 2/2] Better UI for selection of app to be displayed on root --- BTCPayServer/Controllers/ServerController.cs | 43 ++++++++++++++++--- .../HostedServices/CssThemeManager.cs | 14 +----- BTCPayServer/Services/PoliciesSettings.cs | 5 ++- BTCPayServer/Views/Server/Policies.cshtml | 5 +-- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/BTCPayServer/Controllers/ServerController.cs b/BTCPayServer/Controllers/ServerController.cs index e0a8c8cc1..f4ab44835 100644 --- a/BTCPayServer/Controllers/ServerController.cs +++ b/BTCPayServer/Controllers/ServerController.cs @@ -27,6 +27,9 @@ using Renci.SshNet; using BTCPayServer.Logging; using BTCPayServer.Lightning; using System.Runtime.CompilerServices; +using BTCPayServer.Services.Apps; +using Microsoft.AspNetCore.Mvc.Rendering; +using BTCPayServer.Data; namespace BTCPayServer.Controllers { @@ -41,16 +44,18 @@ namespace BTCPayServer.Controllers LightningConfigurationProvider _LnConfigProvider; private readonly TorServices _torServices; BTCPayServerOptions _Options; + ApplicationDbContextFactory _ContextFactory; public ServerController(UserManager userManager, - Configuration.BTCPayServerOptions options, + BTCPayServerOptions options, RateFetcher rateProviderFactory, SettingsRepository settingsRepository, NBXplorerDashboard dashBoard, IHttpClientFactory httpClientFactory, LightningConfigurationProvider lnConfigProvider, TorServices torServices, - Services.Stores.StoreRepository storeRepository) + StoreRepository storeRepository, + ApplicationDbContextFactory contextFactory) { _Options = options; _UserManager = userManager; @@ -61,6 +66,7 @@ namespace BTCPayServer.Controllers _StoreRepository = storeRepository; _LnConfigProvider = lnConfigProvider; _torServices = torServices; + _ContextFactory = contextFactory; } [Route("server/rates")] @@ -441,15 +447,42 @@ namespace BTCPayServer.Controllers public async Task Policies() { var data = (await _SettingsRepository.GetSettingAsync()) ?? new PoliciesSettings(); + + // load display app dropdown + using (var ctx = _ContextFactory.CreateContext()) + { + var selectList = ctx.Apps.Select(a => + new SelectListItem($"{a.AppType} - {a.Name}", a.Id) + ).ToList(); + selectList.Insert(0, new SelectListItem("(None)", null)); + + ViewBag.AppsList = new SelectList(selectList, "Value", "Text", data.RootAppId); + } + return View(data); } [Route("server/policies")] [HttpPost] public async Task Policies(PoliciesSettings settings) { + if (!String.IsNullOrEmpty(settings.RootAppId)) + { + using (var ctx = _ContextFactory.CreateContext()) + { + var app = ctx.Apps.SingleOrDefault(a => a.Id == settings.RootAppId); + if (app != null) + settings.RootAppType = Enum.Parse(app.AppType); + } + } + else + { + // not preserved on client side, but clearing it just in case + settings.RootAppType = null; + } + await _SettingsRepository.UpdateSetting(settings); TempData["StatusMessage"] = "Policies updated successfully"; - return View(settings); + return RedirectToAction(nameof(Policies)); } [Route("server/services")] @@ -473,7 +506,7 @@ namespace BTCPayServer.Controllers Link = this.Url.Action(nameof(SSHService)) }); } - foreach(var torService in _torServices.Services) + foreach (var torService in _torServices.Services) { if (torService.VirtualPort == 80) { @@ -680,7 +713,7 @@ namespace BTCPayServer.Controllers return File(System.IO.File.ReadAllBytes(settings.KeyFile), "application/octet-stream", "id_rsa"); } - var server = Extensions.IsLocalNetwork(settings.Server) ? this.Request.Host.Host: settings.Server; + var server = Extensions.IsLocalNetwork(settings.Server) ? this.Request.Host.Host : settings.Server; SSHServiceViewModel vm = new SSHServiceViewModel(); string port = settings.Port == 22 ? "" : $" -p {settings.Port}"; vm.CommandLine = $"ssh {settings.Username}@{server}{port}"; diff --git a/BTCPayServer/HostedServices/CssThemeManager.cs b/BTCPayServer/HostedServices/CssThemeManager.cs index 69dc56048..2bd75af50 100644 --- a/BTCPayServer/HostedServices/CssThemeManager.cs +++ b/BTCPayServer/HostedServices/CssThemeManager.cs @@ -57,18 +57,8 @@ namespace BTCPayServer.HostedServices 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 - } + RootAppType = data.RootAppType; + RootAppId = data.RootAppId; } } diff --git a/BTCPayServer/Services/PoliciesSettings.cs b/BTCPayServer/Services/PoliciesSettings.cs index c523bb55e..7cb1737f7 100644 --- a/BTCPayServer/Services/PoliciesSettings.cs +++ b/BTCPayServer/Services/PoliciesSettings.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; +using BTCPayServer.Services.Apps; using Newtonsoft.Json; namespace BTCPayServer.Services @@ -21,6 +22,8 @@ namespace BTCPayServer.Services public bool DiscourageSearchEngines { get; set; } [Display(Name = "Display app on website root")] - public string DisplayAppOnRoot { get; set; } + public string RootAppId { get; set; } + + public AppType? RootAppType { get; set; } } } diff --git a/BTCPayServer/Views/Server/Policies.cshtml b/BTCPayServer/Views/Server/Policies.cshtml index 971957db1..f5422ba4b 100644 --- a/BTCPayServer/Views/Server/Policies.cshtml +++ b/BTCPayServer/Views/Server/Policies.cshtml @@ -27,9 +27,8 @@
- - - + +