From 9431ae7fdbbacc03af6299049c06de233fae5604 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Fri, 7 Jun 2013 01:39:53 -1000 Subject: [PATCH] Fixed issues of using StateHelper since we do things in async which don't have an HttpContext.Current, so we use our UmbracoContext.Current instead if required. Changed StateHelper to be obsolete as we need to replace all calls to it with proper extension methods. Updated the content model mappers to ensure we ToArray property objects so they don't keep re-processing the Linq statement when iterating. Content is now saving to the db. --- .../ServerInfoPropertyEditor.cs | 19 +++++++- src/Umbraco.Web/HttpRequestExtensions.cs | 45 +++++++++++++++++++ .../Models/Mapping/ContentModelMapper.cs | 8 ++-- src/Umbraco.Web/UmbracoContext.cs | 7 ++- src/umbraco.businesslogic/StateHelper.cs | 1 + 5 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs b/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs index 1b01676ac9..d03f09a08f 100644 --- a/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs +++ b/src/Umbraco.Web.UI/App_Plugins/MyPackage/PropertyEditors/ServerInfoPropertyEditor.cs @@ -1,7 +1,9 @@ using System; +using System.Threading; using System.Web; using System.Web.Mvc; using System.Web.Routing; +using Umbraco.Core; using Umbraco.Core.PropertyEditors; namespace Umbraco.Web.UI.App_Plugins.MyPackage.PropertyEditors @@ -9,6 +11,10 @@ namespace Umbraco.Web.UI.App_Plugins.MyPackage.PropertyEditors [PropertyEditor("AD056473-492B-47F8-9613-5A4936666C67", "Server Info")] public class ServerInfoPropertyEditor : PropertyEditor { + //cache the URL since these values get called numerous times. + private static string _viewPath; + private static ReaderWriterLockSlim _locker = new ReaderWriterLockSlim(); + protected override ValueEditor CreateValueEditor() { if (UmbracoContext.Current == null || UmbracoContext.Current.HttpContext == null) @@ -16,9 +22,18 @@ namespace Umbraco.Web.UI.App_Plugins.MyPackage.PropertyEditors throw new InvalidOperationException("This property editor only works in an umbraco web context"); } - var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData())); + using (var lck = new UpgradeableReadLock(_locker)) + { + if (_viewPath == null) + { + lck.UpgradeToWriteLock(); + var urlHelper = new UrlHelper(new RequestContext(UmbracoContext.Current.HttpContext, new RouteData())); + _viewPath = urlHelper.Action("ServerEnvironment", "ServerSidePropertyEditors", new { area = "MyPackage" }); + } + return new ValueEditor(_viewPath); + } - return new ValueEditor(urlHelper.Action("ServerEnvironment", "ServerSidePropertyEditors", new {area = "MyPackage"})); + } protected override PreValueEditor CreatePreValueEditor() diff --git a/src/Umbraco.Web/HttpRequestExtensions.cs b/src/Umbraco.Web/HttpRequestExtensions.cs index 238ac06523..12deeb5d5c 100644 --- a/src/Umbraco.Web/HttpRequestExtensions.cs +++ b/src/Umbraco.Web/HttpRequestExtensions.cs @@ -6,6 +6,51 @@ using Umbraco.Core; namespace Umbraco.Web { + /// + /// Extension methods used to check/set cookie values + /// + /// + /// This should 100% supercede the StateManager.Cookies + /// + internal static class HttpCookieExtensions + { + internal const string PreviewCookieName = "UMB_PREVIEW"; + + /// + /// Does a preview cookie exist ? + /// + /// + /// + public static bool HasPreviewCookie(this HttpRequestBase request) + { + return request.Cookies[PreviewCookieName] != null; + } + + /// + /// Does a cookie exist with the specified key ? + /// + /// + /// + /// + public static bool HasCookie(this HttpRequestBase request, string key) + { + return request.Cookies[key] != null; + } + + /// + /// Is there a cookie with the key supplied and does it have a value that is not empty + /// + /// + /// + /// + public static bool HasCookieValue(this HttpRequestBase request, string key) + { + return request.Cookies[key] != null + && request.Cookies[key].Value != null + && request.Cookies[key].Value.IsNullOrWhiteSpace() == false; + } + } + /// /// Extension methods for the HttpRequest and HttpRequestBase objects /// diff --git a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs index 8ee9512d63..7a29c0a973 100644 --- a/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/ContentModelMapper.cs @@ -40,7 +40,7 @@ namespace Umbraco.Web.Models.Mapping var tabs = content.PropertyGroups.Select(propertyGroup => { //get the properties for the current tab - var propertiesForTab = content.GetPropertiesForGroup(propertyGroup); + var propertiesForTab = content.GetPropertiesForGroup(propertyGroup).ToArray(); //convert the properties to ContentPropertyDisplay objects var displayProperties = propertiesForTab @@ -65,7 +65,7 @@ namespace Umbraco.Web.Models.Mapping Id = 0, Label = "Generic properties", Alias = "Generic properties", - Properties = orphanProperties.Select(ToContentPropertyDisplay) + Properties = orphanProperties.Select(ToContentPropertyDisplay).ToArray() }); var result = CreateContent(content, (display, originalContent) => @@ -132,8 +132,8 @@ namespace Umbraco.Web.Models.Mapping UpdateDate = content.UpdateDate, CreateDate = content.CreateDate }; - if (createProperties) - result.Properties = content.Properties.Select(p => CreateProperty(p, propertyCreatedCallback)); + if (createProperties) + result.Properties = content.Properties.Select(p => CreateProperty(p, propertyCreatedCallback)).ToArray(); if (contentCreatedCallback != null) contentCreatedCallback(result, content); return result; diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 471e7403ad..b5c3f1339f 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -17,6 +17,8 @@ using System.Xml; using umbraco.presentation.preview; using Examine.Providers; using Examine; +using IOHelper = Umbraco.Core.IO.IOHelper; +using SystemDirectories = Umbraco.Core.IO.SystemDirectories; namespace Umbraco.Web { @@ -345,9 +347,10 @@ namespace Umbraco.Web var currentUrl = request.Url.AbsolutePath; // zb-00004 #29956 : refactor cookies names & handling return - StateHelper.Cookies.Preview.HasValue // has preview cookie + //StateHelper.Cookies.Preview.HasValue // has preview cookie + HttpContext.Request.HasPreviewCookie() && UmbracoUser != null // has user - && !currentUrl.StartsWith(Core.IO.IOHelper.ResolveUrl(Core.IO.SystemDirectories.Umbraco)); // is not in admin UI + && currentUrl.StartsWith(IOHelper.ResolveUrl(SystemDirectories.Umbraco)) == false; // is not in admin UI } private HttpRequestBase GetRequestFromContext() diff --git a/src/umbraco.businesslogic/StateHelper.cs b/src/umbraco.businesslogic/StateHelper.cs index 387efd2ca3..5b037fb555 100644 --- a/src/umbraco.businesslogic/StateHelper.cs +++ b/src/umbraco.businesslogic/StateHelper.cs @@ -8,6 +8,7 @@ namespace umbraco.BusinessLogic /// /// The StateHelper class provides general helper methods for handling sessions, context, viewstate and cookies. /// + [Obsolete("DO NOT USE THIS ANYMORE! REPLACE ALL CALLS WITH NEW EXTENSION METHODS")] public class StateHelper {