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
Filter selected product ids by ownership in Insert*ProductModel (ARCH-001 Phase 1 Task 11)
KrzysztofPajak2bc3309797f4
diff --git a/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.cs b/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.csindex 367f93b1c..250255df3 100644--- a/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Services/ProductViewModelServiceTests.cs@@ -473,4 +473,124 @@ public class ProductViewModelServiceTestsAssert.AreEqual("vendor2", result.VendorId,"Admin should keep being able to reassign a product's vendor ownership via the edit form.");}++ // --- Per-id ownership filter on Insert*ProductModel (ARCH-001 Phase 1 Task 11) ------------------+ // Mirrors Vendor's original InsertRelatedProductModel ("if (product == null ||+ // !HasAccessToProduct(product)) continue;") and the pattern already used for the selected-ids loop+ // in BaseProductController.AssociatedProductAddPopup(POST) (Task 8). This gap was inert while Vendor+ // wasn't yet subclassed onto BaseProductController; wiring Vendor in (Task 11) makes it live.++ private void SetupOwnershipForInsertTests(Product ownedProduct, Product notOwnedProduct)+ {+ _productServiceMock.Setup(p => p.GetProductById(ownedProduct.Id, It.IsAny<bool>())).ReturnsAsync(ownedProduct);+ _productServiceMock.Setup(p => p.GetProductById(notOwnedProduct.Id, It.IsAny<bool>())).ReturnsAsync(notOwnedProduct);+ _scopeMock.Setup(s => s.HasAccess(ownedProduct)).ReturnsAsync(true);+ _scopeMock.Setup(s => s.HasAccess(notOwnedProduct)).ReturnsAsync(false);+ }++ [TestMethod]+ public async Task InsertRelatedProductModel_DropsNotOwnedSelectedId()+ {+ var mainProduct = new Product { Id = "main" };+ var owned = new Product { Id = "owned" };+ var notOwned = new Product { Id = "not-owned" };+ _productServiceMock.Setup(p => p.GetProductById("main", It.IsAny<bool>())).ReturnsAsync(mainProduct);+ SetupOwnershipForInsertTests(owned, notOwned);++ var model = new ProductModel.AddRelatedProductModel {+ ProductId = "main",+ SelectedProductIds = ["owned", "not-owned"]+ };+ await _productViewModelService.InsertRelatedProductModel(model);++ Assert.IsTrue(mainProduct.RelatedProducts.Any(x => x.ProductId2 == "owned"),+ "The owned selected product should be inserted.");+ Assert.IsFalse(mainProduct.RelatedProducts.Any(x => x.ProductId2 == "not-owned"),+ "A not-owned selected product must be silently dropped, not inserted.");+ _productServiceMock.Verify(p => p.InsertRelatedProduct(It.Is<RelatedProduct>(r => r.ProductId2 == "not-owned"), It.IsAny<string>()), Times.Never);+ }++ [TestMethod]+ public async Task InsertSimilarProductModel_DropsNotOwnedSelectedId()+ {+ var mainProduct = new Product { Id = "main" };+ var owned = new Product { Id = "owned" };+ var notOwned = new Product { Id = "not-owned" };+ _productServiceMock.Setup(p => p.GetProductById("main", It.IsAny<bool>())).ReturnsAsync(mainProduct);+ SetupOwnershipForInsertTests(owned, notOwned);++ var model = new ProductModel.AddSimilarProductModel {+ ProductId = "main",+ SelectedProductIds = ["owned", "not-owned"]+ };+ await _productViewModelService.InsertSimilarProductModel(model);++ Assert.IsTrue(mainProduct.SimilarProducts.Any(x => x.ProductId2 == "owned"),+ "The owned selected product should be inserted.");+ Assert.IsFalse(mainProduct.SimilarProducts.Any(x => x.ProductId2 == "not-owned"),+ "A not-owned selected product must be silently dropped, not inserted.");+ _productServiceMock.Verify(p => p.InsertSimilarProduct(It.Is<SimilarProduct>(r => r.ProductId2 == "not-owned")), Times.Never);+ }++ [TestMethod]+ public async Task InsertBundleProductModel_DropsNotOwnedSelectedId()+ {+ var mainProduct = new Product { Id = "main" };+ var owned = new Product { Id = "owned" };+ var notOwned = new Product { Id = "not-owned" };+ _productServiceMock.Setup(p => p.GetProductById("main", It.IsAny<bool>())).ReturnsAsync(mainProduct);+ SetupOwnershipForInsertTests(owned, notOwned);++ var model = new ProductModel.AddBundleProductModel {+ ProductId = "main",+ SelectedProductIds = ["owned", "not-owned"]+ };+ await _productViewModelService.InsertBundleProductModel(model);++ Assert.IsTrue(mainProduct.BundleProducts.Any(x => x.ProductId == "owned"),+ "The owned selected product should be inserted.");+ Assert.IsFalse(mainProduct.BundleProducts.Any(x => x.ProductId == "not-owned"),+ "A not-owned selected product must be silently dropped, not inserted.");+ _productServiceMock.Verify(p => p.InsertBundleProduct(It.Is<BundleProduct>(r => r.ProductId == "not-owned"), It.IsAny<string>()), Times.Never);+ }++ [TestMethod]+ public async Task InsertCrossSellProductModel_DropsNotOwnedSelectedId()+ {+ var mainProduct = new Product { Id = "main" };+ var owned = new Product { Id = "owned" };+ var notOwned = new Product { Id = "not-owned" };+ _productServiceMock.Setup(p => p.GetProductById("main", It.IsAny<bool>())).ReturnsAsync(mainProduct);+ SetupOwnershipForInsertTests(owned, notOwned);++ var model = new ProductModel.AddCrossSellProductModel {+ ProductId = "main",+ SelectedProductIds = ["owned", "not-owned"]+ };+ await _productViewModelService.InsertCrossSellProductModel(model);++ _productServiceMock.Verify(p => p.InsertCrossSellProduct(It.Is<CrossSellProduct>(r => r.ProductId2 == "owned")), Times.Once);+ _productServiceMock.Verify(p => p.InsertCrossSellProduct(It.Is<CrossSellProduct>(r => r.ProductId2 == "not-owned")), Times.Never,+ "A not-owned selected product must be silently dropped, not inserted.");+ }++ [TestMethod]+ public async Task InsertRecommendedProductModel_DropsNotOwnedSelectedId()+ {+ var mainProduct = new Product { Id = "main" };+ var owned = new Product { Id = "owned" };+ var notOwned = new Product { Id = "not-owned" };+ _productServiceMock.Setup(p => p.GetProductById("main", It.IsAny<bool>())).ReturnsAsync(mainProduct);+ SetupOwnershipForInsertTests(owned, notOwned);++ var model = new ProductModel.AddRecommendedProductModel {+ ProductId = "main",+ SelectedProductIds = ["owned", "not-owned"]+ };+ await _productViewModelService.InsertRecommendedProductModel(model);++ _productServiceMock.Verify(p => p.InsertRecommendedProduct("main", "owned"), Times.Once);+ _productServiceMock.Verify(p => p.InsertRecommendedProduct("main", "not-owned"), Times.Never,+ "A not-owned selected product must be silently dropped, not inserted.");+ }}diff --git a/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.cs b/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.csindex d072b167a..7006a2c8f 100644--- a/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.cs+++ b/src/Web/Grand.Web.AdminShared/Services/ProductViewModelService.cs@@ -1000,20 +1000,22 @@ public class ProductViewModelService(foreach (var id in model.SelectedProductIds){var product = await productService.GetProductById(id);- if (product != null)- {- var existingRelatedProducts = productId1.RelatedProducts;- if (model.ProductId != id)- if (!existingRelatedProducts.Any(x => x.ProductId2 == id))- {- var related = new RelatedProduct {- ProductId2 = id,- DisplayOrder = 1- };- productId1.RelatedProducts.Add(related);- await productService.InsertRelatedProduct(related, model.ProductId);- }- }+ // scope.HasAccess: same per-id ownership filter as BaseProductController's+ // AssociatedProductAddPopup(POST) selected-ids loop - without it a vendor could map another+ // vendor's products into their own related-products list (ARCH-001 Phase 1, Task 11).+ if (product == null || !await scope.HasAccess(product)) continue;++ var existingRelatedProducts = productId1.RelatedProducts;+ if (model.ProductId != id)+ if (!existingRelatedProducts.Any(x => x.ProductId2 == id))+ {+ var related = new RelatedProduct {+ ProductId2 = id,+ DisplayOrder = 1+ };+ productId1.RelatedProducts.Add(related);+ await productService.InsertRelatedProduct(related, model.ProductId);+ }}}@@ -1049,21 +1051,21 @@ public class ProductViewModelService(foreach (var id in model.SelectedProductIds){var product = await productService.GetProductById(id);- if (product != null)- {- var existingSimilarProducts = productId1.SimilarProducts;- if (model.ProductId != id)- if (!existingSimilarProducts.Any(x => x.ProductId2 == id))- {- var similar = new SimilarProduct {- ProductId1 = model.ProductId,- ProductId2 = id,- DisplayOrder = 1- };- productId1.SimilarProducts.Add(similar);- await productService.InsertSimilarProduct(similar);- }- }+ // scope.HasAccess: see InsertRelatedProductModel above.+ if (product == null || !await scope.HasAccess(product)) continue;++ var existingSimilarProducts = productId1.SimilarProducts;+ if (model.ProductId != id)+ if (!existingSimilarProducts.Any(x => x.ProductId2 == id))+ {+ var similar = new SimilarProduct {+ ProductId1 = model.ProductId,+ ProductId2 = id,+ DisplayOrder = 1+ };+ productId1.SimilarProducts.Add(similar);+ await productService.InsertSimilarProduct(similar);+ }}}@@ -1101,21 +1103,21 @@ public class ProductViewModelService(foreach (var id in model.SelectedProductIds){var product = await productService.GetProductById(id);- if (product != null)- {- var existingBundleProducts = productId1.BundleProducts;- if (model.ProductId != id)- if (!existingBundleProducts.Any(x => x.ProductId == id))- {- var bundle = new BundleProduct {- ProductId = id,- DisplayOrder = 1,- Quantity = 1- };- productId1.BundleProducts.Add(bundle);- await productService.InsertBundleProduct(bundle, model.ProductId);- }- }+ // scope.HasAccess: see InsertRelatedProductModel above.+ if (product == null || !await scope.HasAccess(product)) continue;++ var existingBundleProducts = productId1.BundleProducts;+ if (model.ProductId != id)+ if (!existingBundleProducts.Any(x => x.ProductId == id))+ {+ var bundle = new BundleProduct {+ ProductId = id,+ DisplayOrder = 1,+ Quantity = 1+ };+ productId1.BundleProducts.Add(bundle);+ await productService.InsertBundleProduct(bundle, model.ProductId);+ }}}@@ -1152,14 +1154,16 @@ public class ProductViewModelService(foreach (var id in model.SelectedProductIds){var product = await productService.GetProductById(id);- if (product != null)- if (!crossSellProduct.CrossSellProduct.Any(x => x == id))- if (model.ProductId != id)- await productService.InsertCrossSellProduct(- new CrossSellProduct {- ProductId1 = model.ProductId,- ProductId2 = id- });+ // scope.HasAccess: see InsertRelatedProductModel above.+ if (product == null || !await scope.HasAccess(product)) continue;++ if (!crossSellProduct.CrossSellProduct.Any(x => x == id))+ if (model.ProductId != id)+ await productService.InsertCrossSellProduct(+ new CrossSellProduct {+ ProductId1 = model.ProductId,+ ProductId2 = id+ });}}@@ -1178,10 +1182,12 @@ public class ProductViewModelService(foreach (var id in model.SelectedProductIds){var product = await productService.GetProductById(id);- if (product != null)- if (!mainproduct.RecommendedProduct.Any(x => x == id))- if (model.ProductId != id)- await productService.InsertRecommendedProduct(model.ProductId, id);+ // scope.HasAccess: see InsertRelatedProductModel above.+ if (product == null || !await scope.HasAccess(product)) continue;++ if (!mainproduct.RecommendedProduct.Any(x => x == id))+ if (model.ProductId != id)+ await productService.InsertRecommendedProduct(model.ProductId, id);}}@@ -2398,6 +2404,14 @@ public class ProductViewModelService(if (product.ProductPictures.Any(x => x.PictureId == picture.Id))return;+ // IsDefault: deliberately left unset here (defaults to false), matching Admin/Store's original+ // InsertProductPicture. Vendor's original copy set `IsDefault = product.ProductPictures.Any()` -+ // true only once a picture already exists, i.e. false on the very first upload and true on every+ // one after that. That reads as inverted (the first picture is normally the one that should+ // default to true) rather than as an intentional feature Admin/Store were missing, so it is not+ // being ported here. This is a documented behavior decision (ARCH-001 Phase 1, Task 11), not an+ // oversight - flag for product-team review if Vendor's admins report the default-picture picker+ // behaving differently than before.var productPicture = new ProductPicture {PictureId = picture.Id,DisplayOrder = displayOrder