2013-06-17 01:06:31 +02:00
|
|
|
|
using System;
|
2013-06-18 17:22:01 +10:00
|
|
|
|
using AutoMapper;
|
2013-06-17 01:06:31 +02:00
|
|
|
|
using Umbraco.Core;
|
2013-07-01 14:23:56 +10:00
|
|
|
|
using Umbraco.Core.Models.Mapping;
|
2013-06-17 01:06:31 +02:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
2013-07-01 14:23:56 +10:00
|
|
|
|
internal class UserModelMapper : MapperConfiguration
|
2013-06-17 01:06:31 +02:00
|
|
|
|
{
|
2013-07-01 14:23:56 +10:00
|
|
|
|
|
|
|
|
|
|
#region Mapper config
|
2013-07-25 15:31:26 +10:00
|
|
|
|
public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext)
|
2013-06-17 01:06:31 +02:00
|
|
|
|
{
|
2013-07-01 14:23:56 +10:00
|
|
|
|
config.CreateMap<IUser, UserDetail>()
|
2013-06-18 17:22:01 +10:00
|
|
|
|
.ForMember(detail => detail.UserId, opt => opt.MapFrom(user => GetIntId(user.Id)))
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
detail => detail.EmailHash,
|
|
|
|
|
|
opt => opt.MapFrom(user => user.Email.ToLowerInvariant().Trim().ToMd5()));
|
2013-07-01 14:23:56 +10:00
|
|
|
|
config.CreateMap<IProfile, UserBasic>()
|
2013-06-18 17:22:01 +10:00
|
|
|
|
.ForMember(detail => detail.UserId, opt => opt.MapFrom(profile => GetIntId(profile.Id)));
|
2013-07-01 14:23:56 +10:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2013-06-18 17:22:01 +10:00
|
|
|
|
|
|
|
|
|
|
private static int GetIntId(object id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = id.TryConvertTo<int>();
|
2013-06-17 01:06:31 +02:00
|
|
|
|
if (result.Success == false)
|
|
|
|
|
|
{
|
2013-06-18 17:22:01 +10:00
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
|
"Cannot convert the profile to a " + typeof(UserDetail).Name + " object since the id is not an integer");
|
2013-06-17 01:06:31 +02:00
|
|
|
|
}
|
2013-06-18 17:22:01 +10:00
|
|
|
|
return result.Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public UserDetail ToUserDetail(IUser user)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Mapper.Map<UserDetail>(user);
|
2013-06-17 01:06:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public UserBasic ToUserBasic(IProfile profile)
|
|
|
|
|
|
{
|
2013-06-18 17:57:15 +10:00
|
|
|
|
return Mapper.Map<UserBasic>(profile);
|
2013-07-01 14:23:56 +10:00
|
|
|
|
}
|
2013-06-17 01:06:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|