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 RoutedCategoryDataScope and register IAdminDataScope<Category>
KrzysztofPajak68e382f19998
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedCategoryDataScopeTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedCategoryDataScopeTests.csnew file mode 100644index 000000000..7d9896257--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/RoutedCategoryDataScopeTests.cs@@ -0,0 +1,66 @@+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 RoutedCategoryDataScopeTests+{+ private static RoutedCategoryDataScope Build(string area, out Mock<IAdminDataScope<Category>> globalMock, out Mock<IAdminDataScope<Category>> storeMock)+ {+ 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<Category>();+ var store = new StoreAdminDataScope<Category>(BuildContextAccessor());+ globalMock = null;+ storeMock = null;+ return new RoutedCategoryDataScope(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", out _, out _);+ Assert.IsNull(routed.DefaultStoreId);+ }++ [TestMethod]+ public void DefaultStoreId_StoreArea_ResolvesToStoreScope()+ {+ var routed = Build("Store", out _, out _);+ Assert.AreEqual("store-1", routed.DefaultStoreId);+ }++ [TestMethod]+ public void DefaultStoreId_VendorArea_ThrowsFailClosed()+ {+ var routed = Build("Vendor", out _, out _);+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = routed.DefaultStoreId);+ }++ [TestMethod]+ public void DefaultStoreId_MissingArea_ThrowsFailClosed()+ {+ var routed = Build(null, out _, out _);+ Assert.ThrowsExactly<InvalidOperationException>(() => _ = routed.DefaultStoreId);+ }+}diff --git a/src/Web/Grand.Web.AdminShared/Services/RoutedCategoryDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/RoutedCategoryDataScope.csnew file mode 100644index 000000000..4aaae71dc--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/RoutedCategoryDataScope.cs@@ -0,0 +1,55 @@+#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{Category}" /> implementation at+/// request time, based on the current request's "area" route value — same fix and same reason+/// as <see cref="RoutedProductDataScope"/> (see that file's doc comment): Grand.Web (the+/// combined host) loads Admin and Store together in one DI container, so a plain+/// AddScoped<IAdminDataScope<Category>, 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 Category screen at all, so any+/// "Vendor" (or other unrecognized/missing) area value fails closed.+/// </summary>+public class RoutedCategoryDataScope(+ IHttpContextAccessor httpContextAccessor,+ GlobalAdminDataScope<Category> globalScope,+ StoreAdminDataScope<Category> storeScope) : IAdminDataScope<Category>+{+ private IAdminDataScope<Category> 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" - Category has no Vendor screen) must never+ //silently resolve to the unscoped global scope+ _ => throw new InvalidOperationException(+ $"RoutedCategoryDataScope: unrecognized or missing area '{area}'.")+ };+ }+ }++ public Task<bool> HasAccess(Category entity) => Resolved.HasAccess(entity);++ public Task<bool> CanView(Category 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 51937c430..f2a319048 100644--- a/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs+++ b/src/Web/Grand.Web.AdminShared/Startup/StartupApplication.cs@@ -70,6 +70,12 @@ public class StartupApplication : IStartupApplicationservices.AddScoped<StoreAdminDataScope<Product>>();services.AddScoped<VendorProductDataScope>();services.AddScoped<IAdminDataScope<Product>, RoutedProductDataScope>();++ // IAdminDataScope<Category>: registered once here for the same reason as Product above — see+ // RoutedCategoryDataScope's doc comment. No Vendor scope: Category has no Vendor screen.+ services.AddScoped<GlobalAdminDataScope<Category>>();+ services.AddScoped<StoreAdminDataScope<Category>>();+ services.AddScoped<IAdminDataScope<Category>, RoutedCategoryDataScope>();}public void Configure(WebApplication application, IWebHostEnvironment webHostEnvironment)