Skip to content

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.

Migrate 'Bulk editing' region into BaseProductController (ARCH-001 Phase 1)

KrzysztofPajakb8ea13529ada
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs
index d0a0637bc..84af9fced 100644
--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs
@@ -2997,4 +2997,162 @@ public class BaseProductControllerTests
Assert.IsNotNull(redirect);
Assert.AreEqual("List", redirect.ActionName);
}
+
+ // --- Bulk editing --------------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task BulkEdit_UsesScopeDefaultStoreId()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store1");
+ _productViewModelServiceMock.Setup(s => s.PrepareBulkEditListModel("store1"))
+ .ReturnsAsync(new BulkEditListModel());
+
+ var result = await _controller.BulkEdit();
+
+ Assert.IsInstanceOfType(result, typeof(ViewResult));
+ _productViewModelServiceMock.Verify(s => s.PrepareBulkEditListModel("store1"), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task BulkEdit_NullDefaultStoreId_PassesEmptyString()
+ {
+ // Admin/Vendor: scope.DefaultStoreId is null (Admin is global; Vendor is not store-scoped) -
+ // matches Admin's original parameterless call and Vendor's own service's parameterless method.
+ _productViewModelServiceMock.Setup(s => s.PrepareBulkEditListModel(""))
+ .ReturnsAsync(new BulkEditListModel());
+
+ await _controller.BulkEdit();
+
+ _productViewModelServiceMock.Verify(s => s.PrepareBulkEditListModel(""), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task BulkEditSelect_StoreScoped_SetsSearchStoreIdFromScope()
+ {
+ _scopeMock.Setup(s => s.DefaultStoreId).Returns("store1");
+ var model = new BulkEditListModel();
+ _productViewModelServiceMock
+ .Setup(s => s.PrepareBulkEditProductModel(It.IsAny<BulkEditListModel>(), 1, 10))
+ .ReturnsAsync((Enumerable.Empty<BulkEditProductModel>(), 0));
+
+ await _controller.BulkEditSelect(new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);
+
+ Assert.AreEqual("store1", model.SearchStoreId);
+ }
+
+ [TestMethod]
+ public async Task BulkEditSelect_NotStoreScoped_LeavesSearchStoreIdUntouched()
+ {
+ var model = new BulkEditListModel { SearchStoreId = "preset" };
+ _productViewModelServiceMock
+ .Setup(s => s.PrepareBulkEditProductModel(It.IsAny<BulkEditListModel>(), 1, 10))
+ .ReturnsAsync((Enumerable.Empty<BulkEditProductModel>(), 0));
+
+ await _controller.BulkEditSelect(new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);
+
+ Assert.AreEqual("preset", model.SearchStoreId);
+ }
+
+ [TestMethod]
+ public async Task BulkEditUpdate_NullProducts_DoesNotCallService()
+ {
+ var result = await _controller.BulkEditUpdate(null);
+
+ Assert.IsInstanceOfType(result, typeof(JsonResult));
+ _productViewModelServiceMock.Verify(
+ s => s.UpdateBulkEdit(It.IsAny<IEnumerable<BulkEditProductModel>>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task BulkEditUpdate_FiltersOutProductsScopeDenies()
+ {
+ // Regression guard: Admin's original had no ownership check at all on this bulk-mutate endpoint
+ // (any client-supplied id list was updated unconditionally). Store's original filtered via
+ // FilterValidProductsForStore/CanAccessProduct; Vendor's original filtered via
+ // HasAccessToProduct inside its own service. Routing through scope.HasAccess here reproduces
+ // that per-item gate uniformly.
+ var owned = new Product { Id = "owned" };
+ var foreign = new Product { Id = "foreign" };
+ _productServiceMock.Setup(p => p.GetProductsByIds(new[] { "owned", "foreign" }, true))
+ .ReturnsAsync(new List<Product> { owned, foreign });
+ _scopeMock.Setup(s => s.HasAccess(owned)).ReturnsAsync(true);
+ _scopeMock.Setup(s => s.HasAccess(foreign)).ReturnsAsync(false);
+
+ var ownedModel = new BulkEditProductModel { Id = "owned" };
+ var foreignModel = new BulkEditProductModel { Id = "foreign" };
+
+ await _controller.BulkEditUpdate(new List<BulkEditProductModel> { ownedModel, foreignModel });
+
+ _productViewModelServiceMock.Verify(
+ s => s.UpdateBulkEdit(It.Is<List<BulkEditProductModel>>(
+ p => p.Count == 1 && p[0].Id == "owned")), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task BulkEditUpdate_AllProductsScopeDenies_DoesNotCallUpdateBulkEdit()
+ {
+ var foreign = new Product { Id = "foreign" };
+ _productServiceMock.Setup(p => p.GetProductsByIds(new[] { "foreign" }, true))
+ .ReturnsAsync(new List<Product> { foreign });
+ _scopeMock.Setup(s => s.HasAccess(foreign)).ReturnsAsync(false);
+
+ await _controller.BulkEditUpdate(new List<BulkEditProductModel> { new() { Id = "foreign" } });
+
+ _productViewModelServiceMock.Verify(
+ s => s.UpdateBulkEdit(It.IsAny<IEnumerable<BulkEditProductModel>>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task BulkEditDelete_NullProducts_DoesNotCallService()
+ {
+ var result = await _controller.BulkEditDelete(null);
+
+ Assert.IsInstanceOfType(result, typeof(JsonResult));
+ _productViewModelServiceMock.Verify(
+ s => s.DeleteBulkEdit(It.IsAny<IEnumerable<BulkEditProductModel>>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task BulkEditDelete_FiltersOutProductsScopeDenies()
+ {
+ var owned = new Product { Id = "owned" };
+ var foreign = new Product { Id = "foreign" };
+ _productServiceMock.Setup(p => p.GetProductsByIds(new[] { "owned", "foreign" }, true))
+ .ReturnsAsync(new List<Product> { owned, foreign });
+ _scopeMock.Setup(s => s.HasAccess(owned)).ReturnsAsync(true);
+ _scopeMock.Setup(s => s.HasAccess(foreign)).ReturnsAsync(false);
+
+ await _controller.BulkEditDelete(new List<BulkEditProductModel> {
+ new() { Id = "owned" }, new() { Id = "foreign" }
+ });
+
+ _productViewModelServiceMock.Verify(
+ s => s.DeleteBulkEdit(It.Is<List<BulkEditProductModel>>(
+ p => p.Count == 1 && p[0].Id == "owned")), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task BulkEditDelete_AllProductsScopeDenies_DoesNotCallDeleteBulkEdit()
+ {
+ var foreign = new Product { Id = "foreign" };
+ _productServiceMock.Setup(p => p.GetProductsByIds(new[] { "foreign" }, true))
+ .ReturnsAsync(new List<Product> { foreign });
+ _scopeMock.Setup(s => s.HasAccess(foreign)).ReturnsAsync(false);
+
+ await _controller.BulkEditDelete(new List<BulkEditProductModel> { new() { Id = "foreign" } });
+
+ _productViewModelServiceMock.Verify(
+ s => s.DeleteBulkEdit(It.IsAny<IEnumerable<BulkEditProductModel>>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task BulkEditUpdate_ProductsWithEmptyIds_AreIgnoredWithoutServiceCall()
+ {
+ var result = await _controller.BulkEditUpdate(new List<BulkEditProductModel> { new() { Id = "" } });
+
+ Assert.IsInstanceOfType(result, typeof(JsonResult));
+ _productServiceMock.Verify(p => p.GetProductsByIds(It.IsAny<string[]>(), It.IsAny<bool>()), Times.Never);
+ _productViewModelServiceMock.Verify(
+ s => s.UpdateBulkEdit(It.IsAny<IEnumerable<BulkEditProductModel>>()), Times.Never);
+ }
}
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs
index 99da51892..0b7d492be 100644
--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs
@@ -1935,4 +1935,113 @@ public abstract class BaseProductController(
}
#endregion
+
+ #region Bulk editing
+
+ [PermissionAuthorizeAction(PermissionActionName.Preview)]
+ public async Task<IActionResult> BulkEdit()
+ {
+ // scope.DefaultStoreId already encodes the per-host default: null for Admin/Vendor (Admin's
+ // original called PrepareBulkEditListModel() with no storeId; Vendor's own separate service
+ // (Grand.Web.Vendor.Interfaces.IProductViewModelService.PrepareBulkEditListModel) takes no
+ // storeId parameter at all - not store-scoped), StaffStoreId for Store.
+ var model = await productViewModelService.PrepareBulkEditListModel(scope.DefaultStoreId ?? "");
+ return View(model);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ public async Task<IActionResult> BulkEditSelect(DataSourceRequest command, BulkEditListModel model)
+ {
+ if (scope.DefaultStoreId is not null) model.SearchStoreId = scope.DefaultStoreId;
+
+ // Known gap, not fixed here (out of this region's file scope): Vendor's original
+ // PrepareBulkEditProductModel additionally passed vendorId: CurrentVendor.Id into
+ // productService.SearchProducts, so the grid only ever listed the vendor's own products. The
+ // shared IProductViewModelService.PrepareBulkEditProductModel used here has no vendorId parameter
+ // (unlike PrepareProductModel(AddProductModel), which supports SearchVendorId - see
+ // AssociatedProductVendorId above) - BulkEditListModel carries no SearchVendorId field either.
+ // Closing this requires a ProductViewModelService/BulkEditListModel change, which this task's
+ // per-row scope (BaseProductController.cs + tests only) does not permit. Not a security gap by
+ // itself (a wider listing, not a mutation), but it does mean a vendor could currently see other
+ // vendors' products in this grid once Vendor is subclassed onto this controller (Task 11) unless
+ // that follow-up also adds vendor filtering here. The mutate endpoints below (BulkEditUpdate/
+ // BulkEditDelete) are scope-checked per item regardless and never leak another party's product.
+ var (bulkEditProductModels, totalCount) =
+ await productViewModelService.PrepareBulkEditProductModel(model, command.Page, command.PageSize);
+ var gridModel = new DataSourceResult {
+ Data = bulkEditProductModels.ToList(),
+ Total = totalCount
+ };
+ return Json(gridModel);
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Edit)]
+ [HttpPost]
+ public async Task<IActionResult> BulkEditUpdate(IEnumerable<BulkEditProductModel> products)
+ {
+ var validProducts = await FilterBulkEditProductsByAccess(products);
+ if (validProducts.Count > 0) await productViewModelService.UpdateBulkEdit(validProducts);
+
+ return new JsonResult("");
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Delete)]
+ [HttpPost]
+ public async Task<IActionResult> BulkEditDelete(IEnumerable<BulkEditProductModel> products)
+ {
+ var validProducts = await FilterBulkEditProductsByAccess(products);
+ if (validProducts.Count > 0) await productViewModelService.DeleteBulkEdit(validProducts);
+
+ return new JsonResult("");
+ }
+
+ /// <summary>
+ /// Filters a caller-supplied bulk-edit product list (BulkEditUpdate/BulkEditDelete) down to the ones
+ /// the current user may mutate. Same shape as the DeleteSelected gap above: this is a caller-supplied
+ /// list of ids being mutated in one request, and each id must be scope-checked individually before
+ /// mutation, not just implicitly trusted because it appeared in a grid the user was shown.
+ ///
+ /// Admin's original had NO check at all here - both BulkEditUpdate and BulkEditDelete accepted a
+ /// client-supplied list of ids and mutated/deleted every one of them unconditionally, a real unscoped
+ /// bulk-mutate/bulk-delete IDOR (any authenticated admin user hitting these actions directly - not
+ /// through the grid - could update or delete any product in the system by id).
+ ///
+ /// Store's original had this exact check (FilterValidProductsForStore, via CanAccessProduct /
+ /// AccessToEntityByStore) - HasAccess (strict), matching CanAccessProduct's strict rule, not CanView.
+ ///
+ /// Vendor's original enforced the equivalent strict check (HasAccessToProduct / VendorId equality)
+ /// inside its own separate service (Grand.Web.Vendor's ProductViewModelService.UpdateBulkEdit/
+ /// DeleteBulkEdit), not in the controller - functionally the same per-item gate, just placed one layer
+ /// down. Routing it through scope.HasAccess here reproduces that gate at the controller layer, where
+ /// the shared IProductViewModelService.UpdateBulkEdit/DeleteBulkEdit used by this class does not
+ /// filter internally (verified: both loop and mutate/delete every product they're given, no ownership
+ /// check).
+ ///
+ /// No-op for Admin (GlobalAdminDataScope.HasAccess is always true, so validProducts == products).
+ /// Silently drops missing/inaccessible ids rather than throwing (matches Admin's/Vendor's
+ /// null-then-skip behavior, not Store's original throw-on-missing-id - the majority behavior, and a
+ /// single bad id in a bulk request shouldn't fail the whole batch).
+ /// </summary>
+ private async Task<List<BulkEditProductModel>> FilterBulkEditProductsByAccess(
+ IEnumerable<BulkEditProductModel> products)
+ {
+ if (products == null) return [];
+
+ var byId = products
+ .Where(x => !string.IsNullOrEmpty(x.Id))
+ .GroupBy(x => x.Id)
+ .ToDictionary(g => g.Key, g => g.First());
+ if (byId.Count == 0) return [];
+
+ var loadedProducts = await productService.GetProductsByIds(byId.Keys.ToArray(), true);
+ var validProducts = new List<BulkEditProductModel>();
+ foreach (var product in loadedProducts)
+ if (await scope.HasAccess(product))
+ validProducts.Add(byId[product.Id]);
+
+ return validProducts;
+ }
+
+ #endregion
}