From aebbd6efe3a768dae0be9f69b9afd04dd587ed05 Mon Sep 17 00:00:00 2001 From: Stefan Kamphuis Date: Wed, 10 Jun 2020 10:14:12 +0200 Subject: [PATCH] Fix-2841 --- .../Entities/Urls/AdvancedUrlRewriter.cs | 58 ++++++++++++++++++- .../SqlDataProvider/09.06.02.SqlDataProvider | 40 +++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/DNN Platform/Library/Entities/Urls/AdvancedUrlRewriter.cs b/DNN Platform/Library/Entities/Urls/AdvancedUrlRewriter.cs index bb3bd4c8f9b..39e61e62846 100644 --- a/DNN Platform/Library/Entities/Urls/AdvancedUrlRewriter.cs +++ b/DNN Platform/Library/Entities/Urls/AdvancedUrlRewriter.cs @@ -346,6 +346,22 @@ private void ProcessRequest(HttpContext context, } } + // now we may know the TabId. If the current alias is not the same as the primary alias, + // we should check if the current alias is indeed a valid custom alias for the current tab. + if (result.TabId > 0 && result.HttpAlias != result.PrimaryAlias.HTTPAlias && !CheckIfAliasIsCurrentTabCustomTabAlias(ref result, settings)) + { + //it was an incorrect alias + //try and redirect the alias if the settings allow it + if( RedirectPortalAlias(result.PrimaryAlias.HTTPAlias, ref result, settings)) + { + //not correct alias for tab : will be redirected + //perform a 301 redirect if one has already been found + response.AppendHeader("X-Redirect-Reason", result.Reason.ToString().Replace("_", " ") + " Requested"); + response.RedirectPermanent(result.FinalUrl, false); + finished = true; + } + } + if (!finished && result.DoRewrite) { //check the identified portal alias details for any extra rewrite information required @@ -1813,6 +1829,39 @@ private static bool CheckIfAliasIsCustomTabAlias(ref UrlAction result, string ht return isACustomTabAlias; } /// + /// Checks to see whether the specified alias is a customTabAlias for the TabId in result + /// + /// + /// + /// + /// + private static bool CheckIfAliasIsCurrentTabCustomTabAlias(ref UrlAction result, FriendlyUrlSettings settings) + { + var customAliasesForTab = TabController.Instance.GetCustomAliases(result.TabId, result.PortalId); + bool isCurrentTabCustomTabAlias = false; + if (customAliasesForTab != null && customAliasesForTab.Count > 0) + { + //see if we have a customAlias for the current CultureCode + if (customAliasesForTab.ContainsKey(result.CultureCode)) + { + //if it is for the current culture, we need to know if it's a primary alias + var tabPortalAlias = PortalAliasController.Instance.GetPortalAlias(customAliasesForTab[result.CultureCode]); + if (tabPortalAlias != null && !tabPortalAlias.IsPrimary) + { + // it's not a primary alias, so must be a custom tab alias + isCurrentTabCustomTabAlias = true; + } + } + } + // if it's not a custom alias for the current tab, we'll need to change the result + if (!isCurrentTabCustomTabAlias) + { + result.Action = ActionType.Redirect301; + result.Reason = RedirectReason.Wrong_Portal_Alias; + } + return isCurrentTabCustomTabAlias; + } + /// /// Configures the result object to set the correct Alias redirect /// parameters and destination URL /// @@ -1838,8 +1887,15 @@ private static bool ConfigurePortalAliasRedirect(ref UrlAction result, if (ignoreCustomAliasTabs == false) //check out custom alias tabs collection { //if an alias is a custom tab alias for a specific tab, then don't redirect - if (CheckIfAliasIsCustomTabAlias(ref result, wrongAlias, settings)) + // if we have the TabId, we'll need to check if the alias is valid for the current tab + if (result.TabId > 0 && CheckIfAliasIsCurrentTabCustomTabAlias(ref result, settings)) + { doRedirect = false; + } + else if (result.TabId < 0 && CheckIfAliasIsCustomTabAlias(ref result, wrongAlias, settings)) + { + doRedirect = false; + } else { doRedirect = true; diff --git a/DNN Platform/Website/Providers/DataProviders/SqlDataProvider/09.06.02.SqlDataProvider b/DNN Platform/Website/Providers/DataProviders/SqlDataProvider/09.06.02.SqlDataProvider index 5aa987d112b..9c8a21391b4 100644 --- a/DNN Platform/Website/Providers/DataProviders/SqlDataProvider/09.06.02.SqlDataProvider +++ b/DNN Platform/Website/Providers/DataProviders/SqlDataProvider/09.06.02.SqlDataProvider @@ -40,3 +40,43 @@ CREATE NONCLUSTERED INDEX [IX_{objectQualifier}Users_PasswordResetToken] ON {dat [PasswordResetToken] ASC ) GO + +IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'{databaseOwner}{objectQualifier}GetTabCustomAliases') AND type in (N'P', N'PC')) +DROP PROCEDURE {databaseOwner}{objectQualifier}GetTabCustomAliases +GO + +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO + +CREATE PROCEDURE {databaseOwner}{objectQualifier}GetTabCustomAliases +( + @PortalID int +) +AS + SELECT + t.TabId, + Coalesce(trp.CultureCode, '') as CultureCode, + pa.HttpAlias + FROM {databaseOwner}{objectQualifier}Tabs t + INNER JOIN {databaseOwner}{objectQualifier}TabUrls trp ON trp.TabId = t.ParentId + INNER JOIN {databaseOwner}{objectQualifier}PortalAlias pa ON trp.PortalAliasId = pa.PortalAliasId + WHERE trp.PortalAliasUsage = 1 /* child tabs inherit */ + AND (@portalId = t.PortalId OR @portalId = -1) + AND NOT EXISTS (SELECT tr2.TabId + FROM {databaseOwner}{objectQualifier}TabUrls tr2 + WHERE tr2.TabId = t.TabId + AND tr2.CultureCode = trp.CultureCode + ) + UNION + SELECT + t.TabId, + Coalesce(trp.CultureCode, '') as CultureCode, + pa.HttpAlias + FROM {databaseOwner}{objectQualifier}Tabs t + INNER JOIN {databaseOwner}{objectQualifier}TabUrls trp ON trp.TabId = t.Tabid + INNER JOIN {databaseOwner}{objectQualifier}PortalAlias pa ON trp.PortalAliasId = pa.PortalAliasId + WHERE (@portalId = t.PortalId OR @portalId = -1) + AND t.CultureCode IS NULL OR t.CultureCode = trp.CultureCode +GO