diff --git a/src/Umbraco.Core/Models/Identity/BackOfficeIdentityUser.cs b/src/Umbraco.Core/Models/Identity/BackOfficeIdentityUser.cs index 75e7b1dc3d..302ccbcc54 100644 --- a/src/Umbraco.Core/Models/Identity/BackOfficeIdentityUser.cs +++ b/src/Umbraco.Core/Models/Identity/BackOfficeIdentityUser.cs @@ -144,7 +144,7 @@ namespace Umbraco.Core.Models.Identity public override DateTime? LastPasswordChangeDateUtc { get { return _lastPasswordChangeDateUtc; } - set { _tracker.SetPropertyValueAndDetectChanges(value, ref _lastPasswordChangeDateUtc, Ps.Value.LastPasswordChangeDateUtcSelector); } + set { _beingDirty.SetPropertyValueAndDetectChanges(value, ref _lastPasswordChangeDateUtc, Ps.Value.LastPasswordChangeDateUtcSelector); } } /// diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 17115c50b1..d77b2080ee 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -359,12 +359,5 @@ namespace Umbraco.Core.Services /// The content of the partial view. string GetPartialViewSnippetContent(string snippetName); - /// - /// Gets the content of a view. - /// - /// The name of the view. - /// - string GetViewContent(string filename); - } } diff --git a/src/Umbraco.Core/Services/Implement/FileService.cs b/src/Umbraco.Core/Services/Implement/FileService.cs index 09bd096f8e..4b95a0c1c3 100644 --- a/src/Umbraco.Core/Services/Implement/FileService.cs +++ b/src/Umbraco.Core/Services/Implement/FileService.cs @@ -332,16 +332,23 @@ namespace Umbraco.Core.Services.Implement var evtMsgs = EventMessagesFactory.Get(); - //NOTE: This isn't pretty but we need to maintain backwards compatibility so we cannot change + //fixme: This isn't pretty because we we're required to maintain backwards compatibility so we could not change // the event args here. The other option is to create a different event with different event - // args specifically for this method... which also isn't pretty. So for now, we'll use this - // dictionary approach to store 'additional data' in. + // args specifically for this method... which also isn't pretty. So fix this in v8! var additionalData = new Dictionary { { "CreateTemplateForContentType", true }, { "ContentTypeAlias", contentTypeAlias }, }; + // check that the template hasn't been created on disk before creating the content type + // if it exists, set the new template content to the existing file content + string content = GetViewContent(contentTypeAlias); + if (content.IsNullOrWhiteSpace() == false) + { + template.Content = content; + } + using (var scope = ScopeProvider.CreateScope()) { var saveEventArgs = new SaveEventArgs(template, true, evtMsgs, additionalData); @@ -368,6 +375,15 @@ namespace Umbraco.Core.Services.Implement { Content = content }; + + // check that the template hasn't been created on disk before creating the content type + // if it exists, set the new template content to the existing file content + string existingContent = GetViewContent(template.Alias); + if (existingContent.IsNullOrWhiteSpace() == false) + { + template.Content = content; + } + if (masterTemplate != null) { template.SetMasterTemplate(masterTemplate); @@ -659,10 +675,26 @@ namespace Umbraco.Core.Services.Implement return _templateRepository.GetFileSize(filepath); } } + + private string GetViewContent(string fileName) + { + if (fileName.IsNullOrWhiteSpace()) + throw new ArgumentNullException(nameof(fileName)); - #endregion + if (!fileName.EndsWith(".cshtml")) + fileName = string.Concat(fileName, ".cshtml"); - #region Partial Views + var fs = _templateRepository.GetFileContentStream(fileName); + if (fs == null) return string.Empty; + using (var view = new StreamReader(fs)) + { + return view.ReadToEnd().Trim(); + } + } + +#endregion + +#region Partial Views public IEnumerable GetPartialViewSnippetNames(params string[] filterNames) { diff --git a/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs b/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs index 54ae914f51..182f1a98f5 100644 --- a/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs +++ b/src/Umbraco.Tests/FrontEnd/UmbracoHelperTests.cs @@ -404,225 +404,6 @@ namespace Umbraco.Tests.FrontEnd Assert.That(result, Is.Null); } - // ------- Int32 conversion tests - [Test] - public static void Converting_Boxed_34_To_An_Int_Returns_34() - { - // Arrange - const int sample = 34; - - // Act - bool success = UmbracoHelper.ConvertIdObjectToInt( - sample, - out int result - ); - - // Assert - Assert.IsTrue(success); - Assert.That(result, Is.EqualTo(34)); - } - - [Test] - public static void Converting_String_54_To_An_Int_Returns_54() - { - // Arrange - const string sample = "54"; - - // Act - bool success = UmbracoHelper.ConvertIdObjectToInt( - sample, - out int result - ); - - // Assert - Assert.IsTrue(success); - Assert.That(result, Is.EqualTo(54)); - } - - [Test] - public static void Converting_Hello_To_An_Int_Returns_False() - { - // Arrange - const string sample = "Hello"; - - // Act - bool success = UmbracoHelper.ConvertIdObjectToInt( - sample, - out int result - ); - - // Assert - Assert.IsFalse(success); - Assert.That(result, Is.EqualTo(0)); - } - - [Test] - public static void Converting_Unsupported_Object_To_An_Int_Returns_False() - { - // Arrange - var clearlyWillNotConvertToInt = new StringBuilder(0); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToInt( - clearlyWillNotConvertToInt, - out int result - ); - - // Assert - Assert.IsFalse(success); - Assert.That(result, Is.EqualTo(0)); - } - - // ------- GUID conversion tests - [Test] - public static void Converting_Boxed_Guid_To_A_Guid_Returns_Original_Guid_Value() - { - // Arrange - Guid sample = Guid.NewGuid(); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToGuid( - sample, - out Guid result - ); - - // Assert - Assert.IsTrue(success); - Assert.That(result, Is.EqualTo(sample)); - } - - [Test] - public static void Converting_String_Guid_To_A_Guid_Returns_Original_Guid_Value() - { - // Arrange - Guid sample = Guid.NewGuid(); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToGuid( - sample.ToString(), - out Guid result - ); - - // Assert - Assert.IsTrue(success); - Assert.That(result, Is.EqualTo(sample)); - } - - [Test] - public static void Converting_Hello_To_A_Guid_Returns_False() - { - // Arrange - const string sample = "Hello"; - - // Act - bool success = UmbracoHelper.ConvertIdObjectToGuid( - sample, - out Guid result - ); - - // Assert - Assert.IsFalse(success); - Assert.That(result, Is.EqualTo(new Guid("00000000-0000-0000-0000-000000000000"))); - } - - [Test] - public static void Converting_Unsupported_Object_To_A_Guid_Returns_False() - { - // Arrange - var clearlyWillNotConvertToGuid = new StringBuilder(0); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToGuid( - clearlyWillNotConvertToGuid, - out Guid result - ); - - // Assert - Assert.IsFalse(success); - Assert.That(result, Is.EqualTo(new Guid("00000000-0000-0000-0000-000000000000"))); - } - - // ------- UDI Conversion Tests - [Test] - public void Converting_Boxed_Udi_To_A_Udi_Returns_Original_Udi_Value() - { - // Arrange - SetUpDependencyContainer(); - Udi.ResetUdiTypes(); - - Udi sample = new GuidUdi(Constants.UdiEntityType.AnyGuid, Guid.NewGuid()); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToUdi( - sample, - out Udi result - ); - - // Assert - Assert.IsTrue(success); - Assert.That(result, Is.EqualTo(sample)); - } - - [Test] - public void Converting_String_Udi_To_A_Udi_Returns_Original_Udi_Value() - { - // Arrange - SetUpDependencyContainer(); - Udi.ResetUdiTypes(); - - Udi sample = new GuidUdi(Constants.UdiEntityType.AnyGuid, Guid.NewGuid()); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToUdi( - sample.ToString(), - out Udi result - ); - - // Assert - Assert.IsTrue(success); - Assert.That(result, Is.EqualTo(sample)); - } - - [Test] - public void Converting_Hello_To_A_Udi_Returns_False() - { - // Arrange - SetUpDependencyContainer(); - Udi.ResetUdiTypes(); - - const string SAMPLE = "Hello"; - - // Act - bool success = UmbracoHelper.ConvertIdObjectToUdi( - SAMPLE, - out Udi result - ); - - // Assert - Assert.IsFalse(success); - Assert.That(result, Is.Null); - } - - [Test] - public void Converting_Unsupported_Object_To_A_Udi_Returns_False() - { - // Arrange - SetUpDependencyContainer(); - Udi.ResetUdiTypes(); - - var clearlyWillNotConvertToGuid = new StringBuilder(0); - - // Act - bool success = UmbracoHelper.ConvertIdObjectToUdi( - clearlyWillNotConvertToGuid, - out Udi result - ); - - // Assert - Assert.IsFalse(success); - Assert.That(result, Is.Null); - } - private void SetUpDependencyContainer() { // fixme - bad in a unit test - but Udi has a static ctor that wants it?! diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index 38fbd0b865..801eb84d5d 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -281,6 +281,11 @@ namespace Umbraco.Web.Editors { HandleContentNotFound(id); return null;//irrelevant since the above throws + } + var content = MapToDisplay(foundContent, culture); + return content; + } + /// /// Gets the content json for the content id /// @@ -288,15 +293,16 @@ namespace Umbraco.Web.Editors /// [OutgoingEditorModelEvent] [EnsureUserPermissionForContent("id")] - public ContentItemDisplay GetById(Guid id) + public ContentItemDisplay GetById(Guid id, string culture = null) { var foundContent = GetObjectFromRequest(() => Services.ContentService.GetById(id)); if (foundContent == null) { HandleContentNotFound(id); + return null;//irrelevant since the above throws } - var content = AutoMapperExtensions.MapWithUmbracoContext(foundContent, UmbracoContext); + var content = MapToDisplay(foundContent, culture); return content; } @@ -307,22 +313,16 @@ namespace Umbraco.Web.Editors /// [OutgoingEditorModelEvent] [EnsureUserPermissionForContent("id")] - public ContentItemDisplay GetById(Udi id) + public ContentItemDisplay GetById(Udi id, string culture = null) { var guidUdi = id as GuidUdi; if (guidUdi != null) { - return GetById(guidUdi.Guid); + return GetById(guidUdi.Guid, culture); } throw new HttpResponseException(HttpStatusCode.NotFound); - } - - } - - var content = MapToDisplay(foundContent, culture); - return content; - } + } /// /// Gets an empty content item for the diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 0852317b31..fae7bfdfbf 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -176,14 +176,6 @@ namespace Umbraco.Web.Editors else { //create - - // file might already be on disk, if so grab the content to avoid overwriting - string content = Services.FileService.GetViewContent(display.Alias); - if (string.IsNullOrEmpty(content) == false) - { - display.Content = content; - } - ITemplate master = null; if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) { diff --git a/src/Umbraco.Web/WebApi/Filters/EnsureUserPermissionForContentAttribute.cs b/src/Umbraco.Web/WebApi/Filters/EnsureUserPermissionForContentAttribute.cs index 544af44a60..4d9a6db129 100644 --- a/src/Umbraco.Web/WebApi/Filters/EnsureUserPermissionForContentAttribute.cs +++ b/src/Umbraco.Web/WebApi/Filters/EnsureUserPermissionForContentAttribute.cs @@ -6,6 +6,8 @@ using Umbraco.Core.Exceptions; using Umbraco.Web.Composing; using Umbraco.Web.Editors; using Umbraco.Web._Legacy.Actions; +using Umbraco.Core; +using Umbraco.Core.Models; namespace Umbraco.Web.WebApi.Filters { @@ -77,13 +79,15 @@ namespace Umbraco.Web.WebApi.Filters nodeId = parsedId; } else if (Udi.TryParse(argument, true, out Udi udi)) - { - nodeId = ApplicationContext.Current.Services.EntityService.GetIdForUdi(udi).Result; + { + //fixme: inject? we can't because this is an attribute but we could provide ctors and empty ctors that pass in the required services + nodeId = Current.Services.EntityService.GetId(udi).Result; } else { Guid.TryParse(argument, out Guid key); - nodeId = ApplicationContext.Current.Services.EntityService.GetIdForKey(key, UmbracoObjectTypes.Document).Result; + //fixme: inject? we can't because this is an attribute but we could provide ctors and empty ctors that pass in the required services + nodeId = Current.Services.EntityService.GetId(key, UmbracoObjectTypes.Document).Result; } } else @@ -105,7 +109,8 @@ namespace Umbraco.Web.WebApi.Filters if (ContentController.CheckPermissions( actionContext.Request.Properties, - UmbracoContext.Current.Security.CurrentUser, + //fixme: inject? we can't because this is an attribute but we could provide ctors and empty ctors that pass in the required services + Current.UmbracoContext.Security.CurrentUser, Current.Services.UserService, Current.Services.ContentService, Current.Services.EntityService,