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 RoutedCollectionDataScope and register IAdminDataScope<Collection>
KrzysztofPajak018670411085
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedCollectionDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedCollectionDataScopeTests.csnew file mode 100644index 000000000..320e025d5--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedCollectionDataScopeTests.cs@@ -0,0 +1,64 @@+using Grand.Domain.Catalog;+using Grand.Web.AdminShared.Interfaces;+using Grand.Web.AdminShared.Services;+using Microsoft.AspNetCore.Http;+using Microsoft.AspNetCore.Routing;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Web.Admin.Tests.Controllers;++[TestClass]+public class RoutedCollectionDataScopeTests+{+ private static RoutedCollectionDataScope Build(string area)+ {+ var httpContext = new DefaultHttpContext();+ if (area != null)+ httpContext.Request.RouteValues = new RouteValueDictionary { ["area"] = area };++ var httpContextAccessorMock = new Mock<IHttpContextAccessor>();+ httpContextAccessorMock.Setup(a => a.HttpContext).Returns(httpContext);++ var global = new GlobalAdminDataScope<Collection>();+ var store = new StoreAdminDataScope<Collection>(BuildContextAccessor());+ return new RoutedCollectionDataScope(httpContextAccessorMock.Object, global, store);+ }++ private static Grand.Infrastructure.IContextAccessor BuildContextAccessor()+ {+ var workContext = new Mock<Grand.Infrastructure.IWorkContext>();+ workContext.Setup(w => w.CurrentCustomer).Returns(new Grand.Domain.Customers.Customer { StaffStoreId = "store-1" });+ var contextAccessorMock = new Mock<Grand.Infrastructure.IContextAccessor>();+ contextAccessorMock.Setup(c => c.WorkContext).Returns(workContext.Object);+ return contextAccessorMock.Object;+ }++ [TestMethod]+ public void DefaultStoreId_AdminArea_ResolvesToGlobalScope()+ {+ var routed = Build("Admin");+ Assert.IsNull(routed.DefaultStoreId);+ }++ [TestMethod]+ public void DefaultStoreId_StoreArea_ResolvesToStoreScope()+ {+ var routed = Build("Store");+ Assert.AreEqual("store-1", routed.DefaultStoreId);+ }++ [TestMethod]+ public void DefaultStoreId_VendorArea_ThrowsFailClosed()+ {+ var routed = Build("Vendor");+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = routed.DefaultStoreId);+ }++ [TestMethod]+ public void DefaultStoreId_MissingArea_ThrowsFailClosed()+ {+ var routed = Build(null);+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = routed.DefaultStoreId);+ }+}diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedCollectionDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedCollectionDataScope.csnew file mode 100644index 000000000..7dd4d2a69--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedCollectionDataScope.cs@@ -0,0 +1,56 @@+#nullable enable++using Grand.Domain.Catalog;+using Grand.Web.AdminShared.Interfaces;+using Microsoft.AspNetCore.Http;++namespace Grand.Web.AdminShared.Services;++/// <summary>+/// Resolves the correct per-host <see cref="IAdminDataScope{Collection}" /> implementation at+/// request time, based on the current request's "area" route value — same fix and same reason+/// as <see cref="RoutedProductDataScope"/>/<see cref="RoutedCategoryDataScope"/> (see those+/// files' doc comments): Grand.Web (the combined host) loads Admin and Store together in one+/// DI container, so a plain AddScoped<IAdminDataScope<Collection>, X>() per host+/// would silently let whichever host's StartupApplication ran last win for every area in that+/// process.+///+/// Unlike Product, there is no Vendor branch: Vendor has no Collection screen at all, so any+/// "Vendor" (or other unrecognized/missing) area value fails closed.+/// </summary>+public class RoutedCollectionDataScope(+ IHttpContextAccessor httpContextAccessor,+ GlobalAdminDataScope<Collection> globalScope,+ StoreAdminDataScope<Collection> storeScope) : IAdminDataScope<Collection>+{+ private IAdminDataScope<Collection> Resolved+ {+ get+ {+ var area = httpContextAccessor.HttpContext?.Request.RouteValues["area"] as string;+ return area switch {+ "Admin" => globalScope,+ "Store" => storeScope,+ //fail closed: this object fronts store tenant isolation, so an unrecognized or+ //missing area (including "Vendor" - Collection has no Vendor screen) must never+ //silently resolve to the unscoped global scope+ _ => throw new InvalidOperationException(+ $"RoutedCollectionDataScope: unrecognized or missing area '{area}'.")+ };+ }+ }++ public Task<bool> HasAccess(Collection entity) => Resolved.HasAccess(entity);++ public Task<bool> CanView(Collection 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/Startup/StartupApplication.cs b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.csindex f2a319048..bd84eb0ac 100644--- a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs@@ -76,6 +76,12 @@ public class StartupApplication : IStartupApplicationservices.AddScoped<GlobalAdminDataScope<Category>>();services.AddScoped<StoreAdminDataScope<Category>>();services.AddScoped<IAdminDataScope<Category>, RoutedCategoryDataScope>();++ // IAdminDataScope<Collection>: registered once here for the same reason as Category above — see+ // RoutedCollectionDataScope's doc comment. No Vendor scope: Collection has no Vendor screen.+ services.AddScoped<GlobalAdminDataScope<Collection>>();+ services.AddScoped<StoreAdminDataScope<Collection>>();+ services.AddScoped<IAdminDataScope<Collection>, RoutedCollectionDataScope>();}public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)