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
fix(admin): build plugin and discount rule urls from the path base, not the store host (#854)
Krzysztof Pajakcc91204161f2
diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/PaymentControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/PaymentControllerTests.csindex c934ec4ec..17b1740bb 100644--- a/src/Tests/Grand.Web.Admin.Tests/Controllers/PaymentControllerTests.cs+++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/PaymentControllerTests.cs@@ -51,17 +51,13 @@ public class PaymentControllerTests_translationServiceMock = new Mock<ITranslationService>();_translationServiceMock.Setup(t => t.GetResource(It.IsAny<string>())).Returns("resource");- var contextAccessorMock = new Mock<IContextAccessor>();- contextAccessorMock.Setup(c => c.WorkContext).Returns(new Mock<IWorkContext>().Object);-_controller = new PaymentController(_paymentServiceMock.Object,_settingServiceMock.Object,_countryServiceMock.Object,_shippingMethodServiceMock.Object,_translationServiceMock.Object,- new Mock<IServiceProvider>().Object,- contextAccessorMock.Object);+ new Mock<IServiceProvider>().Object);var adminStoreServiceMock = new Mock<IAdminStoreService>();adminStoreServiceMock.Setup(s => s.GetActiveStore()).ReturnsAsync(StoreId);diff --git a/src/Tests/Grand.Web.Admin.Tests/Extensions/PluginExtensionsTests.cs b/src/Tests/Grand.Web.Admin.Tests/Extensions/PluginExtensionsTests.csnew file mode 100644index 000000000..840a08f3f--- /dev/null+++ b/src/Tests/Grand.Web.Admin.Tests/Extensions/PluginExtensionsTests.cs@@ -0,0 +1,61 @@+using Grand.Infrastructure.Plugins;+using Grand.Web.AdminShared.Extensions;+using Microsoft.VisualStudio.TestTools.UnitTesting;+using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;++namespace Grand.Web.Admin.Tests.Extensions;++[TestClass]+public class PluginExtensionsTests+{+ private DirectoryInfo _pluginDirectory;++ [TestInitialize]+ public void Setup()+ {+ //<temp>/<guid>/Plugins/Payments.Test/Payments.Test.dll - the logo url is built from the last two folders+ var root = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));+ _pluginDirectory = Directory.CreateDirectory(Path.Combine(root, "Plugins", "Payments.Test"));+ }++ [TestCleanup]+ public void Cleanup()+ {+ _pluginDirectory.Parent!.Parent!.Delete(true);+ }++ private PluginInfo Plugin(params string[] files)+ {+ foreach (var file in files)+ File.WriteAllText(Path.Combine(_pluginDirectory.FullName, file), string.Empty);+ return new PluginInfo(new FileInfo(Path.Combine(_pluginDirectory.FullName, "Payments.Test.dll")), null, null);+ }++ /// <summary>+ /// No scheme or host: the host the store has configured can differ from the one the panel+ /// was opened on (another domain, a reverse proxy, http behind https).+ /// </summary>+ [TestMethod]+ public void GetLogoUrl_AtTheRoot_ReturnsAPathWithoutHost()+ {+ Assert.AreEqual("/Plugins/Payments.Test/logo.jpg", Plugin("logo.jpg").GetLogoUrl(""));+ }++ [TestMethod]+ public void GetLogoUrl_UnderAPathBase_PrefixesThePathBase()+ {+ Assert.AreEqual("/shop/Plugins/Payments.Test/logo.png", Plugin("logo.png").GetLogoUrl("/shop/"));+ }++ [TestMethod]+ public void GetLogoUrl_PrefersJpgOverPng()+ {+ Assert.AreEqual("/Plugins/Payments.Test/logo.jpg", Plugin("logo.png", "logo.jpg").GetLogoUrl(null));+ }++ [TestMethod]+ public void GetLogoUrl_WithoutALogo_ReturnsNull()+ {+ Assert.IsNull(Plugin().GetLogoUrl(""));+ }+}diff --git a/src/Web/Grand.Web.Admin/Controllers/PaymentController.cs b/src/Web/Grand.Web.Admin/Controllers/PaymentController.csindex 60669eaff..cc8b90cf7 100644--- a/src/Web/Grand.Web.Admin/Controllers/PaymentController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/PaymentController.cs@@ -6,7 +6,6 @@ using Grand.Business.Core.Interfaces.Common.Directory;using Grand.Business.Core.Interfaces.Common.Localization;using Grand.Domain.Permissions;using Grand.Domain.Payments;-using Grand.Infrastructure;using Grand.Infrastructure.Plugins;using Grand.Web.Admin.Extensions;using Grand.Web.AdminShared.Extensions.Mapping;@@ -16,7 +15,6 @@ using Grand.Web.AdminShared.Models.Shipping;using Grand.Web.Common.DataSource;using Grand.Web.Common.Security.Authorization;using Microsoft.AspNetCore.Mvc;-using Grand.Web.AdminShared.Extensions;namespace Grand.Web.Admin.Controllers;@@ -30,8 +28,7 @@ public class PaymentController : BaseAdminControllerICountryService countryService,IShippingMethodService shippingMethodService,ITranslationService translationService,- IServiceProvider serviceProvider,- IContextAccessor contextAccessor)+ IServiceProvider serviceProvider){_paymentService = paymentService;_settingService = settingService;@@ -39,7 +36,6 @@ public class PaymentController : BaseAdminController_shippingMethodService = shippingMethodService;_translationService = translationService;_serviceProvider = serviceProvider;- _contextAccessor = contextAccessor;}#endregion@@ -52,7 +48,6 @@ public class PaymentController : BaseAdminControllerprivate readonly IShippingMethodService _shippingMethodService;private readonly ITranslationService _translationService;private readonly IServiceProvider _serviceProvider;- private readonly IContextAccessor _contextAccessor;#endregion@@ -82,10 +77,7 @@ public class PaymentController : BaseAdminController{var plugin = pluginInfo.Instance<IPlugin>(_serviceProvider);if (plugin != null)- {tmp.ConfigurationUrl = plugin.ConfigurationUrl();- tmp.LogoUrl = pluginInfo.GetLogoUrl(_contextAccessor.StoreContext.CurrentHost.Url);- }}paymentMethodsModel.Add(tmp);diff --git a/src/Web/Grand.Web.Admin/Controllers/PluginController.cs b/src/Web/Grand.Web.Admin/Controllers/PluginController.csindex a2e9ad52f..8508ebb7f 100644--- a/src/Web/Grand.Web.Admin/Controllers/PluginController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/PluginController.cs@@ -38,7 +38,7 @@ public class PluginController({var pluginModel = PluginInfo.ToModel();//logo- pluginModel.LogoUrl = PluginInfo.GetLogoUrl(contextAccessor.StoreContext.CurrentHost.Url);+ pluginModel.LogoUrl = PluginInfo.GetLogoUrl(Request.PathBase);//configuration URLsif (PluginInfo.Installed)diff --git a/src/Web/Grand.Web.AdminShared/Extensions/PluginExtensions.cs b/src/Web/Grand.Web.AdminShared/Extensions/PluginExtensions.csindex 50fd0cb31..9952ff79a 100644--- a/src/Web/Grand.Web.AdminShared/Extensions/PluginExtensions.cs+++ b/src/Web/Grand.Web.AdminShared/Extensions/PluginExtensions.cs@@ -4,16 +4,23 @@ namespace Grand.Web.AdminShared.Extensions;public static class PluginExtensions{- public static string GetLogoUrl(this PluginInfo pluginDescriptor, string url)+ /// <summary>+ /// Get the URL of the plugin logo, relative to the application root+ /// </summary>+ /// <param name="pluginDescriptor">Plugin</param>+ /// <param name="pathBase">Request path base (empty when the application is hosted at the root)</param>+ /// <returns>URL such as /Plugins/Payments.CashOnDelivery/logo.jpg, or null when the plugin has no logo</returns>+ public static string GetLogoUrl(this PluginInfo pluginDescriptor, string pathBase){if (pluginDescriptor.OriginalAssemblyFile?.Directory == null) return null;- var storeLocation = url.TrimEnd('/');+ //no scheme or host: the store's configured url can differ from the one the panel was opened on+ var root = pathBase?.TrimEnd('/') ?? string.Empty;var pluginDirectory = pluginDescriptor.OriginalAssemblyFile.Directory;var logoPluginJpg = Path.Combine(pluginDirectory.FullName, "logo.jpg");if (File.Exists(logoPluginJpg))- return $"{storeLocation}/{pluginDirectory.Parent?.Name}/{pluginDirectory.Name}/logo.jpg";+ return $"{root}/{pluginDirectory.Parent?.Name}/{pluginDirectory.Name}/logo.jpg";var logoPluginPng = Path.Combine(pluginDirectory.FullName, "logo.png");- return File.Exists(logoPluginPng) ? $"{storeLocation}/{pluginDirectory.Parent?.Name}/{pluginDirectory.Name}/logo.png" : null;+ return File.Exists(logoPluginPng) ? $"{root}/{pluginDirectory.Parent?.Name}/{pluginDirectory.Name}/logo.png" : null;}}\ No newline at end of filediff --git a/src/Web/Grand.Web.AdminShared/Views/AdminShared/Discount/Partials/CreateOrUpdate.TabRequirements.cshtml b/src/Web/Grand.Web.AdminShared/Views/AdminShared/Discount/Partials/CreateOrUpdate.TabRequirements.cshtmlindex ffa46ee5c..551dcb31c 100644--- a/src/Web/Grand.Web.AdminShared/Views/AdminShared/Discount/Partials/CreateOrUpdate.TabRequirements.cshtml+++ b/src/Web/Grand.Web.AdminShared/Views/AdminShared/Discount/Partials/CreateOrUpdate.TabRequirements.cshtml@@ -53,11 +53,20 @@});+ //a rule returns its configuration url from the application root ("/DiscountRules.../Configure"),+ //so under a path base (the store in a virtual directory) it has to be prefixed here+ var pathBase = "@(Html.Raw(JavaScriptEncoder.Default.Encode(Context.Request.PathBase.Value ?? string.Empty)))";+ function resolveRuleUrl(url) {+ if (pathBase && url && url.charAt(0) === '/' && url.charAt(1) !== '/' && url.indexOf(pathBase + '/') !== 0)+ return pathBase + url;+ return url;+ }+//load requirement HTMLfunction loadRequirement(configureRuleUrl, placeholderName) {$.ajax({cache:false,- url: configureRuleUrl,+ url: resolveRuleUrl(configureRuleUrl),dataType: "html",success: function (result) {$(placeholderName).html(result);