From b6185b847f7fe13c44737c98fd83dcecdda0d1a6 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 2 Apr 2014 11:13:54 +0200 Subject: [PATCH 1/5] U4-4575 - property converters use wrong source value --- src/Umbraco.Web/Models/PublishedProperty.cs | 69 +++++++++++++++++++ .../PublishedCache/MemberPublishedContent.cs | 17 +++-- src/Umbraco.Web/Umbraco.Web.csproj | 1 + 3 files changed, 78 insertions(+), 9 deletions(-) create mode 100644 src/Umbraco.Web/Models/PublishedProperty.cs diff --git a/src/Umbraco.Web/Models/PublishedProperty.cs b/src/Umbraco.Web/Models/PublishedProperty.cs new file mode 100644 index 0000000000..e5d376c98e --- /dev/null +++ b/src/Umbraco.Web/Models/PublishedProperty.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Umbraco.Core; +using Umbraco.Core.Models; +using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; + +namespace Umbraco.Web.Models +{ + public static class PublishedProperty + { + /// + /// Maps a collection of Property to a collection of IPublishedProperty for a specified collection of PublishedPropertyType. + /// + /// The published property types. + /// The properties. + /// A mapping function. + /// A collection of IPublishedProperty corresponding to the collection of PublishedPropertyType + /// and taking values from the collection of Property. + /// Ensures that all conversions took place correctly. + internal static IEnumerable MapProperties( + IEnumerable propertyTypes, IEnumerable properties, + Func map) + { + var peResolver = PropertyEditorResolver.Current; + var dtService = ApplicationContext.Current.Services.DataTypeService; + return MapProperties(propertyTypes, properties, peResolver, dtService, map); + } + + /// + /// Maps a collection of Property to a collection of IPublishedProperty for a specified collection of PublishedPropertyType. + /// + /// The published property types. + /// The properties. + /// A mapping function. + /// A PropertyEditorResolver instance. + /// An IDataTypeService instance. + /// A collection of IPublishedProperty corresponding to the collection of PublishedPropertyType + /// and taking values from the collection of Property. + /// Ensures that all conversions took place correctly. + internal static IEnumerable MapProperties( + IEnumerable propertyTypes, IEnumerable properties, + PropertyEditorResolver propertyEditorResolver, IDataTypeService dataTypeService, + Func map) + { + return propertyTypes + .Select(x => + { + var p = properties.SingleOrDefault(xx => xx.Alias == x.PropertyTypeAlias); + var v = p == null || p.Value == null ? null : p.Value; + if (v != null) + { + var e = propertyEditorResolver.GetByAlias(x.PropertyEditorAlias); + if (e != null) + v = e.ValueEditor.ConvertDbToString(p, p.PropertyType, dataTypeService); + } + // fixme - means that the IPropertyValueConverter will always get a string + // fixme and never an int or DateTime that's in the DB unless the value editor has + // fixme a way to say it's OK to use what's in the DB? + + return map(x, p, v); + }); + } + } +} diff --git a/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs b/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs index deafcaab40..8570226a64 100644 --- a/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs +++ b/src/Umbraco.Web/PublishedCache/MemberPublishedContent.cs @@ -6,6 +6,8 @@ using System.Web.Security; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; +using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; using Umbraco.Web.Models; namespace Umbraco.Web.PublishedCache @@ -18,7 +20,7 @@ namespace Umbraco.Web.PublishedCache private readonly IMember _member; private readonly MembershipUser _membershipUser; - private readonly List _properties; + private readonly IPublishedProperty[] _properties; private readonly PublishedContentType _publishedMemberType; public MemberPublishedContent(IMember member, MembershipUser membershipUser) @@ -28,21 +30,18 @@ namespace Umbraco.Web.PublishedCache _member = member; _membershipUser = membershipUser; - _properties = new List(); _publishedMemberType = PublishedContentType.Get(PublishedItemType.Member, _member.ContentTypeAlias); if (_publishedMemberType == null) { throw new InvalidOperationException("Could not get member type with alias " + _member.ContentTypeAlias); } - foreach (var propType in _publishedMemberType.PropertyTypes) - { - var val = _member.Properties.Any(x => x.Alias == propType.PropertyTypeAlias) == false - ? string.Empty - : _member.Properties[propType.PropertyTypeAlias].Value; - _properties.Add(new RawValueProperty(propType, val ?? string.Empty)); - } + + _properties = PublishedProperty.MapProperties(_publishedMemberType.PropertyTypes, _member.Properties, + (t, p, v) => new RawValueProperty(t, v ?? string.Empty)) + .ToArray(); } + #region Membership provider member properties public string Email { diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 29815c85d6..41cc68370a 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -352,6 +352,7 @@ + From 6d3e8e1f6d4b352389ae2185b53a87d8c48873f7 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 2 Apr 2014 11:19:06 +0200 Subject: [PATCH 2/5] U4-4559 - tests --- .../PublishedContent/DynamicPublishedContentTests.cs | 11 ++++++++++- .../PublishedContent/PublishedContentTests.cs | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs index a6e7335784..9280c176bb 100644 --- a/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs @@ -72,7 +72,16 @@ namespace Umbraco.Tests.PublishedContent Assert.IsTrue(ddoc.HasProperty(Constants.Conventions.Content.UrlAlias)); } - /// + [Test] + public void U4_4559() + { + var doc = GetDynamicNode(1174); + var result = doc.AncestorOrSelf(1); + Assert.IsNotNull(result); + Assert.AreEqual(1046, result.Id); + } + + /// /// Test class to mimic UmbracoHelper when returning docs /// public class TestHelper diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs index bed40b9b20..fcfc6a6932 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs @@ -508,6 +508,15 @@ namespace Umbraco.Tests.PublishedContent Assert.AreEqual(1173, result.Id); } + [Test] + public void U4_4559() + { + var doc = GetNode(1174); + var result = doc.AncestorOrSelf(1); + Assert.IsNotNull(result); + Assert.AreEqual(1046, result.Id); + } + [Test] public void Ancestors_Or_Self() { From d9c66cd4b3c679b87e68cd6e5c77b949c3908984 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Wed, 2 Apr 2014 11:41:59 +0200 Subject: [PATCH 3/5] Adds AddRelation and missing events in RelationService from v7 into v6 --- src/Umbraco.Core/Services/IRelationService.cs | 8 ++ src/Umbraco.Core/Services/RelationService.cs | 135 +++++++++++++++++- 2 files changed, 137 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Core/Services/IRelationService.cs b/src/Umbraco.Core/Services/IRelationService.cs index 32e7d88f2a..a219cfc43b 100644 --- a/src/Umbraco.Core/Services/IRelationService.cs +++ b/src/Umbraco.Core/Services/IRelationService.cs @@ -182,6 +182,14 @@ namespace Umbraco.Core.Services /// Returns True if any relations exists with the given Id, otherwise False bool IsRelated(int id); + /// + /// Checks whether two items are related + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Returns True if any relations exists with the given Ids, otherwise False + bool AreRelated(int parentId, int childId); + /// /// Saves a /// diff --git a/src/Umbraco.Core/Services/RelationService.cs b/src/Umbraco.Core/Services/RelationService.cs index 5da9fddef0..5102731902 100644 --- a/src/Umbraco.Core/Services/RelationService.cs +++ b/src/Umbraco.Core/Services/RelationService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Persistence; @@ -326,14 +327,18 @@ namespace Umbraco.Core.Services Save(relationType); var relation = new Relation(parent.Id, child.Id, relationType); + if (SavingRelation.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return relation; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); - - return relation; } + + SavedRelation.RaiseEvent(new SaveEventArgs(relation, false), this); + return relation; } /// @@ -350,14 +355,18 @@ namespace Umbraco.Core.Services throw new ArgumentNullException(string.Format("No RelationType with Alias '{0}' exists.", relationTypeAlias)); var relation = new Relation(parent.Id, child.Id, relationType); + if (SavingRelation.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return relation; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); - - return relation; } + + SavedRelation.RaiseEvent(new SaveEventArgs(relation, false), this); + return relation; } /// @@ -388,18 +397,72 @@ namespace Umbraco.Core.Services } } + /// + /// Checks whether two items are related + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Returns True if any relations exists with the given Ids, otherwise False + public bool AreRelated(int parentId, int childId) + { + using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) + { + var query = new Query().Where(x => x.ParentId == parentId && x.ChildId == childId); + return repository.GetByQuery(query).Any(); + } + } + + /// + /// Checks whether two items are related with a given relation type alias + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Alias of the relation type + /// Returns True if any relations exists with the given Ids and relation type, otherwise False + public bool AreRelated(int parentId, int childId, string relationTypeAlias) + { + var relType = GetRelationTypeByAlias(relationTypeAlias); + if(relType == null) + return false; + + return AreRelated(parentId, childId, relType); + } + + + /// + /// Checks whether two items are related with a given relation type + /// + /// Id of the Parent relation + /// Id of the Child relation + /// Type of relation + /// Returns True if any relations exists with the given Ids and relation type, otherwise False + public bool AreRelated(int parentId, int childId, IRelationType relationType) + { + using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) + { + var query = new Query().Where(x => x.ParentId == parentId && x.ChildId == childId && x.RelationTypeId == relationType.Id); + return repository.GetByQuery(query).Any(); + } + } + + /// /// Saves a /// /// Relation to save public void Save(IRelation relation) { + if (SavingRelation.IsRaisedEventCancelled(new SaveEventArgs(relation), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.AddOrUpdate(relation); uow.Commit(); } + + SavedRelation.RaiseEvent(new SaveEventArgs(relation, false), this); } /// @@ -408,12 +471,17 @@ namespace Umbraco.Core.Services /// RelationType to Save public void Save(IRelationType relationType) { + if (SavingRelationType.IsRaisedEventCancelled(new SaveEventArgs(relationType), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) { repository.AddOrUpdate(relationType); uow.Commit(); } + + SavedRelationType.RaiseEvent(new SaveEventArgs(relationType, false), this); } /// @@ -422,12 +490,17 @@ namespace Umbraco.Core.Services /// Relation to Delete public void Delete(IRelation relation) { + if (DeletingRelation.IsRaisedEventCancelled(new DeleteEventArgs(relation), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { repository.Delete(relation); uow.Commit(); } + + DeletedRelation.RaiseEvent(new DeleteEventArgs(relation, false), this); } /// @@ -436,12 +509,17 @@ namespace Umbraco.Core.Services /// RelationType to Delete public void Delete(IRelationType relationType) { + if (DeletingRelationType.IsRaisedEventCancelled(new DeleteEventArgs(relationType), this)) + return; + var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) { repository.Delete(relationType); uow.Commit(); } + + DeletedRelationType.RaiseEvent(new DeleteEventArgs(relationType, false), this); } /// @@ -450,18 +528,21 @@ namespace Umbraco.Core.Services /// to Delete Relations for public void DeleteRelationsOfType(IRelationType relationType) { + var relations = new List(); var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { var query = new Query().Where(x => x.RelationTypeId == relationType.Id); - var list = repository.GetByQuery(query).ToList(); + relations.AddRange(repository.GetByQuery(query).ToList()); - foreach (var relation in list) + foreach (var relation in relations) { repository.Delete(relation); } uow.Commit(); } + + DeletedRelation.RaiseEvent(new DeleteEventArgs(relations, false), this); } #region Private Methods @@ -480,5 +561,47 @@ namespace Umbraco.Core.Services return relations; } #endregion + + #region Events Handlers + /// + /// Occurs before Deleting a Relation + /// + public static event TypedEventHandler> DeletingRelation; + + /// + /// Occurs after a Relation is Deleted + /// + public static event TypedEventHandler> DeletedRelation; + + /// + /// Occurs before Saving a Relation + /// + public static event TypedEventHandler> SavingRelation; + + /// + /// Occurs after a Relation is Saved + /// + public static event TypedEventHandler> SavedRelation; + + /// + /// Occurs before Deleting a RelationType + /// + public static event TypedEventHandler> DeletingRelationType; + + /// + /// Occurs after a RelationType is Deleted + /// + public static event TypedEventHandler> DeletedRelationType; + + /// + /// Occurs before Saving a RelationType + /// + public static event TypedEventHandler> SavingRelationType; + + /// + /// Occurs after a RelationType is Saved + /// + public static event TypedEventHandler> SavedRelationType; + #endregion } } \ No newline at end of file From a768fe5bb3fb0fa623f8b644dee789fd6bb5695c Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Wed, 2 Apr 2014 11:42:48 +0200 Subject: [PATCH 4/5] Ports behaviour from membershiphelper from v7 to 6 to make them the same --- src/Umbraco.Web/Security/MembershipHelper.cs | 33 ++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Web/Security/MembershipHelper.cs b/src/Umbraco.Web/Security/MembershipHelper.cs index 4572026938..d1db014436 100644 --- a/src/Umbraco.Web/Security/MembershipHelper.cs +++ b/src/Umbraco.Web/Security/MembershipHelper.cs @@ -276,8 +276,15 @@ namespace Umbraco.Web.Security { var membershipUser = provider.GetCurrentUser(); var member = GetCurrentMember(); - //this shouldn't happen - if (member == null) return null; + //this shouldn't happen but will if the member is deleted in the back office while the member is trying + // to use the front-end! + if (member == null) + { + //log them out since they've been removed + FormsAuthentication.SignOut(); + + return null; + } var model = ProfileModel.CreateModel(); model.Name = member.Name; @@ -416,8 +423,15 @@ namespace Umbraco.Web.Security if (provider.IsUmbracoMembershipProvider()) { var member = GetCurrentMember(); - //this shouldn't happen - if (member == null) return model; + //this shouldn't happen but will if the member is deleted in the back office while the member is trying + // to use the front-end! + if (member == null) + { + //log them out since they've been removed + FormsAuthentication.SignOut(); + model.IsLoggedIn = false; + return model; + } model.Name = member.Name; model.Username = member.Username; model.Email = member.Email; @@ -425,8 +439,15 @@ namespace Umbraco.Web.Security else { var member = provider.GetCurrentUser(); - //this shouldn't happen - if (member == null) return null; + //this shouldn't happen but will if the member is deleted in the back office while the member is trying + // to use the front-end! + if (member == null) + { + //log them out since they've been removed + FormsAuthentication.SignOut(); + model.IsLoggedIn = false; + return model; + } model.Name = member.UserName; model.Username = member.UserName; model.Email = member.Email; From 53c0bd6938f345ad9f1cd8892af3ae83553e1d94 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Wed, 2 Apr 2014 11:59:16 +0200 Subject: [PATCH 5/5] Fixes: Password is not saved when using the Register snippet --- src/Umbraco.Web/Controllers/UmbRegisterController.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Umbraco.Web/Controllers/UmbRegisterController.cs b/src/Umbraco.Web/Controllers/UmbRegisterController.cs index dea1135131..aafa6fe0db 100644 --- a/src/Umbraco.Web/Controllers/UmbRegisterController.cs +++ b/src/Umbraco.Web/Controllers/UmbRegisterController.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Web.Mvc; using System.Web.Security; +using System.Web.Services.Description; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.member; using Umbraco.Core; @@ -24,6 +25,11 @@ namespace Umbraco.Web.Controllers MembershipCreateStatus status; var member = Members.RegisterMember(model, out status, model.LoginOnSuccess); + // Save the password + var memberService = Services.MemberService; + var m = memberService.GetByUsername(member.UserName); + memberService.SavePassword(m, model.Password); + switch (status) { case MembershipCreateStatus.Success: