diff --git a/src/Umbraco.Core/Configuration/IConfigManipulator.cs b/src/Umbraco.Core/Configuration/IConfigManipulator.cs index 1d9230be44..bc89b7bd1d 100644 --- a/src/Umbraco.Core/Configuration/IConfigManipulator.cs +++ b/src/Umbraco.Core/Configuration/IConfigManipulator.cs @@ -7,5 +7,6 @@ namespace Umbraco.Core.Configuration void RemoveConnectionString(); void SaveConnectionString(string connectionString, string providerName); void SaveConfigValue(string itemPath, object value); + void SaveDisableRedirectUrlTracking(bool disable); } } diff --git a/src/Umbraco.Core/Configuration/XmlConfigManipulator.cs b/src/Umbraco.Core/Configuration/XmlConfigManipulator.cs index 80d795bb48..dd7e4baa94 100644 --- a/src/Umbraco.Core/Configuration/XmlConfigManipulator.cs +++ b/src/Umbraco.Core/Configuration/XmlConfigManipulator.cs @@ -2,8 +2,8 @@ using System.Configuration; using System.IO; using System.Linq; +using System.Xml; using System.Xml.Linq; -using Umbraco.Composing; using Umbraco.Core.IO; using Umbraco.Core.Logging; @@ -111,6 +111,26 @@ namespace Umbraco.Core.Configuration throw new NotImplementedException(); } + public void SaveDisableRedirectUrlTracking(bool disable) + { + var fileName = _ioHelper.MapPath("~/config/umbracoSettings.config"); + + var umbracoConfig = new XmlDocument { PreserveWhitespace = true }; + umbracoConfig.Load(fileName); + + var action = disable ? "disable" : "enable"; + + if (File.Exists(fileName) == false) + throw new Exception($"Couldn't {action} URL Tracker, the umbracoSettings.config file does not exist."); + + if (!(umbracoConfig.SelectSingleNode("//web.routing") is XmlElement webRoutingElement)) + throw new Exception($"Couldn't {action} URL Tracker, the web.routing element was not found in umbracoSettings.config."); + + // note: this adds the attribute if it does not exist + webRoutingElement.SetAttribute("disableRedirectUrlTracking", disable.ToString().ToLowerInvariant()); + umbracoConfig.Save(fileName); + } + private static void AddOrUpdateAttribute(XElement element, string name, string value) { var attribute = element.Attribute(name); diff --git a/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs b/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs index 646df5124d..a7b1528dc2 100644 --- a/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs +++ b/src/Umbraco.Infrastructure/Configuration/JsonConfigManipulator.cs @@ -65,6 +65,40 @@ namespace Umbraco.Core.Configuration } + public void SaveDisableRedirectUrlTracking(bool disable) + { + var provider = GetJsonConfigurationProvider(); + + var json = GetJson(provider); + + var item = GetDisableRedirectUrlItem(disable.ToString().ToLowerInvariant()); + + json.Merge(item, new JsonMergeSettings()); + + SaveJson(provider, json); + } + + private JToken GetDisableRedirectUrlItem(string value) + { + JTokenWriter writer = new JTokenWriter(); + + writer.WriteStartObject(); + writer.WritePropertyName("Umbraco"); + writer.WriteStartObject(); + writer.WritePropertyName("CMS"); + writer.WriteStartObject(); + writer.WritePropertyName("WebRouting"); + writer.WriteStartObject(); + writer.WritePropertyName("DisableRedirectUrlTracking"); + writer.WriteValue(value); + writer.WriteEndObject(); + writer.WriteEndObject(); + writer.WriteEndObject(); + writer.WriteEndObject(); + + return writer.Token; + } + private JToken GetConnectionItem(string connectionString, string providerName) { JTokenWriter writer = new JTokenWriter(); @@ -135,7 +169,7 @@ namespace Umbraco.Core.Configuration { foreach (var provider in configurationRoot.Providers) { - if(provider is JsonConfigurationProvider jsonConfigurationProvider) + if (provider is JsonConfigurationProvider jsonConfigurationProvider) { if (requiredKey is null || provider.TryGet(requiredKey, out _)) { diff --git a/src/Umbraco.Infrastructure/Routing/RedirectTrackingComponent.cs b/src/Umbraco.Infrastructure/Routing/RedirectTrackingComponent.cs index 7fecba7c78..84f0ccdabd 100644 --- a/src/Umbraco.Infrastructure/Routing/RedirectTrackingComponent.cs +++ b/src/Umbraco.Infrastructure/Routing/RedirectTrackingComponent.cs @@ -40,9 +40,6 @@ namespace Umbraco.Web.Routing public void Initialize() { - // don't let the event handlers kick in if Redirect Tracking is turned off in the config - if (_webRoutingSettings.DisableRedirectUrlTracking) return; - ContentService.Publishing += ContentService_Publishing; ContentService.Published += ContentService_Published; ContentService.Moving += ContentService_Moving; @@ -66,6 +63,9 @@ namespace Umbraco.Web.Routing private void ContentService_Publishing(IContentService sender, PublishEventArgs args) { + // don't let the event handlers kick in if Redirect Tracking is turned off in the config + if (_webRoutingSettings.DisableRedirectUrlTracking) return; + var oldRoutes = GetOldRoutes(args.EventState); foreach (var entity in args.PublishedEntities) { @@ -75,12 +75,18 @@ namespace Umbraco.Web.Routing private void ContentService_Published(IContentService sender, ContentPublishedEventArgs args) { + // don't let the event handlers kick in if Redirect Tracking is turned off in the config + if (_webRoutingSettings.DisableRedirectUrlTracking) return; + var oldRoutes = GetOldRoutes(args.EventState); CreateRedirects(oldRoutes); } private void ContentService_Moving(IContentService sender, MoveEventArgs args) { + // don't let the event handlers kick in if Redirect Tracking is turned off in the config + if (_webRoutingSettings.DisableRedirectUrlTracking) return; + var oldRoutes = GetOldRoutes(args.EventState); foreach (var info in args.MoveInfoCollection) { @@ -90,6 +96,9 @@ namespace Umbraco.Web.Routing private void ContentService_Moved(IContentService sender, MoveEventArgs args) { + // don't let the event handlers kick in if Redirect Tracking is turned off in the config + if (_webRoutingSettings.DisableRedirectUrlTracking) return; + var oldRoutes = GetOldRoutes(args.EventState); CreateRedirects(oldRoutes); } diff --git a/src/Umbraco.Web.BackOffice/Controllers/RedirectUrlManagementController.cs b/src/Umbraco.Web.BackOffice/Controllers/RedirectUrlManagementController.cs index 5086919b83..16ab197930 100644 --- a/src/Umbraco.Web.BackOffice/Controllers/RedirectUrlManagementController.cs +++ b/src/Umbraco.Web.BackOffice/Controllers/RedirectUrlManagementController.cs @@ -12,6 +12,7 @@ using Umbraco.Core.Mapping; using Umbraco.Core.Services; using Umbraco.Web.Common.Attributes; using Umbraco.Web.Security; +using Umbraco.Core.Configuration; namespace Umbraco.Web.BackOffice.Controllers { @@ -24,13 +25,16 @@ namespace Umbraco.Web.BackOffice.Controllers private readonly IRedirectUrlService _redirectUrlService; private readonly UmbracoMapper _umbracoMapper; private readonly IHostingEnvironment _hostingEnvironment; + private readonly IConfigManipulator _configManipulator; - public RedirectUrlManagementController(ILogger logger, + public RedirectUrlManagementController( + ILogger logger, IWebRoutingSettings webRoutingSettings, IWebSecurity webSecurity, IRedirectUrlService redirectUrlService, UmbracoMapper umbracoMapper, - IHostingEnvironment hostingEnvironment) + IHostingEnvironment hostingEnvironment, + IConfigManipulator configManipulator) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _webRoutingSettings = webRoutingSettings ?? throw new ArgumentNullException(nameof(webRoutingSettings)); @@ -38,6 +42,7 @@ namespace Umbraco.Web.BackOffice.Controllers _redirectUrlService = redirectUrlService ?? throw new ArgumentNullException(nameof(redirectUrlService)); _umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper)); _hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment)); + _configManipulator = configManipulator ?? throw new ArgumentNullException(nameof(configManipulator)); } /// @@ -111,23 +116,10 @@ namespace Umbraco.Web.BackOffice.Controllers _logger.Debug(errorMessage); throw new SecurityException(errorMessage); } - var configFilePath =_hostingEnvironment.MapPathContentRoot("~/config/umbracoSettings.config"); var action = disable ? "disable" : "enable"; - if (System.IO.File.Exists(configFilePath) == false) - return BadRequest($"Couldn't {action} URL Tracker, the umbracoSettings.config file does not exist."); - - var umbracoConfig = new XmlDocument { PreserveWhitespace = true }; - umbracoConfig.Load(configFilePath); - - var webRoutingElement = umbracoConfig.SelectSingleNode("//web.routing") as XmlElement; - if (webRoutingElement == null) - return BadRequest($"Couldn't {action} URL Tracker, the web.routing element was not found in umbracoSettings.config."); - - // note: this adds the attribute if it does not exist - webRoutingElement.SetAttribute("disableRedirectUrlTracking", disable.ToString().ToLowerInvariant()); - umbracoConfig.Save(configFilePath); + _configManipulator.SaveDisableRedirectUrlTracking(disable); return Ok($"URL tracker is now {action}d."); }