Merge pull request #654 from umbraco/U4-274-disable-alttemplate

U4-274 - Adds config option to disable alternative templates
This commit is contained in:
Stephan
2015-03-17 14:32:08 +01:00
9 changed files with 84 additions and 53 deletions

View File

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

View File

@@ -16,6 +16,12 @@ namespace Umbraco.Core.Configuration.UmbracoSettings
get { return (bool) base["internalRedirectPreservesTemplate"]; } get { return (bool) base["internalRedirectPreservesTemplate"]; }
} }
[ConfigurationProperty("disableAlternativeTemplates", DefaultValue = "false")]
public bool DisableAlternativeTemplates
{
get { return (bool) base["disableAlternativeTemplates"]; }
}
[ConfigurationProperty("urlProviderMode", DefaultValue = "AutoLegacy")] [ConfigurationProperty("urlProviderMode", DefaultValue = "AutoLegacy")]
public string UrlProviderMode 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 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. 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) (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 <web.routing
trySkipIisCustomErrors="false" trySkipIisCustomErrors="false"
internalRedirectPreservesTemplate="false"> internalRedirectPreservesTemplate="false" disableAlternativeTemplates="false">
</web.routing> </web.routing>
</settings> </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 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. 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) (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 <web.routing
trySkipIisCustomErrors="false" trySkipIisCustomErrors="false"
internalRedirectPreservesTemplate="false"> internalRedirectPreservesTemplate="false" disableAlternativeTemplates="false">
</web.routing> </web.routing>
</settings> </settings>

View File

@@ -1,57 +1,58 @@
using Umbraco.Core.Logging; using Umbraco.Core.Logging;
using Umbraco.Core.Models; using Umbraco.Core.Models;
using Umbraco.Core; using Umbraco.Core;
using Umbraco.Core.Configuration;
namespace Umbraco.Web.Routing namespace Umbraco.Web.Routing
{ {
/// <summary> /// <summary>
/// Provides an implementation of <see cref="IContentFinder"/> that handles page nice urls and a template. /// Provides an implementation of <see cref="IContentFinder"/> that handles page nice urls and a template.
/// </summary> /// </summary>
/// <remarks> /// <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>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> /// <para>If successful, then the template of the document request is also assigned.</para>
/// </remarks> /// </remarks>
public class ContentFinderByNiceUrlAndTemplate : ContentFinderByNiceUrl public class ContentFinderByNiceUrlAndTemplate : ContentFinderByNiceUrl
{ {
/// <summary> /// <summary>
/// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>. /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>.
/// </summary> /// </summary>
/// <param name="docRequest">The <c>PublishedContentRequest</c>.</param> /// <param name="docRequest">The <c>PublishedContentRequest</c>.</param>
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns> /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
/// <remarks>If successful, also assigns the template.</remarks> /// <remarks>If successful, also assigns the template.</remarks>
public override bool TryFindContent(PublishedContentRequest docRequest) public override bool TryFindContent(PublishedContentRequest docRequest)
{ {
IPublishedContent node = null; IPublishedContent node = null;
string path = docRequest.Uri.GetAbsolutePathDecoded(); string path = docRequest.Uri.GetAbsolutePathDecoded();
if (docRequest.HasDomain) if (docRequest.HasDomain)
path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path); path = DomainHelper.PathRelativeToDomain(docRequest.DomainUri, path);
if (path != "/") // no template if "/" if (path != "/") // no template if "/"
{ {
var pos = path.LastIndexOf('/'); var pos = path.LastIndexOf('/');
var templateAlias = path.Substring(pos + 1); var templateAlias = path.Substring(pos + 1);
path = pos == 0 ? "/" : path.Substring(0, pos); path = pos == 0 ? "/" : path.Substring(0, pos);
var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias); var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);
if (template != null) 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; var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path;
node = FindContent(docRequest, route); node = FindContent(docRequest, route);
if (node != null) if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false && node != null)
docRequest.TemplateModel = template; docRequest.TemplateModel = template;
} }
else else
{ {
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias); LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("Not a valid template: \"{0}\"", () => templateAlias);
} }
} }
else else
{ {
LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("No template in path \"/\""); LogHelper.Debug<ContentFinderByNiceUrlAndTemplate>("No template in path \"/\"");
} }
return node != null; 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 // only if the published content is the initial once, else the alternate template
// does not apply // does not apply
// + optionnally, apply the alternate template on internal redirects // + optionnally, apply the alternate template on internal redirects
var useAltTemplate = _pcr.IsInitialPublishedContent var useAltTemplate = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false
|| (UmbracoConfig.For.UmbracoSettings().WebRouting.InternalRedirectPreservesTemplate && _pcr.IsInternalRedirectPublishedContent); && (_pcr.IsInitialPublishedContent
|| (UmbracoConfig.For.UmbracoSettings().WebRouting.InternalRedirectPreservesTemplate && _pcr.IsInternalRedirectPublishedContent));
string altTemplate = useAltTemplate string altTemplate = useAltTemplate
? _routingContext.UmbracoContext.HttpContext.Request[Constants.Conventions.Url.AltTemplate] ? _routingContext.UmbracoContext.HttpContext.Request[Constants.Conventions.Url.AltTemplate]
: null; : null;

View File

@@ -11,6 +11,7 @@ using Umbraco.Web.Mvc;
using Umbraco.Web.Routing; using Umbraco.Web.Routing;
using umbraco; using umbraco;
using umbraco.cms.businesslogic.language; using umbraco.cms.businesslogic.language;
using Umbraco.Core.Configuration;
namespace Umbraco.Web.Templates namespace Umbraco.Web.Templates
{ {
@@ -80,7 +81,7 @@ namespace Umbraco.Web.Templates
//set the doc that was found by id //set the doc that was found by id
contentRequest.PublishedContent = doc; contentRequest.PublishedContent = doc;
//set the template, either based on the AltTemplate found or the standard template of the 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(doc.TemplateId)
: ApplicationContext.Current.Services.FileService.GetTemplate(AltTemplate.Value); : ApplicationContext.Current.Services.FileService.GetTemplate(AltTemplate.Value);

View File

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

View File

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