Allowing for displaying of app directly on website root

This commit is contained in:
rockstardev
2019-04-11 16:30:23 -05:00
parent 4067d4b00f
commit 827b133534
8 changed files with 89 additions and 45 deletions

View File

@@ -32,7 +32,7 @@ namespace BTCPayServer.Controllers
{ {
public class AppsPublicController : Controller public class AppsPublicController : Controller
{ {
public AppsPublicController(AppService AppService, public AppsPublicController(AppService AppService,
InvoiceController invoiceController, InvoiceController invoiceController,
UserManager<ApplicationUser> userManager) UserManager<ApplicationUser> userManager)
{ {
@@ -86,34 +86,34 @@ namespace BTCPayServer.Controllers
AppId = appId AppId = appId
}); });
} }
[HttpGet] [HttpGet]
[Route("/apps/{appId}/crowdfund")] [Route("/apps/{appId}/crowdfund")]
[XFrameOptionsAttribute(XFrameOptionsAttribute.XFrameOptions.AllowAll)] [XFrameOptionsAttribute(XFrameOptionsAttribute.XFrameOptions.AllowAll)]
public async Task<IActionResult> ViewCrowdfund(string appId, string statusMessage) public async Task<IActionResult> ViewCrowdfund(string appId, string statusMessage)
{ {
var app = await _AppService.GetApp(appId, AppType.Crowdfund, true); var app = await _AppService.GetApp(appId, AppType.Crowdfund, true);
if (app == null) if (app == null)
return NotFound(); return NotFound();
var settings = app.GetSettings<CrowdfundSettings>(); var settings = app.GetSettings<CrowdfundSettings>();
var isAdmin = await _AppService.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null; 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 (!hasEnoughSettingsToLoad)
{ {
if(!isAdmin) if (!isAdmin)
return NotFound(); return NotFound();
return NotFound("A Target Currency must be set for this app in order to be loadable."); return NotFound("A Target Currency must be set for this app in order to be loadable.");
} }
var appInfo = (ViewCrowdfundViewModel)(await _AppService.GetAppInfo(appId)); var appInfo = (ViewCrowdfundViewModel)(await _AppService.GetAppInfo(appId));
appInfo.HubPath = AppHub.GetHubPath(this.Request); appInfo.HubPath = AppHub.GetHubPath(this.Request);
if (settings.Enabled) return View(appInfo); if (settings.Enabled)
if(!isAdmin) return View(appInfo);
if (!isAdmin)
return NotFound(); return NotFound();
return View(appInfo); return View(appInfo);
@@ -135,7 +135,8 @@ namespace BTCPayServer.Controllers
var isAdmin = await _AppService.GetAppDataIfOwner(GetUserId(), appId, AppType.Crowdfund) != null; 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"); return NotFound("Crowdfund is not currently active");
} }
@@ -193,7 +194,7 @@ namespace BTCPayServer.Controllers
if (request.RedirectToCheckout) if (request.RedirectToCheckout)
{ {
return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice",
new {invoiceId = invoice.Data.Id}); new { invoiceId = invoice.Data.Id });
} }
else else
{ {
@@ -204,7 +205,7 @@ namespace BTCPayServer.Controllers
{ {
return BadRequest(e.Message); return BadRequest(e.Message);
} }
} }
[HttpPost] [HttpPost]
@@ -257,28 +258,28 @@ namespace BTCPayServer.Controllers
var store = await _AppService.GetStore(app); var store = await _AppService.GetStore(app);
store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id)); store.AdditionalClaims.Add(new Claim(Policies.CanCreateInvoice.Key, store.Id));
var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest() var invoice = await _InvoiceController.CreateInvoiceCore(new CreateInvoiceRequest()
{ {
ItemCode = choice?.Id, ItemCode = choice?.Id,
ItemDesc = title, ItemDesc = title,
Currency = settings.Currency, Currency = settings.Currency,
Price = price, Price = price,
BuyerEmail = email, BuyerEmail = email,
OrderId = orderId, OrderId = orderId,
NotificationURL = NotificationURL =
string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl, string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl,
NotificationEmail = settings.NotificationEmail, NotificationEmail = settings.NotificationEmail,
RedirectURL = redirectUrl ?? Request.GetDisplayUrl(), RedirectURL = redirectUrl ?? Request.GetDisplayUrl(),
FullNotifications = true, FullNotifications = true,
ExtendedNotifications = true, ExtendedNotifications = true,
PosData = string.IsNullOrEmpty(posData) ? null : posData, PosData = string.IsNullOrEmpty(posData) ? null : posData,
RedirectAutomatically = settings.RedirectAutomatically, RedirectAutomatically = settings.RedirectAutomatically,
}, store, HttpContext.Request.GetAbsoluteRoot(), }, store, HttpContext.Request.GetAbsoluteRoot(),
new List<string>() {AppService.GetAppInternalTag(appId)}, new List<string>() { AppService.GetAppInternalTag(appId) },
cancellationToken); cancellationToken);
return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", new { invoiceId = invoice.Data.Id }); return RedirectToAction(nameof(InvoiceController.Checkout), "Invoice", new { invoiceId = invoice.Data.Id });
} }
private string GetUserId() private string GetUserId()
{ {
return _UserManager.GetUserId(User); return _UserManager.GetUserId(User);

View File

@@ -8,19 +8,39 @@ using System.Net.Http;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NBitcoin; using NBitcoin;
using Newtonsoft.Json; using Newtonsoft.Json;
using BTCPayServer.Services;
using BTCPayServer.HostedServices;
namespace BTCPayServer.Controllers namespace BTCPayServer.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
private readonly CssThemeManager _cachedServerSettings;
public IHttpClientFactory HttpClientFactory { get; } public IHttpClientFactory HttpClientFactory { get; }
public HomeController(IHttpClientFactory httpClientFactory) public HomeController(IHttpClientFactory httpClientFactory, CssThemeManager cachedServerSettings)
{ {
HttpClientFactory = httpClientFactory; HttpClientFactory = httpClientFactory;
_cachedServerSettings = cachedServerSettings;
} }
public IActionResult Index()
public async Task<IActionResult> 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"); return View("Home");
} }

View File

@@ -13,6 +13,7 @@ using BTCPayServer.Events;
using BTCPayServer.Services; using BTCPayServer.Services;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using BTCPayServer.Security; using BTCPayServer.Security;
using BTCPayServer.Services.Apps;
namespace BTCPayServer.HostedServices namespace BTCPayServer.HostedServices
{ {
@@ -44,13 +45,30 @@ namespace BTCPayServer.HostedServices
get { return _creativeStartUri; } get { return _creativeStartUri; }
} }
public bool ShowRegister { get; set; } public bool ShowRegister { get; set; }
public bool DiscourageSearchEngines { get; set; } public bool DiscourageSearchEngines { get; set; }
public AppType? RootAppType { get; set; }
public string RootAppId { get; set; }
internal void Update(PoliciesSettings data) internal void Update(PoliciesSettings data)
{ {
ShowRegister = !data.LockSubscription; ShowRegister = !data.LockSubscription;
DiscourageSearchEngines = data.DiscourageSearchEngines; 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
}
} }
} }

View File

@@ -78,7 +78,7 @@ namespace BTCPayServer.Hosting
// StyleSrc = "'self' 'unsafe-inline'", // StyleSrc = "'self' 'unsafe-inline'",
// ScriptSrc = "'self' 'unsafe-inline'" // ScriptSrc = "'self' 'unsafe-inline'"
//}); //});
}); }).AddControllersAsServices();
services.TryAddScoped<ContentSecurityPolicies>(); services.TryAddScoped<ContentSecurityPolicies>();
services.Configure<IdentityOptions>(options => services.Configure<IdentityOptions>(options =>
{ {

View File

@@ -10,17 +10,17 @@ namespace BTCPayServer.Services
public class PoliciesSettings public class PoliciesSettings
{ {
[Display(Name = "Requires a confirmation mail for registering")] [Display(Name = "Requires a confirmation mail for registering")]
public bool RequiresConfirmedEmail public bool RequiresConfirmedEmail { get; set; }
{
get; set;
}
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[Display(Name = "Disable registration")] [Display(Name = "Disable registration")]
public bool LockSubscription { get; set; } public bool LockSubscription { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)] [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
[Display(Name = "Discourage search engines from indexing this site")] [Display(Name = "Discourage search engines from indexing this site")]
public bool DiscourageSearchEngines { get; set; } public bool DiscourageSearchEngines { get; set; }
[Display(Name = "Display app on website root")]
public string DisplayAppOnRoot { get; set; }
} }
} }

View File

@@ -135,7 +135,7 @@
</div> </div>
<div class="col-md-4 col-sm-12"> <div class="col-md-4 col-sm-12">
<partial <partial
name="Crowdfund/ContributeForm" name="/Views/AppsPublic/Crowdfund/ContributeForm.cshtml"
model="@(new ContributeToCrowdfund() model="@(new ContributeToCrowdfund()
{ {
ViewCrowdfundViewModel = Model, ViewCrowdfundViewModel = Model,

View File

@@ -41,19 +41,19 @@
<body> <body>
@if (Context.Request.Query.ContainsKey("simple")) @if (Context.Request.Query.ContainsKey("simple"))
{ {
@await Html.PartialAsync("Crowdfund/MinimalCrowdfund", Model) @await Html.PartialAsync("/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml", Model)
} }
else else
{ {
<noscript> <noscript>
@await Html.PartialAsync("Crowdfund/MinimalCrowdfund", Model) @await Html.PartialAsync("/Views/AppsPublic/Crowdfund/MinimalCrowdfund.cshtml", Model)
</noscript> </noscript>
if (Model.AnimationsEnabled) if (Model.AnimationsEnabled)
{ {
<canvas id="fireworks"></canvas> <canvas id="fireworks"></canvas>
} }
@await Html.PartialAsync("Crowdfund/VueCrowdfund", Model) @await Html.PartialAsync("/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml", Model)
} }
</body> </body>

View File

@@ -21,11 +21,16 @@
<div class="form-group"> <div class="form-group">
<label asp-for="LockSubscription"></label> <label asp-for="LockSubscription"></label>
<input asp-for="LockSubscription" type="checkbox" class="form-check-inline" /> <input asp-for="LockSubscription" type="checkbox" class="form-check-inline" />
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="DiscourageSearchEngines"></label> <label asp-for="DiscourageSearchEngines"></label>
<input asp-for="DiscourageSearchEngines" type="checkbox" class="form-check-inline" /> <input asp-for="DiscourageSearchEngines" type="checkbox" class="form-check-inline" />
</div> </div>
<div class="form-group">
<label asp-for="DisplayAppOnRoot"></label>
<input asp-for="DisplayAppOnRoot" class="form-control" />
<span asp-validation-for="DisplayAppOnRoot" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary" name="command" value="Save">Save</button> <button type="submit" class="btn btn-primary" name="command" value="Save">Save</button>
</form> </form>
</div> </div>