Create interface for providing store id to plugins (#3910)

This commit is contained in:
Andrew Camilleri 2022-06-29 16:18:02 +02:00 committed by GitHub
parent ed1f249aaf
commit b8f1c0df09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,7 @@
#nullable enable
namespace BTCPayServer.Abstractions.Contracts;
public interface IScopeProvider
{
string? GetCurrentStoreId();
}

View file

@ -22,6 +22,7 @@ using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Payments;
using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Security;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Wallets;
using Microsoft.AspNetCore.Http;
@ -257,6 +258,11 @@ namespace BTCPayServer
}
}
public static string GetCurrentStoreId(this HttpContext ctx)
{
return ctx.GetImplicitStoreId() ?? ctx.GetUserPrefsCookie()?.CurrentStoreId;
}
public static StoreData GetStoreData(this HttpContext ctx)
{
return ctx.Items.TryGet("BTCPAY.STOREDATA") as StoreData;

View file

@ -98,6 +98,7 @@ namespace BTCPayServer.Hosting
if (configuration.SupportChain("yec") || configuration.SupportChain("zec"))
services.AddZcashLike();
#endif
services.AddScoped<IScopeProvider, ScopeProvider>();
services.TryAddSingleton<SettingsRepository>();
services.TryAddSingleton<ISettingsRepository>(provider => provider.GetService<SettingsRepository>());
services.TryAddSingleton<IStoreRepository>(provider => provider.GetService<StoreRepository>());

View file

@ -0,0 +1,19 @@
#nullable enable
using BTCPayServer.Abstractions.Contracts;
using Microsoft.AspNetCore.Http;
namespace BTCPayServer.Services.Stores;
public class ScopeProvider : IScopeProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
public ScopeProvider(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string? GetCurrentStoreId()
{
return _httpContextAccessor.HttpContext.GetStoreData()?.Id;
}
}