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
Add StoreAdminDataScope for the Store host (ARCH-001 Phase 1)
KrzysztofPajak76aa1bf20318
diff --git a/src/Tests/Grand.Web.Store.Tests/Services/StoreAdminDataScopeTests.cs b/src/Tests/Grand.Web.Store.Tests/Services/StoreAdminDataScopeTests.csnew file mode 100644index 000000000..e27abfd5c--- /dev/null+++ b/src/Tests/Grand.Web.Store.Tests/Services/StoreAdminDataScopeTests.cs@@ -0,0 +1,67 @@+using Grand.Domain.Catalog;+using Grand.Domain.Customers;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Services;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Moq;++namespace Grand.Web.Store.Tests.Services;++[TestClass]+public class StoreAdminDataScopeTests+{+ private Mock<IContextAccessor> _contextAccessor = null!;+ private const string StaffStoreId = "store-1";++ [TestInitialize]+ public void Setup()+ {+ var workContext = new Mock<IWorkContext>();+ workContext.Setup(x => x.CurrentCustomer).Returns(new Customer { StaffStoreId = StaffStoreId });+ _contextAccessor = new Mock<IContextAccessor>();+ _contextAccessor.Setup(x => x.WorkContext).Returns(workContext.Object);+ }++ [TestMethod]+ public async Task HasAccess_ProductNotLimitedToStores_ReturnsTrue()+ {+ var scope = new StoreAdminDataScope<Product>(_contextAccessor.Object);+ var product = new Product { LimitedToStores = false };++ Assert.IsTrue(await scope.HasAccess(product));+ }++ [TestMethod]+ public async Task HasAccess_ProductLimitedToOtherStore_ReturnsFalse()+ {+ var scope = new StoreAdminDataScope<Product>(_contextAccessor.Object);+ var product = new Product { LimitedToStores = true, Stores = ["store-2"] };++ Assert.IsFalse(await scope.HasAccess(product));+ }++ [TestMethod]+ public async Task HasAccess_ProductLimitedToStaffStore_ReturnsTrue()+ {+ var scope = new StoreAdminDataScope<Product>(_contextAccessor.Object);+ var product = new Product { LimitedToStores = true, Stores = [StaffStoreId] };++ Assert.IsTrue(await scope.HasAccess(product));+ }++ [TestMethod]+ public void DefaultStoreId_ReturnsStaffStoreId()+ {+ var scope = new StoreAdminDataScope<Product>(_contextAccessor.Object);+ Assert.AreEqual(StaffStoreId, scope.DefaultStoreId);+ }++ [TestMethod]+ public void ResourceKeyPrefix_IsAdmin()+ {+ // Store has no distinct resource set for Product screens yet (see Task 6) — it renders+ // AdminShared's "Admin.*" keys today, so the migrated scope must keep that behavior.+ var scope = new StoreAdminDataScope<Product>(_contextAccessor.Object);+ Assert.AreEqual("Admin", scope.ResourceKeyPrefix);+ }+}diff --git a/src/Web/Grand.Web.AdminShared/Services/StoreAdminDataScope.cs b/src/Web/Grand.Web.AdminShared/Services/StoreAdminDataScope.csnew file mode 100644index 000000000..a2087751f--- /dev/null+++ b/src/Web/Grand.Web.AdminShared/Services/StoreAdminDataScope.cs@@ -0,0 +1,31 @@+#nullable enable++using Grand.Domain.Stores;+using Grand.Infrastructure;+using Grand.Web.AdminShared.Interfaces;++namespace Grand.Web.AdminShared.Services;++public class StoreAdminDataScope<TEntity>(IContextAccessor contextAccessor) : IAdminDataScope<TEntity>+ where TEntity : IStoreLinkEntity+{+ public Task<bool> HasAccess(TEntity entity)+ {+ if (entity is null) return Task.FromResult(false);++ var staffStoreId = contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;+ var allowed = !entity.LimitedToStores || entity.Stores.Contains(staffStoreId);+ return Task.FromResult(allowed);+ }++ public IQueryable<TEntity> ApplyScope(IQueryable<TEntity> query)+ {+ var staffStoreId = contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;+ if (string.IsNullOrEmpty(staffStoreId)) return query;+ return query.Where(x => !x.LimitedToStores || x.Stores.Contains(staffStoreId));+ }++ public string? DefaultStoreId => contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;++ public string ResourceKeyPrefix => "Admin";+}