Files
Umbraco-CMS/src/Umbraco.PublishedCache.NuCache/PublishedMember.cs

126 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web.PublishedCache.NuCache.DataSource;
namespace Umbraco.Web.PublishedCache.NuCache
{
// note
// the whole PublishedMember thing should be refactored because as soon as a member
// is wrapped on in a model, the inner IMember and all associated properties are lost
internal class PublishedMember : PublishedContent //, IPublishedMember
{
private readonly IMember _member;
private PublishedMember(
IMember member,
ContentNode contentNode,
ContentData contentData,
IPublishedSnapshotAccessor publishedSnapshotAccessor,
IVariationContextAccessor variationContextAccessor,
IPublishedModelFactory publishedModelFactory)
: base(contentNode, contentData, publishedSnapshotAccessor, variationContextAccessor, publishedModelFactory)
{
_member = member;
}
public static IPublishedContent Create(
IMember member,
IPublishedContentType contentType,
bool previewing,
IPublishedSnapshotAccessor publishedSnapshotAccessor,
IVariationContextAccessor variationContextAccessor,
IPublishedModelFactory publishedModelFactory)
{
var d = new ContentData
{
Name = member.Name,
Published = previewing,
TemplateId = -1,
VersionDate = member.UpdateDate,
WriterId = member.CreatorId, // what else?
Properties = GetPropertyValues(contentType, member)
};
var n = new ContentNode(member.Id, member.Key,
contentType,
member.Level, member.Path, member.SortOrder,
member.ParentId,
member.CreateDate, member.CreatorId);
return new PublishedMember(member, n, d, publishedSnapshotAccessor, variationContextAccessor, publishedModelFactory).CreateModel(publishedModelFactory);
}
private static Dictionary<string, PropertyData[]> GetPropertyValues(IPublishedContentType contentType, IMember member)
{
// see node in PublishedSnapshotService
// we do not (want to) support ConvertDbToXml/String
//var propertyEditorResolver = PropertyEditorResolver.Current;
// see note in MemberType.Variations
// we don't want to support variations on members
var properties = member
.Properties
//.Select(property =>
//{
// var e = propertyEditorResolver.GetByAlias(property.PropertyType.PropertyEditorAlias);
// var v = e == null
// ? property.Value
// : e.ValueEditor.ConvertDbToString(property, property.PropertyType, ApplicationContext.Current.Services.DataTypeService);
// return new KeyValuePair<string, object>(property.Alias, v);
//})
//.ToDictionary(x => x.Key, x => x.Value);
.ToDictionary(x => x.Alias, x => new[] { new PropertyData { Value = x.GetValue(), Culture = string.Empty, Segment = string.Empty } }, StringComparer.OrdinalIgnoreCase);
// see also PublishedContentType
AddIf(contentType, properties, nameof(IMember.Email), member.Email);
AddIf(contentType, properties, nameof(IMember.Username), member.Username);
AddIf(contentType, properties, nameof(IMember.Comments), member.Comments);
AddIf(contentType, properties, nameof(IMember.IsApproved), member.IsApproved);
AddIf(contentType, properties, nameof(IMember.IsLockedOut), member.IsLockedOut);
AddIf(contentType, properties, nameof(IMember.LastLockoutDate), member.LastLockoutDate);
AddIf(contentType, properties, nameof(IMember.CreateDate), member.CreateDate);
AddIf(contentType, properties, nameof(IMember.LastLoginDate), member.LastLoginDate);
AddIf(contentType, properties, nameof(IMember.LastPasswordChangeDate), member.LastPasswordChangeDate);
return properties;
}
private static void AddIf(IPublishedContentType contentType, IDictionary<string, PropertyData[]> properties, string alias, object value)
{
var propertyType = contentType.GetPropertyType(alias);
if (propertyType == null || propertyType.IsUserProperty) return;
properties[alias] = new[] { new PropertyData { Value = value, Culture = string.Empty, Segment = string.Empty } };
}
#region IPublishedMember
public IMember Member => _member;
public string Email => _member.Email;
public string UserName => _member.Username;
public string Comments => _member.Comments;
public bool IsApproved => _member.IsApproved;
public bool IsLockedOut => _member.IsLockedOut;
public DateTime LastLockoutDate => _member.LastLockoutDate;
public DateTime CreationDate => _member.CreateDate;
public DateTime LastLoginDate => _member.LastLoginDate;
public DateTime LastActivityDate => _member.LastLoginDate;
public DateTime LastPasswordChangedDate => _member.LastPasswordChangeDate;
#endregion
}
}