Merge pull request #8852 from umbraco/netcore/bugfix/disable-url-tracking
NetCore: Fix for disabling/enabling url tracking
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 _))
|
||||
{
|
||||
|
||||
@@ -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<IContent> 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<IContent> 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<IContent> 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);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -111,23 +116,10 @@ namespace Umbraco.Web.BackOffice.Controllers
|
||||
_logger.Debug<RedirectUrlManagementController>(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.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user