Skip to content

Commit

Permalink
Create interface for providing store id to plugins (btcpayserver#3910)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks authored Jun 29, 2022
1 parent ed1f249 commit b8f1c0d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions BTCPayServer.Abstractions/Contracts/IScopeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#nullable enable
namespace BTCPayServer.Abstractions.Contracts;

public interface IScopeProvider
{
string? GetCurrentStoreId();
}
6 changes: 6 additions & 0 deletions BTCPayServer/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -257,6 +258,11 @@ private static void SetCurrentStoreId(this HttpContext ctx, string storeId)
}
}

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;
Expand Down
1 change: 1 addition & 0 deletions BTCPayServer/Hosting/BTCPayServerServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static IServiceCollection AddBTCPayServer(this IServiceCollection service
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>());
Expand Down
19 changes: 19 additions & 0 deletions BTCPayServer/Services/Stores/ScopeProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit b8f1c0d

Please sign in to comment.