From afd91f8adbad23d2b39ca4cb7b177b5be12a9131 Mon Sep 17 00:00:00 2001 From: David Peck Date: Thu, 3 Nov 2016 16:25:45 +0000 Subject: [PATCH 1/4] Added ability to only allow templates what are allowed by the document type --- .../UmbracoSettings/IWebRoutingSection.cs | 2 + .../UmbracoSettings/WebRoutingElement.cs | 9 +- src/Umbraco.Core/Models/ContentType.cs | 22 ++ src/Umbraco.Core/Models/IContentType.cs | 14 + .../WebRoutingElementDefaultTests.cs | 6 + .../config/umbracoSettings.config | 362 +++++++++--------- .../ContentFinderByNiceUrlAndTemplate.cs | 33 +- .../Routing/ContentFinderByPageIdQuery.cs | 3 +- .../Routing/PublishedContentRequestEngine.cs | 86 +++-- src/Umbraco.Web/Templates/TemplateRenderer.cs | 30 +- .../umbraco.presentation/NotFoundHandlers.cs | 15 +- src/Umbraco.Web/umbraco.presentation/page.cs | 24 +- 12 files changed, 386 insertions(+), 220 deletions(-) diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs index 9eb6d02aa7..aedfac7aa5 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs @@ -8,6 +8,8 @@ bool DisableAlternativeTemplates { get; } + bool DisableNotPermittedAlternativeTemplates { get; } + bool DisableFindContentByIdPath { get; } bool DisableRedirectUrlTracking { get; } diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs index 82f5d46b28..35348b0338 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs @@ -1,4 +1,5 @@ -using System.Configuration; +using System; +using System.Configuration; namespace Umbraco.Core.Configuration.UmbracoSettings { @@ -21,6 +22,12 @@ namespace Umbraco.Core.Configuration.UmbracoSettings { get { return (bool) base["disableAlternativeTemplates"]; } } + + [ConfigurationProperty("disableNotPermittedAlternativeTemplates", DefaultValue = "false")] + public bool DisableNotPermittedAlternativeTemplates + { + get { return (bool)base["disableNotPermittedAlternativeTemplates"]; } + } [ConfigurationProperty("disableFindContentByIdPath", DefaultValue = "false")] public bool DisableFindContentByIdPath { diff --git a/src/Umbraco.Core/Models/ContentType.cs b/src/Umbraco.Core/Models/ContentType.cs index 88c498a147..d05fc74ac8 100644 --- a/src/Umbraco.Core/Models/ContentType.cs +++ b/src/Umbraco.Core/Models/ContentType.cs @@ -98,6 +98,28 @@ namespace Umbraco.Core.Models } } + /// + /// Determines if AllowedTemplates contains templateId + /// + /// The template id to check + /// True if AllowedTemplates contains the templateId else False + public bool IsAllowedTemplate(int templateId) + { + var allowedTemplates = AllowedContentTypes ?? new ContentTypeSort[0]; + return allowedTemplates.Any(t => t.Id.Value == templateId); + } + + /// + /// Determines if AllowedTemplates contains templateId + /// + /// The template alias to check + /// True if AllowedTemplates contains the templateAlias else False + public bool IsAllowedTemplate(string templateAlias) + { + var allowedTemplates = AllowedContentTypes ?? new ContentTypeSort[0]; + return allowedTemplates.Any(t => t.Alias.Equals(templateAlias, StringComparison.InvariantCultureIgnoreCase)); + } + /// /// Sets the default template for the ContentType /// diff --git a/src/Umbraco.Core/Models/IContentType.cs b/src/Umbraco.Core/Models/IContentType.cs index 766a8eec81..356a5acccd 100644 --- a/src/Umbraco.Core/Models/IContentType.cs +++ b/src/Umbraco.Core/Models/IContentType.cs @@ -17,6 +17,20 @@ namespace Umbraco.Core.Models /// IEnumerable AllowedTemplates { get; set; } + /// + /// Determines if AllowedTemplates contains templateId + /// + /// The template id to check + /// True if AllowedTemplates contains the templateId else False + bool IsAllowedTemplate(int templateId); + + /// + /// Determines if AllowedTemplates contains templateId + /// + /// The template alias to check + /// True if AllowedTemplates contains the templateAlias else False + bool IsAllowedTemplate(string templateAlias); + /// /// Sets the default template for the ContentType /// diff --git a/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs b/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs index 1e568c608e..eb27126e57 100644 --- a/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs +++ b/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs @@ -23,6 +23,12 @@ namespace Umbraco.Tests.Configurations.UmbracoSettings Assert.IsTrue(SettingsSection.WebRouting.DisableAlternativeTemplates == false); } + [Test] + public void DisableNotPermittedAlternativeTemplates() + { + Assert.IsTrue(SettingsSection.WebRouting.DisableNotPermittedAlternativeTemplates == false); + } + [Test] public void DisableFindContentByIdPath() { diff --git a/src/Umbraco.Web.UI/config/umbracoSettings.config b/src/Umbraco.Web.UI/config/umbracoSettings.config index 9eb3f135e9..0d0ad45d83 100644 --- a/src/Umbraco.Web.UI/config/umbracoSettings.config +++ b/src/Umbraco.Web.UI/config/umbracoSettings.config @@ -28,162 +28,163 @@ - True - - - + - - 1 - 1079 - 1080 - - - - - your@email.here - + + 1 + 1079 + 1080 + + + + + your@email.here + - - True + +True - - False + +False - - UTF8 + +UTF8 - - false + +false - - - true + + +true - - True + +True - - True + +True - - False + +False - - False + +False - - text + +text - - - In Preview Mode - click to end - ]]> + + + In Preview Mode - click to end + ]]> + - - - 1800 + + +1800 - - - - false + + + +false - - throw - - - ashx,aspx,ascx,config,cshtml,vbhtml,asmx,air,axd,swf,xml,html,htm,svg,php,htaccess +throw - - Textstring - - + +ashx,aspx,ascx,config,cshtml,vbhtml,asmx,air,axd,swf,xml,html,htm,svg,php,htaccess - - - true + +Textstring - - false - - - true - - + - - - false - - true - - - - - - - - - - - - - plus - star - - - ae - oe - aa - ae - oe - ue - ss - ae - oe - - - - - - + + +true - - - - true + +false - - - Mvc - + +true + + + + + +false + +true + + - + + + + + + + + + + plus + star + + + ae + oe + aa + ae + oe + ue + ss + ae + oe + - + + + + + + + + +true + + + +Mvc + - - - - cs - vb - - + + + + cs + vb + + - - - - - p - div - ul - span - - - + + p + div + ul + span + + + - - - + + + - - + + - - - true - true - - - - - - - + +true +true + + + + + + + - - - - + + + + - - - - - 0 + + + + +0 - - + - - - - - - - - - - - - - - - UsersMembershipProvider - - + + - - - - - + + + + + - + + UsersMembershipProvider + + + + + + + + + + - diff --git a/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs b/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs index 246b109dea..570f0d00c5 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs @@ -22,6 +22,8 @@ namespace Umbraco.Web.Routing /// If successful, also assigns the template. public override bool TryFindContent(PublishedContentRequest docRequest) { + const string tracePrefix = "ContentFinderByNiceUrlAndTemplate: "; + IPublishedContent node = null; string path = docRequest.Uri.GetAbsolutePathDecoded(); @@ -43,7 +45,36 @@ namespace Umbraco.Web.Routing node = FindContent(docRequest, route); if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false && node != null) - docRequest.TemplateModel = template; + { + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == true) + { + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(node.DocumentTypeId); + if (publishedContentContentType == null) + { + LogHelper.Warn("Content type ({1}) of published content({2}) can't be fetched ={1} alias=\"{2}\"", () => tracePrefix, () => node.Id, () => node.DocumentTypeId); + } + else + { + var isAllowedTemplate = publishedContentContentType.IsAllowedTemplate(template.Id); + if (isAllowedTemplate == false) + { + //If it is not allowed then default to as if no altTemplate had been supplied + LogHelper.Warn("{0}Content type '{1}' requested altTemplate of '{2}' but this is not an allowed template", () => tracePrefix, () => node.DocumentTypeAlias, () => template.Alias); + + docRequest.PublishedContent = null; + node = null; + } + else + { + docRequest.TemplateModel = template; + } + } + } + else + { + docRequest.TemplateModel = template; + } + } } else { diff --git a/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs b/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs index 4732895311..9ff3f25ae1 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs @@ -12,7 +12,8 @@ namespace Umbraco.Web.Routing public bool TryFindContent(PublishedContentRequest docRequest) { int pageId; - if (int.TryParse(docRequest.RoutingContext.UmbracoContext.HttpContext.Request["umbPageID"], out pageId)) + + if (int.TryParse(docRequest.RoutingContext.UmbracoContext.HttpContext.Request["umbPageID"], out pageId)) { var doc = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(pageId); diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index 03a50a4b99..3acabdcbec 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -20,6 +20,7 @@ using umbraco.cms.businesslogic.member; using Umbraco.Core.Services; using Umbraco.Web.Security; using RenderingEngine = Umbraco.Core.RenderingEngine; +using Umbraco.Core.Models; namespace Umbraco.Web.Routing { @@ -630,20 +631,11 @@ namespace Umbraco.Web.Routing var templateId = _pcr.PublishedContent.TemplateId; - if (templateId > 0) - { - ProfilingLogger.Logger.Debug("{0}Look for template id={1}", () => tracePrefix, () => templateId); - var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateId); - if (template == null) - throw new InvalidOperationException("The template with Id " + templateId + " does not exist, the page cannot render"); - _pcr.TemplateModel = template; - ProfilingLogger.Logger.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); - } - else - { - ProfilingLogger.Logger.Debug("{0}No specified template.", () => tracePrefix); - } - } + // This code was moved to GetTemplateModel to allow the same functionality on a failed altTemplate (U4-8550) + // The only change is a diffent logger prefix and null will be set to _pcr.TemplateModel if the templateId is <= 0 + // rather than no set taking place + _pcr.TemplateModel = GetTemplateModel(_pcr.PublishedContent.TemplateId); + } else { // we have an alternate template specified. lookup the template with that alias @@ -657,15 +649,33 @@ namespace Umbraco.Web.Routing ProfilingLogger.Logger.Debug("{0}Look for alternate template alias=\"{1}\"", () => tracePrefix, () => altTemplate); var template = ApplicationContext.Current.Services.FileService.GetTemplate(altTemplate); - if (template != null) - { - _pcr.TemplateModel = template; - ProfilingLogger.Logger.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); - } - else - { - ProfilingLogger.Logger.Debug("{0}The template with alias=\"{1}\" does not exist, ignoring.", () => tracePrefix, () => altTemplate); - } + if (template != null) + { + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates) + { + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(_pcr.PublishedContent.DocumentTypeId); + if (publishedContentContentType == null) + { + ProfilingLogger.Logger.Warn("{0}Content type ({1}) of published content({2}) can't be fetched ={1} alias=\"{2}\"", () => tracePrefix, () => _pcr.PublishedContent.Id, () => _pcr.PublishedContent.DocumentTypeId); + } + else + { + var isAllowedTemplate = publishedContentContentType.IsAllowedTemplate(template.Id); + if(isAllowedTemplate == false) + { + //If it is not allowed then default to as if no altTemplate had been supplied + ProfilingLogger.Logger.Warn("{0}Content type '{1}' requested altTemplate of '{2}' but this is not an allowed template", () => tracePrefix, () => _pcr.PublishedContent.DocumentTypeAlias, () => altTemplate); + template = GetTemplateModel(_pcr.PublishedContent.TemplateId); + } + } + } + _pcr.TemplateModel = template; + ProfilingLogger.Logger.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); + } + else + { + ProfilingLogger.Logger.Debug("{0}The template with alias=\"{1}\" does not exist, ignoring.", () => tracePrefix, () => altTemplate); + } } if (_pcr.HasTemplate == false) @@ -687,11 +697,31 @@ namespace Umbraco.Web.Routing } } - /// - /// Follows external redirection through umbracoRedirect document property. - /// - /// As per legacy, if the redirect does not work, we just ignore it. - private void FollowExternalRedirect() + private ITemplate GetTemplateModel(int templateId) + { + const string tracePrefix = "GetTemplateModel: "; + + if (templateId > 0) + { + ProfilingLogger.Logger.Debug("{0}Look for template id={1}", () => tracePrefix, () => templateId); + var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateId); + if (template == null) + throw new InvalidOperationException("The template with Id " + templateId + " does not exist, the page cannot render"); + ProfilingLogger.Logger.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); + return template; + } + else + { + ProfilingLogger.Logger.Debug("{0}No specified template.", () => tracePrefix); + } + return null; + } + + /// + /// Follows external redirection through umbracoRedirect document property. + /// + /// As per legacy, if the redirect does not work, we just ignore it. + private void FollowExternalRedirect() { if (_pcr.HasPublishedContent == false) return; diff --git a/src/Umbraco.Web/Templates/TemplateRenderer.cs b/src/Umbraco.Web/Templates/TemplateRenderer.cs index d7d331d887..a409e2da2b 100644 --- a/src/Umbraco.Web/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web/Templates/TemplateRenderer.cs @@ -80,10 +80,32 @@ 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 = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false - ? _umbracoContext.Application.Services.FileService.GetTemplate(doc.TemplateId) - : _umbracoContext.Application.Services.FileService.GetTemplate(AltTemplate.Value); + //set the template, either based on the AltTemplate found or the standard template of the doc + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false) + { + contentRequest.TemplateModel = _umbracoContext.Application.Services.FileService.GetTemplate(doc.TemplateId); + } + else + { + var template = _umbracoContext.Application.Services.FileService.GetTemplate(AltTemplate.Value); + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == false) + { + contentRequest.TemplateModel = template; + } + else + { + //Check if we're allowed to show the selected template + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(contentRequest.PublishedContent.DocumentTypeId); + if (publishedContentContentType != null) + { + var isAllowedTemplate = publishedContentContentType.IsAllowedTemplate(template.Id); + if (isAllowedTemplate == true) + { + contentRequest.TemplateModel = template; + } + } + } + } //if there is not template then exit if (!contentRequest.HasTemplate) diff --git a/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs b/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs index 27e62888d4..de8483b4d0 100644 --- a/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs +++ b/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs @@ -281,7 +281,20 @@ namespace umbraco { { _redirectID = int.Parse(urlNode.Attributes.GetNamedItem("id").Value); - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false) + var altTemplateAllowed = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false; + + //Query: Is it worth the overhead of the DB request here? The usage of AltTemplate is checked later. + if (altTemplateAllowed && UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == true) + { + var nodeTypeAlias = urlNode.Attributes.GetNamedItem("nodeTypeAlias").Value; + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(nodeTypeAlias); + if (publishedContentContentType != null) + { + altTemplateAllowed = publishedContentContentType.IsAllowedTemplate(templateAlias); + } + } + + if (altTemplateAllowed) { HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate] = templateAlias; HttpContext.Current.Trace.Write("umbraco.altTemplateHandler", diff --git a/src/Umbraco.Web/umbraco.presentation/page.cs b/src/Umbraco.Web/umbraco.presentation/page.cs index e71a290754..2172be88e5 100644 --- a/src/Umbraco.Web/umbraco.presentation/page.cs +++ b/src/Umbraco.Web/umbraco.presentation/page.cs @@ -152,9 +152,10 @@ namespace umbraco public page(XmlNode node) { populatePageData(node); - - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false) + + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false) { + var templateSet = false; // Check for alternative template if (HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate] != null && HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString() != String.Empty) @@ -162,16 +163,29 @@ namespace umbraco _template = umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias( HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString()); - _elements.Add("template", _template.ToString()); + templateSet = true; } 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()); + templateSet = true; } - } + + if (templateSet == true && UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == true) + { + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(_nodeTypeAlias); + if (publishedContentContentType != null && publishedContentContentType.IsAllowedTemplate(_template)) + { + _elements.Add("template", _template.ToString()); + } + } + else if(templateSet == true) + { + _elements.Add("template", _template.ToString()); + } + } if (_template == 0) { From 69728fb5c4ac0f7737914106de01cd684461519e Mon Sep 17 00:00:00 2001 From: David Peck Date: Tue, 8 Nov 2016 08:27:18 +0000 Subject: [PATCH 2/4] Changed Allowed Templates setting to be validate templates which checks url only --- .../UmbracoSettings/IWebRoutingSection.cs | 2 +- .../UmbracoSettings/WebRoutingElement.cs | 7 ++-- src/Umbraco.Core/Models/ContentType.cs | 6 +-- .../WebRoutingElementDefaultTests.cs | 4 +- .../config/umbracoSettings.config | 11 +++-- src/Umbraco.Web/PublishedContentExtensions.cs | 39 +++++++++++++++++ .../ContentFinderByNiceUrlAndTemplate.cs | 37 ++++------------ .../Routing/PublishedContentRequestEngine.cs | 36 +++++----------- src/Umbraco.Web/Templates/TemplateRenderer.cs | 40 ++++-------------- .../umbraco.presentation/NotFoundHandlers.cs | 15 +------ src/Umbraco.Web/umbraco.presentation/page.cs | 42 +++++++------------ 11 files changed, 98 insertions(+), 141 deletions(-) diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs index aedfac7aa5..c59d63d42e 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/IWebRoutingSection.cs @@ -8,7 +8,7 @@ bool DisableAlternativeTemplates { get; } - bool DisableNotPermittedAlternativeTemplates { get; } + bool ValidateAlternativeTemplates { get; } bool DisableFindContentByIdPath { get; } diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs index 35348b0338..ed133e5f61 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/WebRoutingElement.cs @@ -23,11 +23,12 @@ namespace Umbraco.Core.Configuration.UmbracoSettings get { return (bool) base["disableAlternativeTemplates"]; } } - [ConfigurationProperty("disableNotPermittedAlternativeTemplates", DefaultValue = "false")] - public bool DisableNotPermittedAlternativeTemplates + [ConfigurationProperty("validateAlternativeTemplates", DefaultValue = "false")] + public bool ValidateAlternativeTemplates { - get { return (bool)base["disableNotPermittedAlternativeTemplates"]; } + get { return (bool)base["validateAlternativeTemplates"]; } } + [ConfigurationProperty("disableFindContentByIdPath", DefaultValue = "false")] public bool DisableFindContentByIdPath { diff --git a/src/Umbraco.Core/Models/ContentType.cs b/src/Umbraco.Core/Models/ContentType.cs index d05fc74ac8..5849fcda04 100644 --- a/src/Umbraco.Core/Models/ContentType.cs +++ b/src/Umbraco.Core/Models/ContentType.cs @@ -105,8 +105,8 @@ namespace Umbraco.Core.Models /// True if AllowedTemplates contains the templateId else False public bool IsAllowedTemplate(int templateId) { - var allowedTemplates = AllowedContentTypes ?? new ContentTypeSort[0]; - return allowedTemplates.Any(t => t.Id.Value == templateId); + var allowedTemplates = AllowedTemplates ?? new ITemplate[0]; + return allowedTemplates.Any(t => t.Id == templateId); } /// @@ -116,7 +116,7 @@ namespace Umbraco.Core.Models /// True if AllowedTemplates contains the templateAlias else False public bool IsAllowedTemplate(string templateAlias) { - var allowedTemplates = AllowedContentTypes ?? new ContentTypeSort[0]; + var allowedTemplates = AllowedTemplates ?? new ITemplate[0]; return allowedTemplates.Any(t => t.Alias.Equals(templateAlias, StringComparison.InvariantCultureIgnoreCase)); } diff --git a/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs b/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs index eb27126e57..4c77fff5bc 100644 --- a/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs +++ b/src/Umbraco.Tests/Configurations/UmbracoSettings/WebRoutingElementDefaultTests.cs @@ -24,9 +24,9 @@ namespace Umbraco.Tests.Configurations.UmbracoSettings } [Test] - public void DisableNotPermittedAlternativeTemplates() + public void ValidateAlternativeTemplates() { - Assert.IsTrue(SettingsSection.WebRouting.DisableNotPermittedAlternativeTemplates == false); + Assert.IsTrue(SettingsSection.WebRouting.ValidateAlternativeTemplates == false); } [Test] diff --git a/src/Umbraco.Web.UI/config/umbracoSettings.config b/src/Umbraco.Web.UI/config/umbracoSettings.config index 0d0ad45d83..522da20c9c 100644 --- a/src/Umbraco.Web.UI/config/umbracoSettings.config +++ b/src/Umbraco.Web.UI/config/umbracoSettings.config @@ -302,9 +302,12 @@ 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 - @disableNotPermittedAlternativeTemplates - Similar to @disableAlternativeTemplates; setting this settings prevents alternative templates which have not been - permitted for the document type. Setting will not have any effect if disableNotPermittedAlternativeTemplates is set. + @validateAlternativeTemplates + 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 will ensure that + only templates that have been permitted on the document type will be allowed @disableFindContentByIdPath By default you can call any content Id in the url and show the content with that id, for example: http://mysite.com/1092 or http://mysite.com/1092.aspx would render the content with id 1092. Setting @@ -316,7 +319,7 @@ --> diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index 5caa08729a..189dda4934 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -12,6 +12,7 @@ using Umbraco.Web.Models; using Umbraco.Core; using Umbraco.Web.Routing; using ContentType = umbraco.cms.businesslogic.ContentType; +using Umbraco.Core.Configuration; namespace Umbraco.Web { @@ -114,6 +115,44 @@ namespace Umbraco.Web return template == null ? string.Empty : template.Alias; } + public static bool IsTemplateAllowed(this IPublishedContent content, int templateId) + { + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == true) + return content.TemplateId == templateId; + + if (content.TemplateId != templateId && UmbracoConfig.For.UmbracoSettings().WebRouting.ValidateAlternativeTemplates == true) + { + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(content.ContentType.Id); + if (publishedContentContentType == null) + throw new NullReferenceException("No content type returned for published content (contentType='" + content.ContentType.Id + "')"); + + return publishedContentContentType.IsAllowedTemplate(templateId); + } + + return true; + } + public static bool IsTemplateAllowed(this IPublishedContent content, string templateAlias) + { + var contentTemplateAlias = content.GetTemplateAlias(); + //I assume it is enough to compare aliases, as you can not have different templates with the same alias + var matchingAlias = contentTemplateAlias.Equals(templateAlias, StringComparison.InvariantCultureIgnoreCase); + + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == true) + return matchingAlias; + + if (matchingAlias == false && UmbracoConfig.For.UmbracoSettings().WebRouting.ValidateAlternativeTemplates == true) + { + var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(content.ContentType.Id); + if (publishedContentContentType == null) + throw new NullReferenceException("No content type returned for published content (contentType='" + content.ContentType.Id + "')"); + + //This doesn't appear to be working because new templates aren't in AllowedTemplates + return publishedContentContentType.IsAllowedTemplate(templateAlias); + } + + return true; + } + #endregion #region IsComposedOf diff --git a/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs b/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs index 570f0d00c5..50696f6867 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs @@ -44,36 +44,15 @@ namespace Umbraco.Web.Routing var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path; node = FindContent(docRequest, route); - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false && node != null) + if (node.IsTemplateAllowed(template.Id)) { - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == true) - { - var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(node.DocumentTypeId); - if (publishedContentContentType == null) - { - LogHelper.Warn("Content type ({1}) of published content({2}) can't be fetched ={1} alias=\"{2}\"", () => tracePrefix, () => node.Id, () => node.DocumentTypeId); - } - else - { - var isAllowedTemplate = publishedContentContentType.IsAllowedTemplate(template.Id); - if (isAllowedTemplate == false) - { - //If it is not allowed then default to as if no altTemplate had been supplied - LogHelper.Warn("{0}Content type '{1}' requested altTemplate of '{2}' but this is not an allowed template", () => tracePrefix, () => node.DocumentTypeAlias, () => template.Alias); - - docRequest.PublishedContent = null; - node = null; - } - else - { - docRequest.TemplateModel = template; - } - } - } - else - { - docRequest.TemplateModel = template; - } + docRequest.TemplateModel = template; + } + else + { + LogHelper.Warn("Configuration settings prevent template \"{0}\" from showing for node \"{1}\"", () => templateAlias, () => node.Id); + docRequest.PublishedContent = null; + node = null; } } else diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index 3acabdcbec..796ef9c1ce 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -607,9 +607,8 @@ 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 = _webRoutingSection.DisableAlternativeTemplates == false - && (_pcr.IsInitialPublishedContent - || (_webRoutingSection.InternalRedirectPreservesTemplate && _pcr.IsInternalRedirectPublishedContent)); + var useAltTemplate = _pcr.IsInitialPublishedContent + || (_webRoutingSection.InternalRedirectPreservesTemplate && _pcr.IsInternalRedirectPublishedContent); string altTemplate = useAltTemplate ? _routingContext.UmbracoContext.HttpContext.Request[Constants.Conventions.Url.AltTemplate] : null; @@ -648,33 +647,18 @@ namespace Umbraco.Web.Routing ProfilingLogger.Logger.Debug("{0}Has a template already, but also an alternate template.", () => tracePrefix); ProfilingLogger.Logger.Debug("{0}Look for alternate template alias=\"{1}\"", () => tracePrefix, () => altTemplate); - var template = ApplicationContext.Current.Services.FileService.GetTemplate(altTemplate); - if (template != null) + if (_pcr.PublishedContent.IsTemplateAllowed(altTemplate)) { - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates) - { - var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(_pcr.PublishedContent.DocumentTypeId); - if (publishedContentContentType == null) - { - ProfilingLogger.Logger.Warn("{0}Content type ({1}) of published content({2}) can't be fetched ={1} alias=\"{2}\"", () => tracePrefix, () => _pcr.PublishedContent.Id, () => _pcr.PublishedContent.DocumentTypeId); - } - else - { - var isAllowedTemplate = publishedContentContentType.IsAllowedTemplate(template.Id); - if(isAllowedTemplate == false) - { - //If it is not allowed then default to as if no altTemplate had been supplied - ProfilingLogger.Logger.Warn("{0}Content type '{1}' requested altTemplate of '{2}' but this is not an allowed template", () => tracePrefix, () => _pcr.PublishedContent.DocumentTypeAlias, () => altTemplate); - template = GetTemplateModel(_pcr.PublishedContent.TemplateId); - } - } - } - _pcr.TemplateModel = template; - ProfilingLogger.Logger.Debug("{0}Got template id={1} alias=\"{2}\"", () => tracePrefix, () => template.Id, () => template.Alias); + var template = ApplicationContext.Current.Services.FileService.GetTemplate(altTemplate); + if (template != null) + _pcr.TemplateModel = template; + else + ProfilingLogger.Logger.Debug("{0}The template with alias=\"{1}\" does not exist, ignoring.", () => tracePrefix, () => altTemplate); } else { - ProfilingLogger.Logger.Debug("{0}The template with alias=\"{1}\" does not exist, ignoring.", () => tracePrefix, () => altTemplate); + LogHelper.Warn("{0}Configuration settings prevent template \"{1}\" from showing for node \"{2}\"", () => tracePrefix, () => altTemplate, () => _pcr.PublishedContent.Id); + _pcr.TemplateModel = GetTemplateModel(_pcr.PublishedContent.TemplateId); } } diff --git a/src/Umbraco.Web/Templates/TemplateRenderer.cs b/src/Umbraco.Web/Templates/TemplateRenderer.cs index a409e2da2b..2eb00a9c75 100644 --- a/src/Umbraco.Web/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web/Templates/TemplateRenderer.cs @@ -77,38 +77,16 @@ namespace Umbraco.Web.Templates { contentRequest.Culture = _umbracoContext.PublishedContentRequest.Culture; } - - //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 - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false) - { - contentRequest.TemplateModel = _umbracoContext.Application.Services.FileService.GetTemplate(doc.TemplateId); - } - else - { - var template = _umbracoContext.Application.Services.FileService.GetTemplate(AltTemplate.Value); - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == false) - { - contentRequest.TemplateModel = template; - } - else - { - //Check if we're allowed to show the selected template - var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(contentRequest.PublishedContent.DocumentTypeId); - if (publishedContentContentType != null) - { - var isAllowedTemplate = publishedContentContentType.IsAllowedTemplate(template.Id); - if (isAllowedTemplate == true) - { - contentRequest.TemplateModel = template; - } - } - } - } - //if there is not template then exit - if (!contentRequest.HasTemplate) + //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 = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false + ? _umbracoContext.Application.Services.FileService.GetTemplate(doc.TemplateId) + : _umbracoContext.Application.Services.FileService.GetTemplate(AltTemplate.Value); + + //if there is not template then exit + if (!contentRequest.HasTemplate) { if (!AltTemplate.HasValue) { diff --git a/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs b/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs index de8483b4d0..27e62888d4 100644 --- a/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs +++ b/src/Umbraco.Web/umbraco.presentation/NotFoundHandlers.cs @@ -281,20 +281,7 @@ namespace umbraco { { _redirectID = int.Parse(urlNode.Attributes.GetNamedItem("id").Value); - var altTemplateAllowed = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false; - - //Query: Is it worth the overhead of the DB request here? The usage of AltTemplate is checked later. - if (altTemplateAllowed && UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == true) - { - var nodeTypeAlias = urlNode.Attributes.GetNamedItem("nodeTypeAlias").Value; - var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(nodeTypeAlias); - if (publishedContentContentType != null) - { - altTemplateAllowed = publishedContentContentType.IsAllowedTemplate(templateAlias); - } - } - - if (altTemplateAllowed) + if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false) { HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate] = templateAlias; HttpContext.Current.Trace.Write("umbraco.altTemplateHandler", diff --git a/src/Umbraco.Web/umbraco.presentation/page.cs b/src/Umbraco.Web/umbraco.presentation/page.cs index 2172be88e5..ed4254198e 100644 --- a/src/Umbraco.Web/umbraco.presentation/page.cs +++ b/src/Umbraco.Web/umbraco.presentation/page.cs @@ -152,42 +152,28 @@ namespace umbraco public page(XmlNode node) { populatePageData(node); - - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false) - { - var templateSet = 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()); - templateSet = true; - } - else if (helper.Request(Constants.Conventions.Url.AltTemplate) != String.Empty) - { - _template = - umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias( - helper.Request(Constants.Conventions.Url.AltTemplate).ToLower()); - templateSet = true; - } - if (templateSet == true && UmbracoConfig.For.UmbracoSettings().WebRouting.DisableNotPermittedAlternativeTemplates == true) + 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) { - var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(_nodeTypeAlias); - if (publishedContentContentType != null && publishedContentContentType.IsAllowedTemplate(_template)) - { - _elements.Add("template", _template.ToString()); - } + _template = + umbraco.cms.businesslogic.template.Template.GetTemplateIdFromAlias( + HttpContext.Current.Items[Constants.Conventions.Url.AltTemplate].ToString()); + _elements.Add("template", _template.ToString()); } - else if(templateSet == true) + 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 (_template == 0) { try { From a031551fb2ba734b2a40be2e42a1720617ad2fa0 Mon Sep 17 00:00:00 2001 From: David Peck Date: Tue, 8 Nov 2016 17:59:30 +0000 Subject: [PATCH 3/4] Rationalised changes in whitespace, to reduce the differences in the PR --- .../config/umbracoSettings.config | 357 +++++++++--------- src/Umbraco.Web/PublishedContentExtensions.cs | 4 +- .../Routing/ContentFinderByPageIdQuery.cs | 3 +- .../Routing/PublishedContentRequestEngine.cs | 2 +- src/Umbraco.Web/Templates/TemplateRenderer.cs | 14 +- src/Umbraco.Web/umbraco.presentation/page.cs | 38 +- 6 files changed, 208 insertions(+), 210 deletions(-) diff --git a/src/Umbraco.Web.UI/config/umbracoSettings.config b/src/Umbraco.Web.UI/config/umbracoSettings.config index 522da20c9c..3b037fa6f0 100644 --- a/src/Umbraco.Web.UI/config/umbracoSettings.config +++ b/src/Umbraco.Web.UI/config/umbracoSettings.config @@ -28,163 +28,162 @@ -True - - - + - - 1 - 1079 - 1080 - - - - - your@email.here - + + 1 + 1079 + 1080 + + + + + your@email.here + - -True + + True - -False + + False - -UTF8 + + UTF8 - -false + + false - - -true + + + true - -True + + True - -True + + True - -False + + False - -False + + False - -text + + text - - - In Preview Mode - click to end - ]]> - + + + In Preview Mode - click to end + ]]> - - -1800 + + + 1800 - - - -false + + + + false - -throw + throw + + + ashx,aspx,ascx,config,cshtml,vbhtml,asmx,air,axd,swf,xml,html,htm,svg,php,htaccess - -ashx,aspx,ascx,config,cshtml,vbhtml,asmx,air,axd,swf,xml,html,htm,svg,php,htaccess + + Textstring + + - -Textstring + + + true - + + false + + + true + + - - -true + + + false + + true + + - + + + + + + + + + + plus + star + + + ae + oe + aa + ae + oe + ue + ss + ae + oe + - + + + + - -false + + + + true - -true - - - - - -false - -true - - - - - - - - - - - - - plus - star - - - ae - oe - aa - ae - oe - ue - ss - ae - oe - - - - - - - - - - -true - - - -Mvc - + + + Mvc + - - - - cs - vb - - + + + + cs + vb + + - - - - - p - div - ul - span - - - + + p + div + ul + span + + + - - - + + + - - + + - - -true -true - - - - - - - + + true + true + + + + + + + - - - - + + + + - - - - -0 + + + + + 0 - - + - + + + + + + + + + - - + + + + + UsersMembershipProvider + + - - - - - + + + + + - - - - - UsersMembershipProvider - - - - - - - - - - - diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index 189dda4934..c08de157b9 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -5,14 +5,14 @@ using System.Globalization; using System.Linq; using System.Web; using Examine.LuceneEngine.SearchCriteria; +using Umbraco.Core; +using Umbraco.Core.Configuration; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Services; using Umbraco.Web.Models; -using Umbraco.Core; using Umbraco.Web.Routing; using ContentType = umbraco.cms.businesslogic.ContentType; -using Umbraco.Core.Configuration; namespace Umbraco.Web { diff --git a/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs b/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs index 9ff3f25ae1..4732895311 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs @@ -12,8 +12,7 @@ namespace Umbraco.Web.Routing public bool TryFindContent(PublishedContentRequest docRequest) { int pageId; - - if (int.TryParse(docRequest.RoutingContext.UmbracoContext.HttpContext.Request["umbPageID"], out pageId)) + if (int.TryParse(docRequest.RoutingContext.UmbracoContext.HttpContext.Request["umbPageID"], out pageId)) { var doc = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(pageId); diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index 796ef9c1ce..4f40af4b59 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -11,6 +11,7 @@ using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.IO; using Umbraco.Core.Logging; +using Umbraco.Core.Models; using Umbraco.Core.Security; using umbraco; @@ -20,7 +21,6 @@ using umbraco.cms.businesslogic.member; using Umbraco.Core.Services; using Umbraco.Web.Security; using RenderingEngine = Umbraco.Core.RenderingEngine; -using Umbraco.Core.Models; namespace Umbraco.Web.Routing { diff --git a/src/Umbraco.Web/Templates/TemplateRenderer.cs b/src/Umbraco.Web/Templates/TemplateRenderer.cs index 2eb00a9c75..d7d331d887 100644 --- a/src/Umbraco.Web/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web/Templates/TemplateRenderer.cs @@ -77,16 +77,16 @@ namespace Umbraco.Web.Templates { contentRequest.Culture = _umbracoContext.PublishedContentRequest.Culture; } - - //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 = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false + + //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 = UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates || AltTemplate.HasValue == false ? _umbracoContext.Application.Services.FileService.GetTemplate(doc.TemplateId) : _umbracoContext.Application.Services.FileService.GetTemplate(AltTemplate.Value); - //if there is not template then exit - if (!contentRequest.HasTemplate) + //if there is not template then exit + if (!contentRequest.HasTemplate) { if (!AltTemplate.HasValue) { diff --git a/src/Umbraco.Web/umbraco.presentation/page.cs b/src/Umbraco.Web/umbraco.presentation/page.cs index ed4254198e..e71a290754 100644 --- a/src/Umbraco.Web/umbraco.presentation/page.cs +++ b/src/Umbraco.Web/umbraco.presentation/page.cs @@ -153,27 +153,27 @@ namespace umbraco { populatePageData(node); - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == false) - { + 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 (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 (_template == 0) { try { From a2611ce35f7335e6d62ec64aa67c88c29fa38f60 Mon Sep 17 00:00:00 2001 From: David Peck Date: Tue, 30 May 2017 22:39:26 +0100 Subject: [PATCH 4/4] Changed the name of IsTemplateAllowed to IsAllowedTemplate to reflect the usage elsewhere and changed the overload of IsAllowedTemplate with the alias parameter to use the template id version to be more DRY --- src/Umbraco.Web/PublishedContentExtensions.cs | 26 +++++-------------- .../ContentFinderByNiceUrlAndTemplate.cs | 2 +- .../Routing/PublishedContentRequestEngine.cs | 2 +- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index c08de157b9..b75547fca0 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -115,7 +115,7 @@ namespace Umbraco.Web return template == null ? string.Empty : template.Alias; } - public static bool IsTemplateAllowed(this IPublishedContent content, int templateId) + public static bool IsAllowedTemplate(this IPublishedContent content, int templateId) { if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == true) return content.TemplateId == templateId; @@ -131,26 +131,14 @@ namespace Umbraco.Web return true; } - public static bool IsTemplateAllowed(this IPublishedContent content, string templateAlias) + public static bool IsAllowedTemplate(this IPublishedContent content, string templateAlias) { - var contentTemplateAlias = content.GetTemplateAlias(); - //I assume it is enough to compare aliases, as you can not have different templates with the same alias - var matchingAlias = contentTemplateAlias.Equals(templateAlias, StringComparison.InvariantCultureIgnoreCase); + var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias); + var isAllowedTemplate = (template != null) ? + content.IsAllowedTemplate(template.Id) : + false; - if (UmbracoConfig.For.UmbracoSettings().WebRouting.DisableAlternativeTemplates == true) - return matchingAlias; - - if (matchingAlias == false && UmbracoConfig.For.UmbracoSettings().WebRouting.ValidateAlternativeTemplates == true) - { - var publishedContentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(content.ContentType.Id); - if (publishedContentContentType == null) - throw new NullReferenceException("No content type returned for published content (contentType='" + content.ContentType.Id + "')"); - - //This doesn't appear to be working because new templates aren't in AllowedTemplates - return publishedContentContentType.IsAllowedTemplate(templateAlias); - } - - return true; + return isAllowedTemplate; } #endregion diff --git a/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs b/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs index 50696f6867..f19886c4f2 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByNiceUrlAndTemplate.cs @@ -44,7 +44,7 @@ namespace Umbraco.Web.Routing var route = docRequest.HasDomain ? (docRequest.Domain.RootNodeId.ToString() + path) : path; node = FindContent(docRequest, route); - if (node.IsTemplateAllowed(template.Id)) + if (node.IsAllowedTemplate(template.Id)) { docRequest.TemplateModel = template; } diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index 4f40af4b59..5ab62137ee 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -647,7 +647,7 @@ namespace Umbraco.Web.Routing ProfilingLogger.Logger.Debug("{0}Has a template already, but also an alternate template.", () => tracePrefix); ProfilingLogger.Logger.Debug("{0}Look for alternate template alias=\"{1}\"", () => tracePrefix, () => altTemplate); - if (_pcr.PublishedContent.IsTemplateAllowed(altTemplate)) + if (_pcr.PublishedContent.IsAllowedTemplate(altTemplate)) { var template = ApplicationContext.Current.Services.FileService.GetTemplate(altTemplate); if (template != null)