Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

feat(arch001): add IAdminDataScope<PaymentTransaction> implementations (Store/Routed)

KrzysztofPajakf7b0e3cc8d0a
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedPaymentTransactionDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedPaymentTransactionDataScopeTests.cs
new file mode 100644
index 000000000..b484ee402
--- /dev/null
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedPaymentTransactionDataScopeTests.cs
@@ -0,0 +1,68 @@
+#nullable enable
+
+using Grand.Domain.Customers;
+using Grand.Domain.Payments;
+using Grand.Infrastructure;
+using Grand.Web.AdminShared.Services;
+using Microsoft.AspNetCore.Http;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Web.Admin.Tests.Controllers;
+
+[TestClass]
+public class RoutedPaymentTransactionDataScopeTests
+{
+ private const string StaffStoreId = "store-1";
+
+ private GlobalAdminDataScope<PaymentTransaction> _adminScope = null!;
+ private StorePaymentTransactionDataScope _storeScope = null!;
+
+ [TestInitialize]
+ public void Setup()
+ {
+ var workContext = new Mock<IWorkContext>();
+ workContext.Setup(x => x.CurrentCustomer).Returns(new Customer { StaffStoreId = StaffStoreId });
+ var contextAccessor = new Mock<IContextAccessor>();
+ contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object);
+
+ _adminScope = new GlobalAdminDataScope<PaymentTransaction>();
+ _storeScope = new StorePaymentTransactionDataScope(contextAccessor.Object);
+ }
+
+ private RoutedPaymentTransactionDataScope ResolverForArea(string? area)
+ {
+ var httpContext = new DefaultHttpContext();
+ if (area is not null) httpContext.Request.RouteValues["area"] = area;
+ var httpContextAccessor = new Mock<IHttpContextAccessor>();
+ httpContextAccessor.Setup(x => x.HttpContext).Returns(httpContext);
+ return new RoutedPaymentTransactionDataScope(httpContextAccessor.Object, _adminScope, _storeScope);
+ }
+
+ [TestMethod]
+ public void AdminArea_ResolvesToAdminScope()
+ {
+ var resolver = ResolverForArea("Admin");
+ Assert.IsNull(resolver.DefaultStoreId);
+ }
+
+ [TestMethod]
+ public void StoreArea_ResolvesToStoreScope()
+ {
+ var resolver = ResolverForArea("Store");
+ Assert.AreEqual(StaffStoreId, resolver.DefaultStoreId);
+ }
+
+ [TestMethod]
+ public void VendorOrUnrecognizedOrMissingArea_ThrowsFailClosed()
+ {
+ var vendorResolver = ResolverForArea("Vendor");
+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = vendorResolver.ResourceKeyPrefix);
+
+ var unknownResolver = ResolverForArea("Vue");
+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = unknownResolver.ResourceKeyPrefix);
+
+ var noAreaResolver = ResolverForArea(null);
+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = noAreaResolver.ResourceKeyPrefix);
+ }
+}
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/StorePaymentTransactionDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/StorePaymentTransactionDataScopeTests.cs
new file mode 100644
index 000000000..662998b61
--- /dev/null
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/StorePaymentTransactionDataScopeTests.cs
@@ -0,0 +1,51 @@
+using Grand.Domain.Customers;
+using Grand.Domain.Payments;
+using Grand.Infrastructure;
+using Grand.Web.AdminShared.Services;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Web.Admin.Tests.Controllers;
+
+[TestClass]
+public class StorePaymentTransactionDataScopeTests
+{
+ private static StorePaymentTransactionDataScope Build(string staffStoreId)
+ {
+ var customer = new Customer { StaffStoreId = staffStoreId };
+ var workContextMock = new Mock<IWorkContext>();
+ workContextMock.Setup(w => w.CurrentCustomer).Returns(customer);
+ var contextAccessorMock = new Mock<IContextAccessor>();
+ contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);
+ return new StorePaymentTransactionDataScope(contextAccessorMock.Object);
+ }
+
+ [TestMethod]
+ public async Task HasAccess_MatchingStoreId_True()
+ {
+ var scope = Build("store-1");
+ Assert.IsTrue(await scope.HasAccess(new PaymentTransaction { StoreId = "store-1" }));
+ }
+
+ [TestMethod]
+ public async Task HasAccess_MismatchedStoreId_False()
+ {
+ var scope = Build("store-1");
+ Assert.IsFalse(await scope.HasAccess(new PaymentTransaction { StoreId = "store-2" }));
+ }
+
+ [TestMethod]
+ public async Task HasAccess_NullEntity_False()
+ {
+ var scope = Build("store-1");
+ Assert.IsFalse(await scope.HasAccess(null));
+ }
+
+ [TestMethod]
+ public void DefaultStoreId_ReturnsStaffStoreId()
+ {
+ var scope = Build("store-1");
+ Assert.AreEqual("store-1", scope.DefaultStoreId);
+ Assert.IsNull(scope.DefaultVendorId);
+ }
+}
diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedPaymentTransactionDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedPaymentTransactionDataScope.cs
new file mode 100644
index 000000000..376670d5b
--- /dev/null
+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedPaymentTransactionDataScope.cs
@@ -0,0 +1,52 @@
+#nullable enable
+
+using Grand.Domain.Payments;
+using Grand.Web.AdminShared.Interfaces;
+using Microsoft.AspNetCore.Http;
+
+namespace Grand.Web.AdminShared.Services;
+
+/// <summary>
+/// Resolves the correct per-host <see cref="IAdminDataScope{PaymentTransaction}" />
+/// implementation at request time, based on the current request's "area" route value — same
+/// fix and same reason as <see cref="RoutedCategoryDataScope"/>/<see cref="RoutedCollectionDataScope"/>:
+/// <c>Grand.Web</c> (the combined host) <c>ProjectReference</c>s <c>Grand.Web.Admin</c> and
+/// <c>Grand.Web.Store</c> directly and discovers every <c>IStartupApplication</c> via assembly
+/// scan, so both hosts' <c>StartupApplication.ConfigureServices</c> calls run in the one
+/// process — a plain per-host <c>AddScoped&lt;IAdminDataScope&lt;PaymentTransaction&gt;, X&gt;()</c>
+/// would let whichever host's registration ran last silently win for both areas.
+///
+/// No Vendor branch: <c>Grand.Web.Vendor</c> has no <c>PaymentTransactionController</c> at all,
+/// so any "Vendor" (or other unrecognized/missing) area value fails closed, same shape as
+/// <see cref="RoutedCategoryDataScope"/>.
+/// </summary>
+public class RoutedPaymentTransactionDataScope(
+ IHttpContextAccessor httpContextAccessor,
+ GlobalAdminDataScope<PaymentTransaction> adminScope,
+ StorePaymentTransactionDataScope storeScope) : IAdminDataScope<PaymentTransaction>
+{
+ private IAdminDataScope<PaymentTransaction> Resolved
+ {
+ get
+ {
+ var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;
+ return area switch {
+ "Admin" => adminScope,
+ "Store" => storeScope,
+ //fail closed: this object fronts store tenant isolation, so an unrecognized or
+ //missing area (including "Vendor" - PaymentTransaction has no Vendor screen) must
+ //never silently resolve to the unscoped global scope
+ _ => throw new InvalidOperationException(
+ $"RoutedPaymentTransactionDataScope: unrecognized or missing area '{area}'.")
+ };
+ }
+ }
+
+ public Task<bool> HasAccess(PaymentTransaction entity) => Resolved.HasAccess(entity);
+ public Task<bool> CanView(PaymentTransaction entity) => Resolved.CanView(entity);
+ public string? DefaultStoreId => Resolved.DefaultStoreId;
+ public string ResourceKeyPrefix => Resolved.ResourceKeyPrefix;
+ public bool ShowStoreSelector => Resolved.ShowStoreSelector;
+ public string? DefaultVendorId => Resolved.DefaultVendorId;
+ public bool CanFeatureOnHomepage => Resolved.CanFeatureOnHomepage;
+}
diff --git a/src/Web/Grand.Web.AdminShared/Services/StorePaymentTransactionDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/StorePaymentTransactionDataScope.cs
new file mode 100644
index 000000000..6912a411e
--- /dev/null
+++ b/src/Web/Grand.Web.AdminShared/Services/StorePaymentTransactionDataScope.cs
@@ -0,0 +1,32 @@
+#nullable enable
+
+using Grand.Domain.Payments;
+using Grand.Infrastructure;
+using Grand.Web.AdminShared.Interfaces;
+
+namespace Grand.Web.AdminShared.Services;
+
+/// <summary>
+/// Store's <see cref="IAdminDataScope{PaymentTransaction}" />. Bespoke, not the generic
+/// <see cref="StoreAdminDataScope{TEntity}" />: <c>PaymentTransaction</c> is a plain
+/// <see cref="Grand.Domain.BaseEntity" /> with a single <c>StoreId</c> field, not
+/// <c>IStoreLinkEntity</c> (no <c>Stores</c>/<c>LimitedToStores</c> list), so the generic
+/// class's <c>where TEntity : BaseEntity, IStoreLinkEntity</c> constraint doesn't apply.
+/// Mirrors Store's original controller's <c>paymentTransaction.StoreId != StaffStoreId</c>
+/// check, repeated at every action site in that file. No <see cref="CanView" /> override:
+/// Store's original code has one uniform check for both viewing and mutating, unlike
+/// Category/Product's loose/strict split.
+/// </summary>
+public class StorePaymentTransactionDataScope(IContextAccessor contextAccessor)
+ : IAdminDataScope<PaymentTransaction>
+{
+ public Task<bool> HasAccess(PaymentTransaction entity) =>
+ Task.FromResult(entity is not null &&
+ entity.StoreId == contextAccessor.WorkContext.CurrentCustomer.StaffStoreId);
+
+ public string? DefaultStoreId => contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
+ public string ResourceKeyPrefix => "Admin";
+ public bool ShowStoreSelector => true;
+ public string? DefaultVendorId => null;
+ public bool CanFeatureOnHomepage => true; // unused for PaymentTransaction; required interface member
+}
diff --git a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
index cd4355fcc..6d43b4875 100644
--- a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
+++ b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs
@@ -2,6 +2,7 @@ using elFinder.Net.AspNetCore.Extensions;
using elFinder.Net.Drivers.FileSystem.Extensions;
using Grand.Domain.Catalog;
using Grand.Domain.Orders;
+using Grand.Domain.Payments;
using Grand.Infrastructure;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Services;
@@ -90,6 +91,13 @@ public class StartupApplication : IStartupApplication
services.AddScoped<StoreOrderDataScope>();
services.AddScoped<VendorOrderDataScope>();
services.AddScoped<IAdminDataScope<Order>, RoutedOrderDataScope>();
+
+ // IAdminDataScope<PaymentTransaction>: registered once here for the same reason as
+ // Product/Category/Collection/Order above — see RoutedPaymentTransactionDataScope's doc
+ // comment. No Vendor scope: PaymentTransaction has no Vendor screen.
+ services.AddScoped<GlobalAdminDataScope<PaymentTransaction>>();
+ services.AddScoped<StorePaymentTransactionDataScope>();
+ services.AddScoped<IAdminDataScope<PaymentTransaction>, RoutedPaymentTransactionDataScope>();
}
public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)