diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/SecurityElement.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/SecurityElement.cs index 33a8a8584b..34642c8c90 100644 --- a/src/Umbraco.Core/Configuration/UmbracoSettings/SecurityElement.cs +++ b/src/Umbraco.Core/Configuration/UmbracoSettings/SecurityElement.cs @@ -36,7 +36,7 @@ namespace Umbraco.Core.Configuration.UmbracoSettings return new OptionalInnerTextConfigurationElement( (InnerTextConfigurationElement)this["authCookieName"], //set the default - "UMB_UCONTEXT"); + Constants.Web.AuthCookieName); } } diff --git a/src/Umbraco.Core/Constants-System.cs b/src/Umbraco.Core/Constants-System.cs index c4ce5b58f6..f22c41d922 100644 --- a/src/Umbraco.Core/Constants-System.cs +++ b/src/Umbraco.Core/Constants-System.cs @@ -23,5 +23,22 @@ public const int RecycleBinMedia = -21; } + + /// + /// Defines the identifiers for Umbraco system nodes. + /// + public static class Web + { + /// + /// The preview cookie name + /// + public const string PreviewCookieName = "UMB_PREVIEW"; + + /// + /// The auth cookie name + /// + public const string AuthCookieName = "UMB_UCONTEXT"; + + } } } \ No newline at end of file diff --git a/src/Umbraco.Tests/Configurations/UmbracoSettings/SecurityElementTests.cs b/src/Umbraco.Tests/Configurations/UmbracoSettings/SecurityElementTests.cs index 219721c767..8fbf4a1523 100644 --- a/src/Umbraco.Tests/Configurations/UmbracoSettings/SecurityElementTests.cs +++ b/src/Umbraco.Tests/Configurations/UmbracoSettings/SecurityElementTests.cs @@ -1,4 +1,5 @@ using NUnit.Framework; +using Umbraco.Core; namespace Umbraco.Tests.Configurations.UmbracoSettings { @@ -23,7 +24,7 @@ namespace Umbraco.Tests.Configurations.UmbracoSettings [Test] public void AuthCookieName() { - Assert.IsTrue(SettingsSection.Security.AuthCookieName == "UMB_UCONTEXT"); + Assert.IsTrue(SettingsSection.Security.AuthCookieName == Constants.Web.AuthCookieName); } } } \ No newline at end of file diff --git a/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs b/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs index f5d86f5a9d..c6ee114bb9 100644 --- a/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs +++ b/src/Umbraco.Tests/TestHelpers/FakeHttpContextFactory.cs @@ -6,6 +6,7 @@ using System.Security.Principal; using System.Web; using System.Web.Routing; using Moq; +using Umbraco.Core; namespace Umbraco.Tests.TestHelpers { @@ -58,7 +59,7 @@ namespace Umbraco.Tests.TestHelpers //Cookie collection var cookieCollection = new HttpCookieCollection(); - cookieCollection.Add(new HttpCookie("UMB_UCONTEXT", "FBA996E7-D6BE-489B-B199-2B0F3D2DD826")); + cookieCollection.Add(new HttpCookie(Constants.Web.AuthCookieName, "FBA996E7-D6BE-489B-B199-2B0F3D2DD826")); //Request var requestMock = new Mock(); diff --git a/src/Umbraco.Web.UI.Client/src/views/content/content.edit.controller.js b/src/Umbraco.Web.UI.Client/src/views/content/content.edit.controller.js index c5a3bba58b..fa7c7bcb5c 100644 --- a/src/Umbraco.Web.UI.Client/src/views/content/content.edit.controller.js +++ b/src/Umbraco.Web.UI.Client/src/views/content/content.edit.controller.js @@ -255,7 +255,8 @@ function ContentEditController($scope, $routeParams, $q, $timeout, $window, appS $scope.save().then(function(data){ $window.open('dialogs/preview.aspx?id='+data.id,'umbpreview'); }); - }else{ + } + else { $window.open('dialogs/preview.aspx?id='+content.id,'umbpreview'); } }; diff --git a/src/Umbraco.Web.UI/umbraco/dialogs/Preview.aspx b/src/Umbraco.Web.UI/umbraco/dialogs/Preview.aspx index c93b21f8c8..8ea3e989fc 100644 --- a/src/Umbraco.Web.UI/umbraco/dialogs/Preview.aspx +++ b/src/Umbraco.Web.UI/umbraco/dialogs/Preview.aspx @@ -1,4 +1,4 @@ -<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="../masterpages/umbracoDialog.Master" CodeBehind="Preview.aspx.cs" Inherits="umbraco.presentation.dialogs.Preview" %> +<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="../masterpages/umbracoDialog.Master" Inherits="umbraco.presentation.dialogs.Preview" %> <%@ Register TagPrefix="cc1" Namespace="umbraco.uicontrols" Assembly="controls" %> diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index 63961782a2..47c8d167d7 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -28,6 +28,8 @@ using umbraco; using Umbraco.Core.Models; using Umbraco.Core.Dynamics; using umbraco.BusinessLogic.Actions; +using umbraco.cms.businesslogic.web; +using umbraco.presentation.preview; using Constants = Umbraco.Core.Constants; namespace Umbraco.Web.Editors @@ -303,9 +305,29 @@ namespace Umbraco.Web.Editors break; } + UpdatePreviewContext(contentItem.PersistedContent.Id); + return display; } + /// + /// Checks if the user is currently in preview mode and if so will update the preview content for this item + /// + /// + private void UpdatePreviewContext(int contentId) + { + var previewId = Request.GetPreviewCookieValue(); + if (previewId.IsNullOrWhiteSpace()) return; + Guid id; + if (Guid.TryParse(previewId, out id)) + { + var d = new Document(contentId); + var pc = new PreviewContent(UmbracoUser, id, false); + pc.PrepareDocument(UmbracoUser, d, true); + pc.SavePreviewSet(); + } + } + /// /// Maps the dto property values to the persisted model /// diff --git a/src/Umbraco.Web/HttpCookieExtensions.cs b/src/Umbraco.Web/HttpCookieExtensions.cs index 4e49610ca6..8a17d440c4 100644 --- a/src/Umbraco.Web/HttpCookieExtensions.cs +++ b/src/Umbraco.Web/HttpCookieExtensions.cs @@ -1,4 +1,7 @@ -using System.Web; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Web; using Umbraco.Core; namespace Umbraco.Web @@ -11,7 +14,18 @@ namespace Umbraco.Web /// internal static class HttpCookieExtensions { - internal const string PreviewCookieName = "UMB_PREVIEW"; + public static string GetPreviewCookieValue(this HttpRequestMessage request) + { + var cookie = request.Headers.GetCookies(Constants.Web.PreviewCookieName).FirstOrDefault(); + if (cookie != null) + { + if (cookie[Constants.Web.PreviewCookieName] != null) + { + return cookie[Constants.Web.PreviewCookieName].Value; + } + } + return null; + } /// /// Does a preview cookie exist ? @@ -20,7 +34,7 @@ namespace Umbraco.Web /// public static bool HasPreviewCookie(this HttpRequestBase request) { - return request.Cookies[PreviewCookieName] != null; + return request.Cookies[Constants.Web.PreviewCookieName] != null; } /// diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/Preview.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/Preview.aspx.cs index 5e0b8772d4..e3586eee8f 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/Preview.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/dialogs/Preview.aspx.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Configuration; using System.Data; +using System.Globalization; using System.Linq; using System.Web; using System.Web.Security; @@ -10,6 +11,7 @@ using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; +using Umbraco.Web; using umbraco.cms.businesslogic.web; using umbraco.presentation.preview; using umbraco.BusinessLogic; @@ -25,14 +27,14 @@ namespace umbraco.presentation.dialogs protected void Page_Load(object sender, EventArgs e) { - var d = new Document(int.Parse(helper.Request("id"))); - var pc = new PreviewContent(base.getUser(), Guid.NewGuid(), false); - pc.PrepareDocument(base.getUser(), d, true); + var d = new Document(Request.GetItemAs("id")); + var pc = new PreviewContent(UmbracoUser, Guid.NewGuid(), false); + pc.PrepareDocument(UmbracoUser, d, true); pc.SavePreviewSet(); docLit.Text = d.Text; changeSetUrl.Text = pc.PreviewsetPath; pc.ActivatePreviewCookie(); - Response.Redirect("../../" + d.Id.ToString() + ".aspx", true); + Response.Redirect("../../" + d.Id.ToString(CultureInfo.InvariantCulture) + ".aspx", true); } } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs index f2e823a33d..74040695ef 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/preview/PreviewContent.cs @@ -23,6 +23,8 @@ using Umbraco.Core.IO; namespace umbraco.presentation.preview { + //TODO : Migrate this to a new API! + public class PreviewContent { // zb-00004 #29956 : refactor cookies names & handling diff --git a/src/umbraco.businesslogic/StateHelper.cs b/src/umbraco.businesslogic/StateHelper.cs index 5b037fb555..10e81dd614 100644 --- a/src/umbraco.businesslogic/StateHelper.cs +++ b/src/umbraco.businesslogic/StateHelper.cs @@ -2,6 +2,7 @@ using System; using System.Reflection; using System.Web; using System.Web.UI; +using Umbraco.Core; namespace umbraco.BusinessLogic { @@ -348,8 +349,8 @@ namespace umbraco.BusinessLogic * we currently reproduce this by configuring each cookie with a 30d expires, but does * that actually make sense? shouldn't some cookie have _no_ expires? */ - static readonly Cookie _preview = new Cookie("UMB_PREVIEW", 30d); // was "PreviewSet" - static readonly Cookie _userContext = new Cookie("UMB_UCONTEXT", 30d); // was "UserContext" + static readonly Cookie _preview = new Cookie(Constants.Web.PreviewCookieName, 30d); // was "PreviewSet" + static readonly Cookie _userContext = new Cookie(Constants.Web.AuthCookieName, 30d); // was "UserContext" static readonly Cookie _member = new Cookie("UMB_MEMBER", 30d); // was "umbracoMember" public static Cookie Preview { get { return _preview; } }