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 'Export / Import' region into BaseProductController (ARCH-001 Phase 1)

KrzysztofPajak061181c63fef
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs
index 803ef43d3..d0a0637bc 100644
--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs
+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BaseProductControllerTests.cs
@@ -1,7 +1,9 @@
+using Grand.Business.Core.Dto;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Security;
+using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Business.Core.Interfaces.Storage;
using Grand.Domain;
using Grand.Domain.Catalog;
@@ -17,7 +19,9 @@ using Grand.Web.AdminShared.Models.Orders;
using Grand.Web.Common.Localization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
+using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
@@ -95,6 +99,22 @@ public class BaseProductControllerTests
_scopeMock.Object);
var httpContext = new DefaultHttpContext();
+ // Needed for actions whose catch (Exception) block calls Error(exc), which logs via
+ // HttpContext.RequestServices.GetRequiredService<ILoggerFactory>() (see Export / Import below).
+ // Once RequestServices is non-null, ControllerBase.Url's own
+ // HttpContext.RequestServices.GetRequiredService<IUrlHelperFactory>() call (used by every
+ // RedirectToAction(action, controller) - e.g. GoToSku above) stops being short-circuited by the
+ // null-conditional it uses when RequestServices itself is null, so IUrlHelperFactory must resolve
+ // too or those pre-existing tests start throwing.
+ var loggerFactoryMock = new Mock<ILoggerFactory>();
+ loggerFactoryMock.Setup(l => l.CreateLogger(It.IsAny<string>())).Returns(new Mock<ILogger>().Object);
+ var urlHelperFactoryMock = new Mock<IUrlHelperFactory>();
+ urlHelperFactoryMock.Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>()))
+ .Returns(new Mock<IUrlHelper>().Object);
+ var requestServicesMock = new Mock<IServiceProvider>();
+ requestServicesMock.Setup(s => s.GetService(typeof(ILoggerFactory))).Returns(loggerFactoryMock.Object);
+ requestServicesMock.Setup(s => s.GetService(typeof(IUrlHelperFactory))).Returns(urlHelperFactoryMock.Object);
+ httpContext.RequestServices = requestServicesMock.Object;
_controller.ControllerContext = new ControllerContext { HttpContext = httpContext };
_controller.TempData = new TempDataDictionary(httpContext, new Mock<ITempDataProvider>().Object);
}
@@ -2835,4 +2855,146 @@ public class BaseProductControllerTests
productReviewServiceMock.Verify(
s => s.GetAllProductReviews("", null, null, null, "", "", "p1", 0, int.MaxValue), Times.Once);
}
+
+ // --- Export / Import ---------------------------------------------------------------------------
+
+ [TestMethod]
+ public async Task ExportExcelAll_Success_ReturnsXlsxFile()
+ {
+ var model = new ProductListModel();
+ var products = new List<Product> { new() { Id = "p1" } };
+ // No scope filtering here: productViewModelService.PrepareProducts is host-specific and already
+ // returns only the caller's products (Vendor's implementation constrains by CurrentVendor.Id
+ // internally) - the controller trusts it, same as ProductList trusts PrepareProductsModel.
+ _productViewModelServiceMock.Setup(s => s.PrepareProducts(model)).ReturnsAsync(products);
+ var exportManagerMock = new Mock<IExportManager<Product>>();
+ exportManagerMock.Setup(e => e.Export(products)).ReturnsAsync([1, 2, 3]);
+
+ var result = await _controller.ExportExcelAll(model, exportManagerMock.Object);
+
+ var file = result as FileContentResult;
+ Assert.IsNotNull(file);
+ Assert.AreEqual("text/xls", file.ContentType);
+ Assert.AreEqual("products.xlsx", file.FileDownloadName);
+ CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, file.FileContents);
+ }
+
+ [TestMethod]
+ public async Task ExportExcelAll_ExportThrows_ReturnsRedirectToList()
+ {
+ var model = new ProductListModel();
+ _productViewModelServiceMock.Setup(s => s.PrepareProducts(model)).ReturnsAsync([]);
+ var exportManagerMock = new Mock<IExportManager<Product>>();
+ exportManagerMock.Setup(e => e.Export(It.IsAny<IEnumerable<Product>>()))
+ .ThrowsAsync(new Exception("boom"));
+
+ var result = await _controller.ExportExcelAll(model, exportManagerMock.Object);
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ }
+
+ [TestMethod]
+ public async Task ExportExcelSelected_NullSelectedIds_ExportsEmptyList()
+ {
+ var exportManagerMock = new Mock<IExportManager<Product>>();
+ exportManagerMock.Setup(e => e.Export(It.Is<IEnumerable<Product>>(p => !p.Any())))
+ .ReturnsAsync([9]);
+
+ var result = await _controller.ExportExcelSelected(null, exportManagerMock.Object);
+
+ var file = result as FileContentResult;
+ Assert.IsNotNull(file);
+ _productServiceMock.Verify(p => p.GetProductsByIds(It.IsAny<string[]>(), true), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task ExportExcelSelected_FiltersOutProductsScopeDenies()
+ {
+ // Mirrors Vendor's original explicit HasAccessToProduct re-check on selectedIds (caller-supplied,
+ // not derived from a scoped search, unlike ExportExcelAll) - applied unconditionally here so
+ // Admin (where the original had no check at all) gets the same protection.
+ var owned = new Product { Id = "owned" };
+ var foreign = new Product { Id = "foreign" };
+ _productServiceMock.Setup(p => p.GetProductsByIds(new[] { "owned", "foreign" }, true))
+ .ReturnsAsync([owned, foreign]);
+ _scopeMock.Setup(s => s.HasAccess(owned)).ReturnsAsync(true);
+ _scopeMock.Setup(s => s.HasAccess(foreign)).ReturnsAsync(false);
+ var exportManagerMock = new Mock<IExportManager<Product>>();
+ exportManagerMock
+ .Setup(e => e.Export(It.Is<IEnumerable<Product>>(p => p.Single() == owned)))
+ .ReturnsAsync([7]);
+
+ var result = await _controller.ExportExcelSelected("owned,foreign", exportManagerMock.Object);
+
+ var file = result as FileContentResult;
+ Assert.IsNotNull(file);
+ exportManagerMock.Verify(e => e.Export(It.Is<IEnumerable<Product>>(p => p.Single() == owned)), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task ImportExcel_EmptyFile_DoesNotImport_RedirectsToListWithError()
+ {
+ var fileMock = new Mock<IFormFile>();
+ fileMock.Setup(f => f.Length).Returns(0);
+ var importManagerMock = new Mock<IImportManager<ProductDto>>();
+
+ var result = await _controller.ImportExcel(fileMock.Object, importManagerMock.Object);
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ importManagerMock.Verify(i => i.Import(It.IsAny<Stream>()), Times.Never);
+ _translationServiceMock.Verify(t => t.GetResource("Admin.Common.UploadFile"), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task ImportExcel_NullFile_DoesNotImport_RedirectsToListWithError()
+ {
+ var importManagerMock = new Mock<IImportManager<ProductDto>>();
+
+ var result = await _controller.ImportExcel(null, importManagerMock.Object);
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ importManagerMock.Verify(i => i.Import(It.IsAny<Stream>()), Times.Never);
+ }
+
+ [TestMethod]
+ public async Task ImportExcel_ValidFile_ImportsAndRedirectsToListWithSuccess()
+ {
+ using var stream = new MemoryStream([1, 2, 3]);
+ var fileMock = new Mock<IFormFile>();
+ fileMock.Setup(f => f.Length).Returns(3);
+ fileMock.Setup(f => f.OpenReadStream()).Returns(stream);
+ var importManagerMock = new Mock<IImportManager<ProductDto>>();
+ importManagerMock.Setup(i => i.Import(stream)).Returns(Task.CompletedTask);
+
+ var result = await _controller.ImportExcel(fileMock.Object, importManagerMock.Object);
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ importManagerMock.Verify(i => i.Import(stream), Times.Once);
+ _translationServiceMock.Verify(t => t.GetResource("Admin.Catalog.Products.Imported"), Times.Once);
+ }
+
+ [TestMethod]
+ public async Task ImportExcel_ImportThrows_RedirectsToListWithError()
+ {
+ using var stream = new MemoryStream([1]);
+ var fileMock = new Mock<IFormFile>();
+ fileMock.Setup(f => f.Length).Returns(1);
+ fileMock.Setup(f => f.OpenReadStream()).Returns(stream);
+ var importManagerMock = new Mock<IImportManager<ProductDto>>();
+ importManagerMock.Setup(i => i.Import(stream)).ThrowsAsync(new Exception("bad file"));
+
+ var result = await _controller.ImportExcel(fileMock.Object, importManagerMock.Object);
+
+ var redirect = result as RedirectToActionResult;
+ Assert.IsNotNull(redirect);
+ Assert.AreEqual("List", redirect.ActionName);
+ }
}
diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs
index 54a723841..99da51892 100644
--- a/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs
+++ b/src/Web/Grand.Web.AdminShared/Controllers/BaseProductController.cs
@@ -1837,4 +1837,102 @@ public abstract class BaseProductController(
}
#endregion
+
+ #region Export / Import
+
+ [PermissionAuthorizeAction(PermissionActionName.Export)]
+ [HttpPost]
+ public async Task<IActionResult> ExportExcelAll(ProductListModel model,
+ [FromServices] IExportManager<Product> exportManager)
+ {
+ // No explicit scope filter needed here: productViewModelService is host-specific (Admin's
+ // implementation returns all products matching the search model; Vendor's PrepareProducts always
+ // constrains the SearchProducts call to WorkContext.CurrentVendor.Id regardless of what's in
+ // model), so scoping is already enforced inside the polymorphic call, same as ProductList above.
+ var products = await productViewModelService.PrepareProducts(model);
+ try
+ {
+ var bytes = await exportManager.Export(products);
+ return File(bytes, "text/xls", "products.xlsx");
+ }
+ catch (Exception exc)
+ {
+ Error(exc);
+ return RedirectToAction("List");
+ }
+ }
+
+ [PermissionAuthorizeAction(PermissionActionName.Export)]
+ [HttpPost]
+ public async Task<IActionResult> ExportExcelSelected(string selectedIds,
+ [FromServices] IExportManager<Product> exportManager)
+ {
+ var products = new List<Product>();
+ if (selectedIds != null)
+ {
+ var ids = selectedIds
+ .Split([','], StringSplitOptions.RemoveEmptyEntries)
+ .Select(x => x)
+ .ToArray();
+ products.AddRange(await productService.GetProductsByIds(ids, true));
+ }
+
+ // Unlike ExportExcelAll, selectedIds is caller-supplied and not derived from a scoped search -
+ // Vendor's original explicitly re-checked HasAccessToProduct per id for exactly this reason
+ // (a vendor could otherwise pass another vendor's product id and export it). Admin's original had
+ // no check at all (GlobalAdminDataScope.HasAccess is a no-op there), so applying the filter
+ // unconditionally closes that gap the same way as every other row in this task, without changing
+ // Admin's or Store's observable behavior.
+ var scoped = new List<Product>();
+ foreach (var product in products)
+ if (await scope.HasAccess(product))
+ scoped.Add(product);
+
+ var bytes = await exportManager.Export(scoped);
+ return File(bytes, "text/xls", "products.xlsx");
+ }
+
+ // Not virtual: Vendor's original ProductController has no ImportExcel action at all, and Vendor is
+ // never granted the "Products" permission's Import action (grep across src/Web/Grand.Web.Vendor found
+ // no PermissionActionName.Import usage anywhere) - vendors are deliberately not allowed to bulk-import
+ // products. [PermissionAuthorizeAction(PermissionActionName.Import)] below already 403s for any host
+ // whose role has no Import grant for Products, so this is safe to expose unconditionally on the base
+ // class; Vendor's (future) subclass simply never routes a view to it, same as any other
+ // permission-gated action already in this file.
+ //
+ // Concern (flagged, not fixed - out of scope for this row): ImportExcel only checks
+ // `importexcelfile.Length > 0` before handing the raw stream to IImportManager<ProductDto>.Import.
+ // There is no file-extension allowlist and no upper bound on Length before the stream is read into
+ // memory by the importer. Commit a153496a6 hardened exactly this shape of gap (memory DoS + extension
+ // bypass) for attribute file uploads; this action has the same shape and was not touched by that fix.
+ // This is pre-existing behavior being ported verbatim, not something introduced by this migration -
+ // worth a follow-up ticket, not a silent fix here.
+ [PermissionAuthorizeAction(PermissionActionName.Import)]
+ [HttpPost]
+ public async Task<IActionResult> ImportExcel(IFormFile importexcelfile,
+ [FromServices] IImportManager<ProductDto> importManager)
+ {
+ try
+ {
+ if (importexcelfile is { Length: > 0 })
+ {
+ await importManager.Import(importexcelfile.OpenReadStream());
+ }
+ else
+ {
+ Error(translationService.GetResource("Admin.Common.UploadFile"));
+ return RedirectToAction("List");
+ }
+
+ Success(translationService.GetResource("Admin.Catalog.Products.Imported"));
+ return RedirectToAction("List");
+ }
+ catch (Exception exc)
+ {
+ Error(exc);
+ return RedirectToAction("List");
+ }
+ }
+
+ #endregion
}