Updates business logic to persist the start nodes for users

This commit is contained in:
Shannon
2017-05-25 02:46:21 +10:00
parent 59872b0e17
commit 2cd23cb24f
4 changed files with 54 additions and 13 deletions

View File

@@ -24,7 +24,7 @@ namespace Umbraco.Core.Models.Rdbms
[Column("startNodeType")]
[NullSetting(NullSetting = NullSettings.NotNull)]
[Index(IndexTypes.UniqueNonClustered, ForColumns = "startNodeType, startNode", Name = "IX_umbracoUserStartNode_startNodeType")]
[Index(IndexTypes.UniqueNonClustered, ForColumns = "startNodeType, startNode, userId", Name = "IX_umbracoUserStartNode_startNodeType")]
public int StartNodeType { get; set; }
public enum StartNodeTypeValue

View File

@@ -32,8 +32,8 @@ namespace Umbraco.Core.Persistence.Factories
user.DisableChangeTracking();
user.Key = guidId;
user.StartContentIds = dto.UserStartNodeDtos.Where(x => x.StartNodeType == (int)UserStartNodeDto.StartNodeTypeValue.Content).Select(x => x.Id).ToArray();
user.StartMediaIds = dto.UserStartNodeDtos.Where(x => x.StartNodeType == (int) UserStartNodeDto.StartNodeTypeValue.Media).Select(x => x.Id).ToArray();
user.StartContentIds = dto.UserStartNodeDtos.Where(x => x.StartNodeType == (int)UserStartNodeDto.StartNodeTypeValue.Content).Select(x => x.StartNode).ToArray();
user.StartMediaIds = dto.UserStartNodeDtos.Where(x => x.StartNodeType == (int) UserStartNodeDto.StartNodeTypeValue.Media).Select(x => x.StartNode).ToArray();
user.IsLockedOut = dto.NoConsole;
user.IsApproved = dto.Disabled == false;
user.Language = dto.UserLanguage;

View File

@@ -204,6 +204,18 @@ namespace Umbraco.Core.Persistence.Repositories
var id = Convert.ToInt32(Database.Insert(userDto));
entity.Id = id;
if (entity.IsPropertyDirty("StartContentIds") || entity.IsPropertyDirty("StartMediaIds"))
{
if (entity.IsPropertyDirty("StartContentIds"))
{
AddingOrUpdateStartNodes(entity, Enumerable.Empty<UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
}
if (entity.IsPropertyDirty("StartMediaIds"))
{
AddingOrUpdateStartNodes(entity, Enumerable.Empty<UserStartNodeDto>(), UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
}
}
if (entity.IsPropertyDirty("Groups"))
{
//lookup all assigned
@@ -228,7 +240,7 @@ namespace Umbraco.Core.Persistence.Repositories
protected override void PersistUpdatedItem(IUser entity)
{
//Updates Modified date
((User)entity).UpdatingEntity();
((User)entity).UpdatingEntity();
//ensure security stamp if non
if (entity.SecurityStamp.IsNullOrWhiteSpace())
@@ -287,8 +299,21 @@ namespace Umbraco.Core.Persistence.Repositories
Database.Update(userDto, changedCols);
}
if (entity.IsPropertyDirty("StartContentIds") || entity.IsPropertyDirty("StartMediaIds"))
{
var assignedStartNodes = Database.Fetch<UserStartNodeDto>("SELECT * FROM umbracoUserStartNode WHERE userId = @userId", new { userId = entity.Id });
if (entity.IsPropertyDirty("StartContentIds"))
{
AddingOrUpdateStartNodes(entity, assignedStartNodes, UserStartNodeDto.StartNodeTypeValue.Content, entity.StartContentIds);
}
if (entity.IsPropertyDirty("StartMediaIds"))
{
AddingOrUpdateStartNodes(entity, assignedStartNodes, UserStartNodeDto.StartNodeTypeValue.Media, entity.StartMediaIds);
}
}
if (entity.IsPropertyDirty("Groups"))
{
{
//lookup all assigned
var assigned = entity.Groups == null || entity.Groups.Any() == false
? new List<UserGroupDto>()
@@ -312,6 +337,28 @@ namespace Umbraco.Core.Persistence.Repositories
entity.ResetDirtyProperties();
}
private void AddingOrUpdateStartNodes(IEntity entity, IEnumerable<UserStartNodeDto> current, UserStartNodeDto.StartNodeTypeValue startNodeType, int[] entityStartIds)
{
var assignedIds = current.Where(x => x.StartNodeType == (int)startNodeType).Select(x => x.StartNode).ToArray();
//remove the ones not assigned to the entity
var toDelete = assignedIds.Except(entityStartIds).ToArray();
if (toDelete.Length > 0)
Database.Delete<UserStartNodeDto>("WHERE UserId = @UserId AND startNode IN (@startNodes)", new {UserId = entity.Id, startNodes = toDelete});
//add the ones not currently in the db
var toAdd = entityStartIds.Except(assignedIds).ToArray();
foreach (var i in toAdd)
{
var dto = new UserStartNodeDto
{
StartNode = i,
StartNodeType = (int)startNodeType,
UserId = entity.Id
};
Database.Insert(dto);
}
}
#endregion
#region Implementation of IUserRepository

View File

@@ -718,10 +718,7 @@ namespace umbraco.BusinessLogic
}
set
{
if (UserEntity.StartContentIds == null)
UserEntity.StartContentIds = new int[] {value};
else if (UserEntity.StartContentIds.Contains(value) == false)
UserEntity.StartContentIds = UserEntity.StartContentIds.Concat(new[] {value}).ToArray();
UserEntity.StartContentIds = new int[] { value };
}
}
@@ -735,10 +732,7 @@ namespace umbraco.BusinessLogic
}
set
{
if (UserEntity.StartMediaIds == null)
UserEntity.StartMediaIds = new int[] { value };
else if (UserEntity.StartMediaIds.Contains(value) == false)
UserEntity.StartMediaIds = UserEntity.StartMediaIds.Concat(new[] { value }).ToArray();
UserEntity.StartMediaIds = new int[] { value };
}
}