btcpayserver/BTCPayServer/Controllers/UserStoresController.cs

87 lines
3.1 KiB
C#
Raw Normal View History

2020-08-27 10:31:33 +02:00
using System.Linq;
2018-03-23 08:24:57 +01:00
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Client;
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;
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]
public class UserStoresController : Controller
2018-03-23 08:24:57 +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)
{
_repo = storeRepository;
_userManager = userManager;
2020-06-28 10:55:27 +02:00
}
[HttpGet("create")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
public IActionResult CreateStore()
{
return View();
2018-03-23 08:24:57 +01:00
}
[HttpPost("create")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettingsUnscoped)]
public async Task<IActionResult> CreateStore(CreateStoreViewModel vm)
{
if (!ModelState.IsValid)
{
return View(vm);
}
var store = await _repo.CreateStore(GetUserId(), vm.Name);
CreatedStoreId = store.Id;
TempData[WellKnownTempData.SuccessMessage] = "Store successfully created";
return RedirectToAction(nameof(StoresController.Dashboard), "Stores", new
{
storeId = store.Id
});
}
public string CreatedStoreId
{
get; set;
}
[HttpGet("{storeId}/me/delete")]
[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();
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
}
[HttpPost("{storeId}/me/delete")]
[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();
await _repo.RemoveStore(storeId, userId);
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
}
private string GetUserId() => _userManager.GetUserId(User);
2018-03-23 08:24:57 +01:00
}
}