btcpayserver/BTCPayServer/Controllers/UIHomeController.cs

185 lines
6.7 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
using System.Collections.Generic;
2017-09-13 15:47:34 +09:00
using System.Diagnostics;
2020-06-28 17:55:27 +09:00
using System.IO;
using System.Linq;
2020-06-28 17:55:27 +09:00
using System.Net.Http;
2017-09-13 15:47:34 +09:00
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
using BTCPayServer.Components.StoreSelector;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Data;
2021-03-30 11:41:44 +09:00
using BTCPayServer.Filters;
2020-06-28 17:55:27 +09:00
using BTCPayServer.HostedServices;
2017-09-13 15:47:34 +09:00
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Payments;
using BTCPayServer.Payments.Lightning;
2020-06-28 17:55:27 +09:00
using BTCPayServer.Security;
using BTCPayServer.Services;
using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Stores;
using ExchangeSharp;
using Google.Apis.Auth.OAuth2;
2020-06-28 17:55:27 +09:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
2020-06-28 17:55:27 +09:00
using Microsoft.AspNetCore.Mvc;
2020-03-18 20:08:09 +09:00
using Microsoft.Extensions.FileProviders;
2020-06-28 17:55:27 +09:00
using NBitcoin;
using NBitcoin.Payment;
using NBitpayClient;
2020-06-28 17:55:27 +09:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2017-09-13 15:47:34 +09:00
namespace BTCPayServer.Controllers
{
2022-01-07 12:32:00 +09:00
public class UIHomeController : Controller
2017-09-13 15:47:34 +09:00
{
private readonly ISettingsRepository _settingsRepository;
private readonly StoreRepository _storeRepository;
private readonly BTCPayNetworkProvider _networkProvider;
private IHttpClientFactory HttpClientFactory { get; }
private SignInManager<ApplicationUser> SignInManager { get; }
public LanguageService LanguageService { get; }
2018-12-03 23:57:43 +09:00
2022-01-07 12:32:00 +09:00
public UIHomeController(IHttpClientFactory httpClientFactory,
ISettingsRepository settingsRepository,
LanguageService languageService,
StoreRepository storeRepository,
BTCPayNetworkProvider networkProvider,
SignInManager<ApplicationUser> signInManager)
2018-12-03 23:57:43 +09:00
{
_settingsRepository = settingsRepository;
2018-12-03 23:57:43 +09:00
HttpClientFactory = httpClientFactory;
LanguageService = languageService;
_networkProvider = networkProvider;
_storeRepository = storeRepository;
SignInManager = signInManager;
2018-12-03 23:57:43 +09:00
}
[HttpGet("home")]
public Task<IActionResult> Home()
{
return Index();
}
2021-03-30 11:41:44 +09:00
[Route("")]
[DomainMappingConstraint]
public async Task<IActionResult> Index()
{
if ((await _settingsRepository.GetTheme()).FirstRun)
{
2022-01-07 12:32:00 +09:00
return RedirectToAction(nameof(UIAccountController.Register), "UIAccount");
}
if (SignInManager.IsSignedIn(User))
{
var userId = SignInManager.UserManager.GetUserId(HttpContext.User);
var storeId = HttpContext.GetUserPrefsCookie()?.CurrentStoreId;
if (storeId != null)
{
// verify store exists and redirect to it
var store = await _storeRepository.FindStore(storeId, userId);
if (store != null)
{
return RedirectToStore(store);
}
}
var stores = await _storeRepository.GetStoresByUserId(userId);
if (stores.Any())
{
// redirect to first store
return RedirectToStore(stores.First());
}
var vm = new HomeViewModel
{
HasStore = stores.Any()
};
return View("Home", vm);
}
2021-12-31 16:59:02 +09:00
return Challenge();
2017-09-13 15:47:34 +09:00
}
[Route("misc/lang")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
public IActionResult Languages()
{
return Json(LanguageService.GetLanguages(), new JsonSerializerSettings { Formatting = Formatting.Indented });
}
2018-12-03 23:57:43 +09:00
[Route("misc/permissions")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
public IActionResult Permissions()
{
return Json(Client.Models.PermissionMetadata.PermissionNodes, new JsonSerializerSettings { Formatting = Formatting.Indented });
}
2020-03-18 20:08:09 +09:00
[Route("swagger/v1/swagger.json")]
2020-06-28 17:55:27 +09:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
public async Task<IActionResult> Swagger([FromServices] IEnumerable<ISwaggerProvider> swaggerProviders)
2020-03-18 20:08:09 +09:00
{
JObject json = new();
var res = await Task.WhenAll(swaggerProviders.Select(provider => provider.Fetch()));
foreach (JObject jObject in res)
{
json.Merge(jObject);
}
2020-03-18 20:08:09 +09:00
var servers = new JArray();
servers.Add(new JObject(new JProperty("url", HttpContext.Request.GetAbsoluteRoot())));
json["servers"] = servers;
2022-04-21 12:30:49 +09: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 20:08:09 +09:00
return Json(json);
}
[Route("docs")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
2020-03-18 20:08:09 +09:00
public IActionResult SwaggerDocs()
{
return View();
}
[Route("recovery-seed-backup")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
public IActionResult RecoverySeedBackup(RecoverySeedBackupViewModel vm)
{
return View("RecoverySeedBackup", vm);
}
[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);
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public RedirectToActionResult RedirectToStore(StoreData store)
{
return store.Role == StoreRoles.Owner
? RedirectToAction("Dashboard", "UIStores", new { storeId = store.Id })
: RedirectToAction("ListInvoices", "UIInvoice", new { storeId = store.Id });
}
2017-09-13 15:47:34 +09:00
}
}