U4-274 - Adds config option to disable alternative templates

This commit is contained in:
Sebastiaan Janssen
2015-03-17 13:50:33 +01:00
parent 97a085d12b
commit d24ef3650e
9 changed files with 84 additions and 53 deletions

View File

@@ -6,6 +6,8 @@
bool InternalRedirectPreservesTemplate { get; }
bool DisableAlternativeTemplates { get; }
string UrlProviderMode { get; }
}

View File

@@ -16,6 +16,12 @@ namespace Umbraco.Core.Configuration.UmbracoSettings
get { return (bool) base["internalRedirectPreservesTemplate"]; }
}
[ConfigurationProperty("disableAlternativeTemplates", DefaultValue = "false")]
public bool DisableAlternativeTemplates
{
get { return (bool) base["disableAlternativeTemplates"]; }
}
[ConfigurationProperty("urlProviderMode", DefaultValue = "AutoLegacy")]
public string UrlProviderMode
{

View File

@@ -132,10 +132,15 @@
finder or by the alt. template. Set this option to true to preserve the template set by the finder
or by the alt. template, in case of an internal redirect.
(false by default, and in fact should remain false unless you know what you're doing)
@disableAlternativeTemplates
By default you can add a altTemplate querystring or append a template name to the current URL which
will make Umbraco render the content on the current page with the template you requested, for example:
http://mysite.com/about-us/?altTemplate=Home and http://mysite.com/about-us/Home would render the
"About Us" page with a template with the alias Home. Setting this setting to true stops that behavior
-->
<web.routing
trySkipIisCustomErrors="false"
internalRedirectPreservesTemplate="false">
internalRedirectPreservesTemplate="false" disableAlternativeTemplates="false">
</web.routing>
</settings>

View File

@@ -290,10 +290,15 @@
finder or by the alt. template. Set this option to true to preserve the template set by the finder
or by the alt. template, in case of an internal redirect.
(false by default, and in fact should remain false unless you know what you're doing)
@disableAlternativeTemplates
By default you can add a altTemplate querystring or append a template name to the current URL which
will make Umbraco render the content on the current page with the template you requested, for example:
http://mysite.com/about-us/?altTemplate=Home and http://mysite.com/about-us/Home would render the
"About Us" page with a template with the alias Home. Setting this setting to true stops that behavior
-->
<web.routing
trySkipIisCustomErrors="false"
internalRedirectPreservesTemplate="false">
internalRedirectPreservesTemplate="false" disableAlternativeTemplates="false">
</web.routing>
</settings>

View File

@@ -1,57 +1,58 @@
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core;
using Umbraco.Core.Configuration;
namespace Umbraco.Web.Routing
{
/// <summary>
/// Provides an implementation of <see cref="IContentFinder"/> that handles page nice urls and a template.
/// </summary>
/// <remarks>
/// <para>Handles <c>/foo/bar/template</c> where <c>/foo/bar</c> is the nice url of a document, and <c>template</c> a template alias.</para>
/// <para>If successful, then the template of the document request is also assigned.</para>
/// </remarks>
/// <summary>
/// Provides an implementation of <see cref="IContentFinder"/> that handles page nice urls and a template.
/// </summary>
/// <remarks>
/// <para>Handles <c>/foo/bar/template</c> where <c>/foo/bar</c> is the nice url of a document, and <c>template</c> a template alias.</para>
/// <para>If successful, then the template of the document request is also assigned.</para>
/// </remarks>
public class ContentFinderByNiceUrlAndTemplate : ContentFinderByNiceUrl
{
/// <summary>
/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
/// </summary>
/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
/// <remarks>If successful, also assigns the template.</remarks>
public override bool TryFindContent(PublishedContentRequest docRequest)
/// <summary>
/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
/// </summary>
/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
/// <remarks>If successful, also assigns the template.</remarks>
public override bool TryFindContent(PublishedContentRequest docRequest)
{
IPublishedContent node = null;
string path = docRequest.Uri.GetAbsolutePathDecoded();
string path = docRequest.Uri.GetAbsolutePathDecoded();
if (docRequest.HasDomain)
path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path);
if (docRequest.HasDomain)
path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path);
if (path != "/") // no template if "/"
if (path != "/") // no template if "/"
{
var pos = path.LastIndexOf('/');
var templateAlias = path.Substring(pos + 1);
path = pos == 0 ? "/" : path.Substring(0, pos);
var pos = path.LastIndexOf('/');
var templateAlias = path.Substring(pos + 1);
path = pos == 0 ? "/" : path.Substring(0, pos);
var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);
if (template != null)
{
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("Valid template: \"{0}\"", () => templateAlias);
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("Valid template: \"{0}\"", () => templateAlias);
var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path;
node = FindContent(docRequest, route);
var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path;
node = FindContent(docRequest, route);
if (node != null)
docRequest.TemplateModel = template;
if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false && node != null)
docRequest.TemplateModel = template;
}
else
{
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias);
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias);
}
}
else
{
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("No template in path \"/\"");
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("No template in path \"/\"");
}
return node != null;

View File

@@ -586,8 +586,9 @@ namespace Umbraco.Web.Routing
// only if the published content is the initial once, else the alternate template
// does not apply
// + optionnally, apply the alternate template on internal redirects
var useAltTemplate = _pcr.IsInitialPublishedContent
|| (UmbracoConfig.For.UmbracoSettings().WebRouting.InternalRedirectPreservesTemplate && _pcr.IsInternalRedirectPublishedContent);
var useAltTemplate = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false
&& (_pcr.IsInitialPublishedContent
|| (UmbracoConfig.For.UmbracoSettings().WebRouting.InternalRedirectPreservesTemplate && _pcr.IsInternalRedirectPublishedContent));
string altTemplate = useAltTemplate
? _routingContext.UmbracoContext.HttpContext.Request[Constants.Conventions.Url.AltTemplate]
: null;

View File

@@ -11,6 +11,7 @@ using Umbraco.Web.Mvc;
using Umbraco.Web.Routing;
using umbraco;
using umbraco.cms.businesslogic.language;
using Umbraco.Core.Configuration;
namespace Umbraco.Web.Templates
{
@@ -80,7 +81,7 @@ namespace Umbraco.Web.Templates
//set the doc that was found by id
contentRequest.PublishedContent = doc;
//set the template, either based on the AltTemplate found or the standard template of the doc
contentRequest.TemplateModel = !AltTemplate.HasValue
contentRequest.TemplateModel = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false
? ApplicationContext.Current.Services.FileService.GetTemplate(doc.TemplateId)
: ApplicationContext.Current.Services.FileService.GetTemplate(AltTemplate.Value);

View File

@@ -13,6 +13,7 @@ using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
using Umbraco.Core.IO;
using umbraco.NodeFactory;
using Umbraco.Core;
namespace umbraco {
@@ -277,10 +278,13 @@ namespace umbraco {
{
_redirectID = int.Parse(urlNode.Attributes.GetNamedItem("id").Value);
HttpContext.Current.Items["altTemplate"] = templateAlias;
HttpContext.Current.Trace.Write("umbraco.altTemplateHandler",
string.Format("Templated changed to: '{0}'",
HttpContext.Current.Items["altTemplate"]));
if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false)
{
HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate] = templateAlias;
HttpContext.Current.Trace.Write("umbraco.altTemplateHandler",
string.Format("Template changed to: '{0}'", HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate]));
}
_succes = true;
}
}

View File

@@ -17,6 +17,7 @@ using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.template;
using umbraco.cms.businesslogic.web;
using umbraco.interfaces;
using Umbraco.Core.Configuration;
using Property = umbraco.cms.businesslogic.property.Property;
namespace umbraco
@@ -152,22 +153,27 @@ namespace umbraco
{
populatePageData(node);
// Check for alternative template
if (HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate] != null &&
HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString() != String.Empty)
{
_template =
umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias(
HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString());
_elements.Add("template", _template.ToString());
}
else if (helper.Request(Constants.Conventions.Url.AltTemplate) != String.Empty)
{
_template =
umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias(helper.Request(Constants.Conventions.Url.AltTemplate).ToLower());
_elements.Add("template", _template.ToString());
}
if (_template == 0)
if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false)
{
// Check for alternative template
if (HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate] != null &&
HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString() != String.Empty)
{
_template =
umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias(
HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString());
_elements.Add("template", _template.ToString());
}
else if (helper.Request(Constants.Conventions.Url.AltTemplate) != String.Empty)
{
_template =
umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias(
helper.Request(Constants.Conventions.Url.AltTemplate).ToLower());
_elements.Add("template", _template.ToString());
}
}
if (_template == 0)
{
try
{