2020-08-27 10:31:33 +02:00
|
|
|
using System.Linq;
|
2018-03-23 08:24:57 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2021-12-31 08:36:38 +01:00
|
|
|
using BTCPayServer.Client;
|
2019-08-29 17:24:42 +02:00
|
|
|
using BTCPayServer.Data;
|
2018-03-23 08:24:57 +01:00
|
|
|
using BTCPayServer.Models;
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2018-04-29 19:33:42 +02:00
|
|
|
using BTCPayServer.Security;
|
2018-03-23 08:24:57 +01:00
|
|
|
using BTCPayServer.Services.Stores;
|
2020-10-16 07:35:53 +02:00
|
|
|
using ExchangeSharp;
|
2018-03-23 08:24:57 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
[Route("stores")]
|
|
|
|
[AutoValidateAntiforgeryToken]
|
2021-12-31 08:36:38 +01:00
|
|
|
public class UserStoresController : Controller
|
2018-03-23 08:24:57 +01:00
|
|
|
{
|
2021-12-31 08:36:38 +01:00
|
|
|
private readonly StoreRepository _repo;
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2018-03-23 08:24:57 +01:00
|
|
|
|
|
|
|
public UserStoresController(
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
StoreRepository storeRepository)
|
|
|
|
{
|
2021-12-31 08:36:38 +01:00
|
|
|
_repo = storeRepository;
|
|
|
|
_userManager = userManager;
|
2020-06-28 10:55:27 +02:00
|
|
|
}
|
2018-07-19 15:23:14 +02:00
|
|
|
|
2021-12-31 08:36:38 +01:00
|
|
|
[HttpGet("create")]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
|
2018-07-19 15:23:14 +02:00
|
|
|
public IActionResult CreateStore()
|
|
|
|
{
|
|
|
|
return View();
|
2018-03-23 08:24:57 +01:00
|
|
|
}
|
2018-07-19 15:23:14 +02:00
|
|
|
|
2021-12-31 08:36:38 +01:00
|
|
|
[HttpPost("create")]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
|
2020-10-16 06:30:46 +02:00
|
|
|
public async Task<IActionResult> CreateStore(CreateStoreViewModel vm)
|
|
|
|
{
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(vm);
|
|
|
|
}
|
2021-12-31 08:36:38 +01:00
|
|
|
var store = await _repo.CreateStore(GetUserId(), vm.Name);
|
2020-10-16 06:30:46 +02:00
|
|
|
CreatedStoreId = store.Id;
|
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "Store successfully created";
|
2022-01-13 09:08:15 +01:00
|
|
|
return RedirectToAction(nameof(StoresController.Dashboard), "Stores", new
|
2020-10-16 06:30:46 +02:00
|
|
|
{
|
|
|
|
storeId = store.Id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:23:14 +02:00
|
|
|
public string CreatedStoreId
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
|
2021-09-07 04:55:53 +02:00
|
|
|
[HttpGet("{storeId}/me/delete")]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
|
2018-04-29 19:33:42 +02:00
|
|
|
public IActionResult DeleteStore(string storeId)
|
2018-03-23 08:24:57 +01:00
|
|
|
{
|
2018-04-29 19:33:42 +02:00
|
|
|
var store = HttpContext.GetStoreData();
|
2018-03-23 08:24:57 +01:00
|
|
|
if (store == null)
|
|
|
|
return NotFound();
|
2021-09-07 04:55:53 +02:00
|
|
|
return View("Confirm", new ConfirmModel($"Delete store {store.StoreName}", "This store will still be accessible to users sharing it", "Delete"));
|
2018-03-23 08:24:57 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 04:55:53 +02:00
|
|
|
[HttpPost("{storeId}/me/delete")]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
|
2018-03-23 08:24:57 +01:00
|
|
|
public async Task<IActionResult> DeleteStorePost(string storeId)
|
|
|
|
{
|
|
|
|
var userId = GetUserId();
|
2018-04-29 19:33:42 +02:00
|
|
|
var store = HttpContext.GetStoreData();
|
2018-03-23 08:24:57 +01:00
|
|
|
if (store == null)
|
|
|
|
return NotFound();
|
2021-12-31 08:36:38 +01:00
|
|
|
await _repo.RemoveStore(storeId, userId);
|
2019-10-31 04:29:59 +01:00
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "Store removed successfully";
|
2022-01-14 03:59:27 +01:00
|
|
|
return RedirectToAction(nameof(HomeController.Index), "Home");
|
2018-03-23 08:24:57 +01:00
|
|
|
}
|
|
|
|
|
2021-12-31 08:36:38 +01:00
|
|
|
private string GetUserId() => _userManager.GetUserId(User);
|
2018-03-23 08:24:57 +01:00
|
|
|
}
|
|
|
|
}
|