2013-08-12 15:06:12 +02:00
|
|
|
|
using System;
|
2016-10-28 09:20:52 +02:00
|
|
|
|
using System.Linq;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models.Mapping;
|
|
|
|
|
|
using Umbraco.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2013-11-15 12:26:29 +11:00
|
|
|
|
using umbraco;
|
2017-05-26 02:15:37 +10:00
|
|
|
|
using Umbraco.Core.IO;
|
2015-03-24 20:17:37 +11:00
|
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
|
using Umbraco.Core.Models.Identity;
|
|
|
|
|
|
using Umbraco.Core.Security;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Models.Mapping
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class UserModelMapper : MapperConfiguration
|
|
|
|
|
|
{
|
|
|
|
|
|
public override void ConfigureMappings(IConfiguration config, ApplicationContext applicationContext)
|
2017-05-25 12:17:32 +10:00
|
|
|
|
{
|
|
|
|
|
|
//Used for merging existing UserSave to an existing IUser instance - this will not create an IUser instance!
|
|
|
|
|
|
config.CreateMap<UserSave, IUser>()
|
|
|
|
|
|
.ForMember(user => user.Language, expression => expression.MapFrom(save => save.Culture))
|
2017-05-26 00:02:32 +10:00
|
|
|
|
.ForMember(user => user.Avatar, expression => expression.Ignore())
|
2017-05-25 12:17:32 +10:00
|
|
|
|
.ForMember(user => user.SessionTimeout, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.SecurityStamp, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.ProviderUserKey, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.RawPasswordValue, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.PasswordQuestion, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.RawPasswordAnswerValue, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.Comments, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.IsApproved, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.IsLockedOut, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.LastLoginDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.LastPasswordChangeDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.LastLockoutDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.FailedPasswordAttempts, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.DeletedDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.CreateDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.UpdateDate, expression => expression.Ignore())
|
|
|
|
|
|
.AfterMap((save, user) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
user.ClearGroups();
|
2017-05-26 11:17:06 +10:00
|
|
|
|
var foundGroups = applicationContext.Services.UserService.GetUserGroupsByAlias(save.UserGroups.ToArray());
|
|
|
|
|
|
foreach (var group in foundGroups)
|
2017-05-25 12:17:32 +10:00
|
|
|
|
{
|
2017-05-26 11:17:06 +10:00
|
|
|
|
user.AddGroup(group.ToReadOnlyGroup());
|
2017-05-25 12:17:32 +10:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-05-25 02:03:41 +10:00
|
|
|
|
config.CreateMap<UserInvite, IUser>()
|
2017-05-24 19:01:01 +10:00
|
|
|
|
.ConstructUsing(invite => new User(invite.Name, invite.Email, invite.Email, Guid.NewGuid().ToString("N")))
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(user => user.Id, expression => expression.Ignore())
|
2017-05-26 00:02:32 +10:00
|
|
|
|
.ForMember(user => user.Avatar, expression => expression.Ignore())
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(user => user.SessionTimeout, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.StartContentIds, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.StartMediaIds, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.Language, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.SecurityStamp, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.ProviderUserKey, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.Username, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.RawPasswordValue, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.PasswordQuestion, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.RawPasswordAnswerValue, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.Comments, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.IsApproved, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.IsLockedOut, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.LastLoginDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.LastPasswordChangeDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.LastLockoutDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.FailedPasswordAttempts, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.DeletedDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.CreateDate, expression => expression.Ignore())
|
|
|
|
|
|
.ForMember(user => user.UpdateDate, expression => expression.Ignore())
|
2017-05-24 19:01:01 +10:00
|
|
|
|
.AfterMap((invite, user) =>
|
|
|
|
|
|
{
|
2017-05-26 11:17:06 +10:00
|
|
|
|
user.ClearGroups();
|
|
|
|
|
|
var foundGroups = applicationContext.Services.UserService.GetUserGroupsByAlias(invite.UserGroups.ToArray());
|
|
|
|
|
|
foreach (var group in foundGroups)
|
2017-05-24 19:01:01 +10:00
|
|
|
|
{
|
2017-05-26 11:17:06 +10:00
|
|
|
|
user.AddGroup(group.ToReadOnlyGroup());
|
|
|
|
|
|
}
|
2017-05-24 19:01:01 +10:00
|
|
|
|
});
|
|
|
|
|
|
|
2017-05-23 15:48:08 +10:00
|
|
|
|
config.CreateMap<IUserGroup, UserGroupDisplay>()
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(detail => detail.AvailableSections, opt => opt.MapFrom(x => applicationContext.Services.SectionService.GetSections()))
|
2017-05-23 15:48:08 +10:00
|
|
|
|
.ForMember(detail => detail.Sections, opt => opt.MapFrom(x => x.AllowedSections))
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(detail => detail.Notifications, opt => opt.Ignore())
|
2017-05-23 15:48:08 +10:00
|
|
|
|
.ForMember(detail => detail.Udi, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Trashed, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.ParentId, opt => opt.UseValue(-1))
|
|
|
|
|
|
.ForMember(detail => detail.Path, opt => opt.MapFrom(user => "-1," + user.Id))
|
|
|
|
|
|
.ForMember(detail => detail.AdditionalData, opt => opt.Ignore());
|
2017-05-23 15:23:10 +10:00
|
|
|
|
|
2017-05-25 12:17:32 +10:00
|
|
|
|
config.CreateMap<IUser, UserDisplay>()
|
2017-05-26 02:15:37 +10:00
|
|
|
|
.ForMember(detail => detail.Avatars, opt => opt.MapFrom(user => user.GetCurrentUserAvatarUrls(applicationContext.Services.UserService, applicationContext.ApplicationCache.RuntimeCache)))
|
2017-05-25 12:17:32 +10:00
|
|
|
|
.ForMember(detail => detail.Username, opt => opt.MapFrom(user => user.Username))
|
2017-05-26 12:47:31 +10:00
|
|
|
|
.ForMember(detail => detail.LastLoginDate, opt => opt.MapFrom(user => user.LastLoginDate == default(DateTime) ? null : (DateTime?)user.LastLoginDate))
|
2017-05-26 12:09:06 +10:00
|
|
|
|
.ForMember(detail => detail.UserGroups, opt => opt.MapFrom(user => user.Groups.Select(x => x.Alias).ToArray()))
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(detail => detail.StartContentIds, opt => opt.MapFrom(user => user.StartContentIds))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaIds, opt => opt.MapFrom(user => user.StartMediaIds))
|
2017-05-11 13:11:41 +10:00
|
|
|
|
.ForMember(detail => detail.Culture, opt => opt.MapFrom(user => user.GetUserCulture(applicationContext.Services.TextService)))
|
|
|
|
|
|
.ForMember(
|
2017-05-16 18:18:10 +10:00
|
|
|
|
detail => detail.AvailableUserGroups,
|
2017-05-25 02:03:41 +10:00
|
|
|
|
opt => opt.MapFrom(user => applicationContext.Services.UserService.GetAllUserGroups()))
|
2017-05-16 18:18:10 +10:00
|
|
|
|
.ForMember(
|
|
|
|
|
|
detail => detail.AvailableCultures,
|
|
|
|
|
|
opt => opt.MapFrom(user => applicationContext.Services.TextService.GetSupportedCultures().ToDictionary(x => x.Name, x => x.DisplayName)))
|
2017-05-22 21:23:04 +10:00
|
|
|
|
.ForMember(
|
|
|
|
|
|
detail => detail.EmailHash,
|
|
|
|
|
|
opt => opt.MapFrom(user => user.Email.ToLowerInvariant().Trim().ToMd5()))
|
2017-05-16 18:18:10 +10:00
|
|
|
|
.ForMember(detail => detail.ParentId, opt => opt.UseValue(-1))
|
2017-05-22 21:23:04 +10:00
|
|
|
|
.ForMember(detail => detail.Path, opt => opt.MapFrom(user => "-1," + user.Id))
|
2017-05-16 18:18:10 +10:00
|
|
|
|
.ForMember(detail => detail.Notifications, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Udi, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Icon, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Trashed, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Alias, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Trashed, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.AdditionalData, opt => opt.Ignore());
|
2017-05-11 13:11:41 +10:00
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
config.CreateMap<IUser, UserDetail>()
|
2017-05-26 02:15:37 +10:00
|
|
|
|
.ForMember(detail => detail.Avatars, opt => opt.MapFrom(user => user.GetCurrentUserAvatarUrls(applicationContext.Services.UserService, applicationContext.ApplicationCache.RuntimeCache)))
|
2014-04-14 15:20:02 +10:00
|
|
|
|
.ForMember(detail => detail.UserId, opt => opt.MapFrom(user => GetIntId(user.Id)))
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(detail => detail.StartContentIds, opt => opt.MapFrom(user => user.StartContentIds))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaIds, opt => opt.MapFrom(user => user.StartMediaIds))
|
2015-03-24 20:17:37 +11:00
|
|
|
|
.ForMember(detail => detail.Culture, opt => opt.MapFrom(user => user.GetUserCulture(applicationContext.Services.TextService)))
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
detail => detail.EmailHash,
|
|
|
|
|
|
opt => opt.MapFrom(user => user.Email.ToLowerInvariant().Trim().ToMd5()))
|
2017-05-26 11:17:06 +10:00
|
|
|
|
.ForMember(detail => detail.SecondsUntilTimeout, opt => opt.Ignore());
|
2013-10-15 18:46:44 +11:00
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
config.CreateMap<IProfile, UserBasic>()
|
|
|
|
|
|
.ForMember(detail => detail.UserId, opt => opt.MapFrom(profile => GetIntId(profile.Id)));
|
2015-03-24 20:17:37 +11:00
|
|
|
|
|
|
|
|
|
|
config.CreateMap<IUser, UserData>()
|
2016-02-02 15:14:47 +01:00
|
|
|
|
.ConstructUsing((IUser user) => new UserData())
|
2015-03-24 20:17:37 +11:00
|
|
|
|
.ForMember(detail => detail.Id, opt => opt.MapFrom(user => user.Id))
|
|
|
|
|
|
.ForMember(detail => detail.AllowedApplications, opt => opt.MapFrom(user => user.AllowedSections))
|
|
|
|
|
|
.ForMember(detail => detail.RealName, opt => opt.MapFrom(user => user.Name))
|
2017-05-10 21:00:30 +10:00
|
|
|
|
.ForMember(detail => detail.Roles, opt => opt.MapFrom(user => user.Groups.ToArray()))
|
2017-05-25 02:03:41 +10:00
|
|
|
|
.ForMember(detail => detail.StartContentNodes, opt => opt.MapFrom(user => user.StartContentIds))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaNodes, opt => opt.MapFrom(user => user.StartMediaIds))
|
2015-03-24 20:17:37 +11:00
|
|
|
|
.ForMember(detail => detail.Username, opt => opt.MapFrom(user => user.Username))
|
2015-04-01 14:29:35 +11:00
|
|
|
|
.ForMember(detail => detail.Culture, opt => opt.MapFrom(user => user.GetUserCulture(applicationContext.Services.TextService)))
|
|
|
|
|
|
.ForMember(detail => detail.SessionId, opt => opt.MapFrom(user => user.SecurityStamp.IsNullOrWhiteSpace() ? Guid.NewGuid().ToString("N") : user.SecurityStamp));
|
2015-11-26 13:07:22 +01:00
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
2013-08-26 11:26:58 +10:00
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
private static int GetIntId(object id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = id.TryConvertTo<int>();
|
|
|
|
|
|
if (result.Success == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
|
"Cannot convert the profile to a " + typeof(UserDetail).Name + " object since the id is not an integer");
|
|
|
|
|
|
}
|
|
|
|
|
|
return result.Result;
|
|
|
|
|
|
}
|
2013-08-26 11:26:58 +10:00
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
2013-06-17 01:06:31 +02:00
|
|
|
|
}
|