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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
feat(arch001): add IAdminDataScope<Shipment> implementations (Store/Vendor/Routed)
KrzysztofPajakcd62b2fa10f0
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedShipmentDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedShipmentDataScopeTests.csnew file mode 100644index 000000000..1f01eaf51--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedShipmentDataScopeTests.cs@@ -0,0 +1,81 @@+#nullable enable++using Grand.Domain.Customers;+using Grand.Domain.Shipping;+using Grand.Domain.Vendors;+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 RoutedShipmentDataScopeTests+{+ private const string StaffStoreId = "store-1";+ private const string VendorId = "vendor-1";++ private GlobalAdminDataScope<Shipment> _adminScope = null!;+ private StoreShipmentDataScope _storeScope = null!;+ private VendorShipmentDataScope _vendorScope = null!;++ [TestInitialize]+ public void Setup()+ {+ var workContext = new Mock<IWorkContext>();+ workContext.Setup(x => x.CurrentCustomer).Returns(new Customer { StaffStoreId = StaffStoreId });+ workContext.Setup(x => x.CurrentVendor).Returns(new Vendor { Id = VendorId });+ var contextAccessor = new Mock<IContextAccessor>();+ contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object);++ _adminScope = new GlobalAdminDataScope<Shipment>();+ _storeScope = new StoreShipmentDataScope(contextAccessor.Object);+ _vendorScope = new VendorShipmentDataScope(contextAccessor.Object);+ }++ private RoutedShipmentDataScope 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 RoutedShipmentDataScope(httpContextAccessor.Object, _adminScope, _storeScope, _vendorScope);+ }++ [TestMethod]+ public void AdminArea_ResolvesToAdminScope()+ {+ var resolver = ResolverForArea("Admin");+ Assert.IsNull(resolver.DefaultStoreId);+ Assert.IsNull(resolver.DefaultVendorId);+ }++ [TestMethod]+ public void StoreArea_ResolvesToStoreScope()+ {+ var resolver = ResolverForArea("Store");+ Assert.AreEqual(StaffStoreId, resolver.DefaultStoreId);+ Assert.IsNull(resolver.DefaultVendorId);+ }++ [TestMethod]+ public void VendorArea_ResolvesToVendorScope()+ {+ var resolver = ResolverForArea("Vendor");+ Assert.AreEqual("Vendor", resolver.ResourceKeyPrefix);+ Assert.AreEqual(VendorId, resolver.DefaultVendorId);+ Assert.IsFalse(resolver.ShowStoreSelector);+ }++ [TestMethod]+ public void UnrecognizedOrMissingArea_ThrowsFailClosed()+ {+ var resolver = ResolverForArea("Vue");+ Assert.Throws<InvalidOperationException>(() => _ = resolver.ResourceKeyPrefix);++ var resolverNoArea = ResolverForArea(null);+ Assert.Throws<InvalidOperationException>(() => _ = resolverNoArea.ResourceKeyPrefix);+ }+}diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/StoreShipmentDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/StoreShipmentDataScopeTests.csnew file mode 100644index 000000000..3fc769762--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/StoreShipmentDataScopeTests.cs@@ -0,0 +1,51 @@+using Grand.Domain.Customers;+using Grand.Domain.Shipping;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Services;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Web.Admin.Tests.Controllers;++[TestClass]+public class StoreShipmentDataScopeTests+{+ private static StoreShipmentDataScope 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 StoreShipmentDataScope(contextAccessorMock.Object);+ }++ [TestMethod]+ public async Task HasAccess_MatchingStoreId_True()+ {+ var scope = Build("store-1");+ Assert.IsTrue(await scope.HasAccess(new Shipment { StoreId = "store-1" }));+ }++ [TestMethod]+ public async Task HasAccess_MismatchedStoreId_False()+ {+ var scope = Build("store-1");+ Assert.IsFalse(await scope.HasAccess(new Shipment { 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/Tests/Grand.Web.Admin.Tests/Controllers/VendorShipmentDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/VendorShipmentDataScopeTests.csnew file mode 100644index 000000000..030c44194--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/VendorShipmentDataScopeTests.cs@@ -0,0 +1,67 @@+using Grand.Domain.Orders;+using Grand.Domain.Shipping;+using Grand.Domain.Vendors;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Services;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Web.Admin.Tests.Controllers;++[TestClass]+public class VendorShipmentDataScopeTests+{+ private static VendorShipmentDataScope Build(string currentVendorId)+ {+ var workContextMock = new Mock<IWorkContext>();+ workContextMock.Setup(w => w.CurrentVendor).Returns(new Vendor { Id = currentVendorId });+ var contextAccessorMock = new Mock<IContextAccessor>();+ contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object);+ return new VendorShipmentDataScope(contextAccessorMock.Object);+ }++ [TestMethod]+ public async Task HasAccess_MatchingVendorId_True()+ {+ var scope = Build("vendor-A");+ Assert.IsTrue(await scope.HasAccess(new Shipment { VendorId = "vendor-A" }));+ }++ [TestMethod]+ public async Task HasAccess_MismatchedVendorId_False()+ {+ var scope = Build("vendor-A");+ Assert.IsFalse(await scope.HasAccess(new Shipment { VendorId = "vendor-B" }));+ }++ [TestMethod]+ public async Task HasAccess_NullEntity_False()+ {+ var scope = Build("vendor-A");+ Assert.IsFalse(await scope.HasAccess(null));+ }++ [TestMethod]+ public void FilterOrderItems_MixedVendorOrder_ReturnsOnlyOwnItems()+ {+ var scope = Build("vendor-A");+ var itemA1 = new OrderItem { Id = "i1", VendorId = "vendor-A" };+ var itemB = new OrderItem { Id = "i2", VendorId = "vendor-B" };+ var itemA2 = new OrderItem { Id = "i3", VendorId = "vendor-A" };++ var filtered = scope.FilterOrderItems([itemA1, itemB, itemA2]).ToList();++ CollectionAssert.AreEqual(new[] { itemA1, itemA2 }, filtered);+ }++ [TestMethod]+ public void ScopeDefaults_VendorScoped()+ {+ var scope = Build("vendor-A");+ Assert.IsNull(scope.DefaultStoreId);+ Assert.AreEqual("vendor-A", scope.DefaultVendorId);+ Assert.AreEqual("Vendor", scope.ResourceKeyPrefix);+ Assert.IsFalse(scope.ShowStoreSelector);+ Assert.IsFalse(scope.CanFeatureOnHomepage);+ }+}diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedShipmentDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedShipmentDataScope.csnew file mode 100644index 000000000..30a916a64--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedShipmentDataScope.cs@@ -0,0 +1,50 @@+#nullable enable++using Grand.Domain.Orders;+using Grand.Domain.Shipping;+using Grand.Web.AdminShared.Interfaces;+using Microsoft.AspNetCore.Http;++namespace Grand.Web.AdminShared.Services;++/// <summary>+/// Resolves the correct per-host <see cref="IAdminDataScope{Shipment}" /> implementation at+/// request time, based on the current request's "area" route value — same fix and same reason+/// as <see cref="RoutedOrderDataScope"/>/<see cref="RoutedProductDataScope"/>: <c>Grand.Web</c>+/// (the combined host) loads all three hosts into one DI container, so a plain per-host+/// registration would let whichever host's StartupApplication ran last win for every area in+/// that process.+/// </summary>+public class RoutedShipmentDataScope(+ IHttpContextAccessor httpContextAccessor,+ GlobalAdminDataScope<Shipment> adminScope,+ StoreShipmentDataScope storeScope,+ VendorShipmentDataScope vendorScope) : IAdminDataScope<Shipment>+{+ private IAdminDataScope<Shipment> Resolved+ {+ get+ {+ var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;+ return area switch {+ "Admin" => adminScope,+ "Store" => storeScope,+ "Vendor" => vendorScope,+ //fail closed: this object fronts store/vendor tenant isolation, so an+ //unrecognized or missing area must never silently resolve to any concrete scope+ _ => throw new InvalidOperationException(+ $"RoutedShipmentDataScope: unrecognized or missing area '{area}'.")+ };+ }+ }++ public Task<bool> HasAccess(Shipment entity) => Resolved.HasAccess(entity);+ public Task<bool> CanView(Shipment entity) => Resolved.CanView(entity);+ public IEnumerable<OrderItem> FilterOrderItems(IEnumerable<OrderItem> orderItems) =>+ Resolved.FilterOrderItems(orderItems);+ 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/StoreShipmentDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/StoreShipmentDataScope.csnew file mode 100644index 000000000..6a6ffeb21--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/StoreShipmentDataScope.cs@@ -0,0 +1,30 @@+#nullable enable++using Grand.Domain.Shipping;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Interfaces;++namespace Grand.Web.AdminShared.Services;++/// <summary>+/// Store's <see cref="IAdminDataScope{Shipment}" />. Bespoke, not the generic+/// <see cref="StoreAdminDataScope{TEntity}" />: <c>Shipment</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>shipment.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 StoreShipmentDataScope(IContextAccessor contextAccessor) : IAdminDataScope<Shipment>+{+ public Task<bool> HasAccess(Shipment 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 Shipment; required interface member+}diff --git a/src/Web/Grand.Web.AdminShared/Services/VendorShipmentDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/VendorShipmentDataScope.csnew file mode 100644index 000000000..edaa95d3a--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/VendorShipmentDataScope.cs@@ -0,0 +1,37 @@+#nullable enable++using Grand.Domain.Orders;+using Grand.Domain.Shipping;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Interfaces;++namespace Grand.Web.AdminShared.Services;++/// <summary>+/// Vendor's <see cref="IAdminDataScope{Shipment}" />. Bespoke: ownership is a flat+/// <c>VendorId</c> field directly on the entity, simpler than Order's child-collection+/// ownership — ports the existing <c>HasAccessToShipment</c>/<c>HasAccessToOrderItem</c>+/// extension methods (<c>Grand.Web.Vendor/Extensions/HasAccess.cs</c>) inline, the same way+/// <see cref="VendorOrderDataScope" />/<see cref="VendorProductDataScope" /> do — not imported+/// directly, since <c>Grand.Web.Vendor</c> already references <c>Grand.Web.AdminShared</c> and+/// a reference the other way would be circular. Also overrides <see cref="FilterOrderItems" />,+/// reusing the interface member the Order phase already added: ports Vendor's original+/// <c>order.OrderItems.Where(HasAccessToOrderItem)</c> filter (used when building the+/// AddShipment order-item picker) so a vendor can only ship its own line items on a+/// mixed-vendor order.+/// </summary>+public class VendorShipmentDataScope(IContextAccessor contextAccessor) : IAdminDataScope<Shipment>+{+ public Task<bool> HasAccess(Shipment entity) =>+ Task.FromResult(entity is not null &&+ entity.VendorId == contextAccessor.WorkContext.CurrentVendor.Id);++ public IEnumerable<OrderItem> FilterOrderItems(IEnumerable<OrderItem> orderItems) =>+ orderItems.Where(i => i.VendorId == contextAccessor.WorkContext.CurrentVendor.Id);++ public string? DefaultStoreId => null;+ public string ResourceKeyPrefix => "Vendor";+ public bool ShowStoreSelector => false;+ public string? DefaultVendorId => contextAccessor.WorkContext.CurrentVendor.Id;+ public bool CanFeatureOnHomepage => false;+}diff --git a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.csindex cd4355fcc..b3d250217 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.Shipping;using Grand.Infrastructure;using Grand.Web.AdminShared.Interfaces;using Grand.Web.AdminShared.Services;@@ -90,6 +91,15 @@ public class StartupApplication : IStartupApplicationservices.AddScoped<StoreOrderDataScope>();services.AddScoped<VendorOrderDataScope>();services.AddScoped<IAdminDataScope<Order>, RoutedOrderDataScope>();++ // IAdminDataScope<Shipment>: registered once here for the same reason as Order above — see+ // RoutedShipmentDataScope's doc comment. Admin reuses the generic GlobalAdminDataScope<T>+ // unmodified (no Sales-Manager restriction on Shipment); Store/Vendor are bespoke because+ // Shipment isn't IStoreLinkEntity and Vendor ownership is a flat VendorId field.+ services.AddScoped<GlobalAdminDataScope<Shipment>>();+ services.AddScoped<StoreShipmentDataScope>();+ services.AddScoped<VendorShipmentDataScope>();+ services.AddScoped<IAdminDataScope<Shipment>, RoutedShipmentDataScope>();}public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)