2020-09-17 11:37:49 +02:00
|
|
|
using System.Collections.Generic;
|
2017-09-13 08:47:34 +02:00
|
|
|
using System.Diagnostics;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.IO;
|
2019-06-25 20:41:32 +02:00
|
|
|
using System.Linq;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Net.Http;
|
2017-09-13 08:47:34 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2021-07-27 14:08:54 +02:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
2022-03-02 18:28:12 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2021-07-08 07:34:10 +02:00
|
|
|
using BTCPayServer.Client;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Data;
|
2021-03-30 04:41:44 +02:00
|
|
|
using BTCPayServer.Filters;
|
2017-09-13 08:47:34 +02:00
|
|
|
using BTCPayServer.Models;
|
2020-07-15 19:51:01 +02:00
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2020-12-10 15:34:50 +01:00
|
|
|
using BTCPayServer.Services;
|
2021-12-31 08:36:38 +01:00
|
|
|
using BTCPayServer.Services.Stores;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-09-17 11:37:49 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-11-06 06:31:45 +01:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-03-18 12:08:09 +01:00
|
|
|
using Microsoft.Extensions.FileProviders;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
public class UIHomeController : Controller
|
2017-09-13 08:47:34 +02:00
|
|
|
{
|
2022-05-24 06:18:16 +02:00
|
|
|
private readonly ThemeSettings _theme;
|
2021-12-31 08:36:38 +01:00
|
|
|
private readonly StoreRepository _storeRepository;
|
2022-01-13 09:08:15 +01:00
|
|
|
private readonly BTCPayNetworkProvider _networkProvider;
|
2021-12-31 08:36:38 +01:00
|
|
|
private IHttpClientFactory HttpClientFactory { get; }
|
|
|
|
private SignInManager<ApplicationUser> SignInManager { get; }
|
2023-03-01 07:49:21 +01:00
|
|
|
|
|
|
|
private IFileProvider _WebRootFileProvider;
|
|
|
|
|
2020-12-10 15:34:50 +01:00
|
|
|
public LanguageService LanguageService { get; }
|
2018-12-03 15:57:43 +01:00
|
|
|
|
2022-01-07 04:32:00 +01:00
|
|
|
public UIHomeController(IHttpClientFactory httpClientFactory,
|
2022-05-24 06:18:16 +02:00
|
|
|
ThemeSettings theme,
|
2020-12-10 15:34:50 +01:00
|
|
|
LanguageService languageService,
|
2021-12-31 08:36:38 +01:00
|
|
|
StoreRepository storeRepository,
|
2022-01-13 09:08:15 +01:00
|
|
|
BTCPayNetworkProvider networkProvider,
|
2023-03-01 07:49:21 +01:00
|
|
|
IWebHostEnvironment environment,
|
2019-11-06 06:31:45 +01:00
|
|
|
SignInManager<ApplicationUser> signInManager)
|
2018-12-03 15:57:43 +01:00
|
|
|
{
|
2022-05-24 06:18:16 +02:00
|
|
|
_theme = theme;
|
2018-12-03 15:57:43 +01:00
|
|
|
HttpClientFactory = httpClientFactory;
|
2020-12-10 15:34:50 +01:00
|
|
|
LanguageService = languageService;
|
2022-01-13 09:08:15 +01:00
|
|
|
_networkProvider = networkProvider;
|
2021-12-31 08:36:38 +01:00
|
|
|
_storeRepository = storeRepository;
|
2019-11-06 06:31:45 +01:00
|
|
|
SignInManager = signInManager;
|
2023-03-01 07:49:21 +01:00
|
|
|
_WebRootFileProvider = environment.WebRootFileProvider;
|
2018-12-03 15:57:43 +01:00
|
|
|
}
|
2019-05-24 08:07:09 +02:00
|
|
|
|
2022-02-07 13:18:22 +01:00
|
|
|
[HttpGet("home")]
|
|
|
|
public Task<IActionResult> Home()
|
|
|
|
{
|
|
|
|
return Index();
|
|
|
|
}
|
|
|
|
|
2021-03-30 04:41:44 +02:00
|
|
|
[Route("")]
|
2021-12-31 08:36:38 +01:00
|
|
|
[DomainMappingConstraint]
|
2021-07-27 14:08:54 +02:00
|
|
|
public async Task<IActionResult> Index()
|
2019-06-25 20:41:32 +02:00
|
|
|
{
|
2022-05-24 06:18:16 +02:00
|
|
|
if (_theme.FirstRun)
|
2019-11-06 06:31:45 +01:00
|
|
|
{
|
2022-01-07 04:32:00 +01:00
|
|
|
return RedirectToAction(nameof(UIAccountController.Register), "UIAccount");
|
2019-11-06 06:31:45 +01:00
|
|
|
}
|
2021-12-31 08:36:38 +01:00
|
|
|
|
2019-11-06 06:31:45 +01:00
|
|
|
if (SignInManager.IsSignedIn(User))
|
2021-12-31 08:36:38 +01:00
|
|
|
{
|
2022-01-13 09:08:15 +01:00
|
|
|
var userId = SignInManager.UserManager.GetUserId(HttpContext.User);
|
2021-12-31 08:36:38 +01:00
|
|
|
var storeId = HttpContext.GetUserPrefsCookie()?.CurrentStoreId;
|
|
|
|
if (storeId != null)
|
|
|
|
{
|
2022-01-18 02:20:59 +01:00
|
|
|
// verify store exists and redirect to it
|
2021-12-31 08:36:38 +01:00
|
|
|
var store = await _storeRepository.FindStore(storeId, userId);
|
|
|
|
if (store != null)
|
|
|
|
{
|
2023-05-26 16:49:32 +02:00
|
|
|
return RedirectToStore(userId, store);
|
2021-12-31 08:36:38 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 14:18:07 +01:00
|
|
|
|
2022-01-13 09:08:15 +01:00
|
|
|
var stores = await _storeRepository.GetStoresByUserId(userId);
|
2023-05-10 11:18:29 +02:00
|
|
|
return stores.Any()
|
2023-05-26 16:49:32 +02:00
|
|
|
? RedirectToStore(userId, stores.First())
|
2023-05-10 11:18:29 +02:00
|
|
|
: RedirectToAction(nameof(UIUserStoresController.CreateStore), "UIUserStores");
|
2021-12-31 08:36:38 +01:00
|
|
|
}
|
2021-12-31 08:59:02 +01:00
|
|
|
|
2021-12-31 08:36:38 +01:00
|
|
|
return Challenge();
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|
2018-05-21 13:44:03 +02:00
|
|
|
|
2020-12-10 15:34:50 +01:00
|
|
|
[Route("misc/lang")]
|
2021-04-18 04:22:50 +02:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
|
2020-12-10 15:34:50 +01:00
|
|
|
public IActionResult Languages()
|
|
|
|
{
|
2021-12-31 08:36:38 +01:00
|
|
|
return Json(LanguageService.GetLanguages(), new JsonSerializerSettings { Formatting = Formatting.Indented });
|
2020-12-10 15:34:50 +01:00
|
|
|
}
|
2018-12-03 15:57:43 +01:00
|
|
|
|
2021-07-08 07:34:10 +02:00
|
|
|
[Route("misc/permissions")]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
|
|
|
|
public IActionResult Permissions()
|
|
|
|
{
|
2021-12-31 08:36:38 +01:00
|
|
|
return Json(Client.Models.PermissionMetadata.PermissionNodes, new JsonSerializerSettings { Formatting = Formatting.Indented });
|
2021-07-08 07:34:10 +02:00
|
|
|
}
|
2023-03-01 07:49:21 +01:00
|
|
|
[Route("misc/translations/{resource}/{lang}")]
|
|
|
|
[AllowAnonymous]
|
|
|
|
public IActionResult GetTranslations(string resource, string lang)
|
|
|
|
{
|
|
|
|
string path;
|
|
|
|
if (resource == "checkout-v1")
|
|
|
|
path = "locales";
|
|
|
|
else if (resource == "checkout-v2")
|
|
|
|
path = "locales/checkout";
|
|
|
|
else
|
|
|
|
return NotFound();
|
|
|
|
var enLang = Lang(path + "/en.json");
|
|
|
|
var en = (enLang as JsonResult)?.Value as JObject;
|
|
|
|
if (en is null || lang == "en" || lang == "en-US")
|
|
|
|
return enLang;
|
|
|
|
lang = LanguageService.FindLanguage(lang)?.Code;
|
|
|
|
if (lang is null)
|
|
|
|
return enLang;
|
|
|
|
var oLang = Lang(path + $"/{lang}.json");
|
|
|
|
var o = (oLang as JsonResult)?.Value as JObject;
|
|
|
|
if (o is null)
|
|
|
|
return enLang;
|
|
|
|
en.Merge(o);
|
|
|
|
return Json(en);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IActionResult Lang(string path)
|
|
|
|
{
|
|
|
|
var fi = _WebRootFileProvider.GetFileInfo(path);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using var fs = fi.CreateReadStream();
|
|
|
|
return Json(JObject.Load(new JsonTextReader(new StreamReader(fs, leaveOpen: true))));
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
}
|
2021-07-08 07:34:10 +02:00
|
|
|
|
2020-03-18 12:08:09 +01:00
|
|
|
[Route("swagger/v1/swagger.json")]
|
2020-06-28 10:55:27 +02:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
|
2022-03-29 20:29:27 +02:00
|
|
|
public async Task<IActionResult> Swagger([FromServices] IEnumerable<ISwaggerProvider> swaggerProviders)
|
2020-03-18 12:08:09 +01:00
|
|
|
{
|
2022-03-29 20:29:27 +02:00
|
|
|
JObject json = new();
|
|
|
|
var res = await Task.WhenAll(swaggerProviders.Select(provider => provider.Fetch()));
|
|
|
|
foreach (JObject jObject in res)
|
2020-04-01 15:48:39 +02:00
|
|
|
{
|
2022-03-29 20:29:27 +02:00
|
|
|
json.Merge(jObject);
|
2020-04-01 15:48:39 +02:00
|
|
|
}
|
2020-03-18 12:08:09 +01:00
|
|
|
var servers = new JArray();
|
|
|
|
servers.Add(new JObject(new JProperty("url", HttpContext.Request.GetAbsoluteRoot())));
|
|
|
|
json["servers"] = servers;
|
2022-04-21 05:30:49 +02:00
|
|
|
var tags = (JArray)json["tags"];
|
|
|
|
json["tags"] = new JArray(tags
|
|
|
|
.Select(o => (name: ((JObject)o)["name"].Value<string>(), o))
|
|
|
|
.OrderBy(o => o.name)
|
|
|
|
.Select(o => o.o)
|
|
|
|
.ToArray());
|
2020-03-18 12:08:09 +01:00
|
|
|
return Json(json);
|
|
|
|
}
|
2022-05-18 07:59:56 +02:00
|
|
|
|
2020-03-18 12:08:09 +01:00
|
|
|
[Route("docs")]
|
2020-05-23 21:18:51 +02:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
|
2020-03-18 12:08:09 +01:00
|
|
|
public IActionResult SwaggerDocs()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
2020-07-15 19:51:01 +02:00
|
|
|
[Route("recovery-seed-backup")]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
|
2020-07-15 19:51:01 +02:00
|
|
|
public IActionResult RecoverySeedBackup(RecoverySeedBackupViewModel vm)
|
|
|
|
{
|
|
|
|
return View("RecoverySeedBackup", vm);
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:37:49 +02:00
|
|
|
[HttpPost]
|
|
|
|
[Route("postredirect-callback-test")]
|
|
|
|
public ActionResult PostRedirectCallbackTestpage(IFormCollection data)
|
|
|
|
{
|
|
|
|
var list = data.Keys.Aggregate(new Dictionary<string, string>(), (res, key) =>
|
|
|
|
{
|
|
|
|
res.Add(key, data[key]);
|
|
|
|
return res;
|
|
|
|
});
|
|
|
|
return Json(list);
|
|
|
|
}
|
|
|
|
|
2018-05-21 13:44:03 +02:00
|
|
|
public IActionResult Error()
|
|
|
|
{
|
|
|
|
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
|
|
|
}
|
2022-01-28 09:36:48 +01:00
|
|
|
|
2023-05-26 16:49:32 +02:00
|
|
|
public RedirectToActionResult RedirectToStore(string userId, StoreData store)
|
2022-01-28 09:36:48 +01:00
|
|
|
{
|
2023-05-26 16:49:32 +02:00
|
|
|
return store.HasPermission(userId, Policies.CanModifyStoreSettings)
|
2023-01-06 14:18:07 +01:00
|
|
|
? RedirectToAction("Dashboard", "UIStores", new { storeId = store.Id })
|
2022-01-28 09:36:48 +01:00
|
|
|
: RedirectToAction("ListInvoices", "UIInvoice", new { storeId = store.Id });
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|
|
|
|
}
|