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;
|
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-24 19:01:01 +10:00
|
|
|
|
config.CreateMap<UserInvite, IUser>()
|
|
|
|
|
|
.ConstructUsing(invite => new User(invite.Name, invite.Email, invite.Email, Guid.NewGuid().ToString("N")))
|
|
|
|
|
|
.AfterMap((invite, user) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var group in invite.UserGroups)
|
|
|
|
|
|
{
|
|
|
|
|
|
user.AddGroup(group);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-05-23 15:48:08 +10:00
|
|
|
|
config.CreateMap<IUserGroup, UserGroupDisplay>()
|
|
|
|
|
|
.ForMember(detail => detail.Notifications, opt => opt.Ignore())
|
|
|
|
|
|
.ForMember(detail => detail.Sections, opt => opt.MapFrom(x => x.AllowedSections))
|
|
|
|
|
|
.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
|
|
|
|
|
|
|
|
|
|
config.CreateMap<IUser, UserDisplay>()
|
2017-05-16 18:18:10 +10:00
|
|
|
|
.ForMember(detail => detail.UserGroups, opt => opt.MapFrom(user => user.Groups))
|
2017-05-11 13:11:41 +10:00
|
|
|
|
.ForMember(detail => detail.StartContentId, opt => opt.MapFrom(user => user.StartContentId))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaId, opt => opt.MapFrom(user => user.StartMediaId))
|
|
|
|
|
|
.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,
|
|
|
|
|
|
opt => opt.MapFrom(user => applicationContext.Services.UserService.GetAllUserGroups().ToDictionary(x => x.Alias, x => x.Name)))
|
|
|
|
|
|
.ForMember(
|
|
|
|
|
|
detail => detail.AvailableSections,
|
|
|
|
|
|
opt => opt.MapFrom(user => applicationContext.Services.SectionService.GetSections().ToDictionary(x => x.Alias, x => x.Name)))
|
|
|
|
|
|
.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())
|
2017-05-22 21:23:04 +10:00
|
|
|
|
//TODO: Enable this when we can!
|
|
|
|
|
|
.ForMember(detail => detail.CustomAvatar, opt => opt.Ignore())
|
2017-05-16 18:18:10 +10:00
|
|
|
|
.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>()
|
2014-04-14 15:20:02 +10:00
|
|
|
|
.ForMember(detail => detail.UserId, opt => opt.MapFrom(user => GetIntId(user.Id)))
|
2014-10-07 14:59:07 +11:00
|
|
|
|
.ForMember(detail => detail.StartContentId, opt => opt.MapFrom(user => user.StartContentId))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaId, opt => opt.MapFrom(user => user.StartMediaId))
|
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()))
|
|
|
|
|
|
.ForMember(detail => detail.SecondsUntilTimeout, opt => opt.Ignore());
|
|
|
|
|
|
|
|
|
|
|
|
config.CreateMap<BackOfficeIdentityUser, UserDetail>()
|
|
|
|
|
|
.ForMember(detail => detail.UserId, opt => opt.MapFrom(user => user.Id))
|
|
|
|
|
|
.ForMember(detail => detail.StartContentId, opt => opt.MapFrom(user => user.StartContentId))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaId, opt => opt.MapFrom(user => user.StartMediaId))
|
|
|
|
|
|
.ForMember(detail => detail.Culture, opt => opt.MapFrom(user => user.Culture))
|
|
|
|
|
|
.ForMember(detail => detail.AllowedSections, opt => opt.MapFrom(user => user.AllowedSections))
|
2014-04-14 15:20:02 +10:00
|
|
|
|
.ForMember(
|
|
|
|
|
|
detail => detail.EmailHash,
|
|
|
|
|
|
opt => opt.MapFrom(user => user.Email.ToLowerInvariant().Trim().ToMd5()))
|
|
|
|
|
|
.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()))
|
2015-03-24 20:17:37 +11:00
|
|
|
|
.ForMember(detail => detail.StartContentNode, opt => opt.MapFrom(user => user.StartContentId))
|
|
|
|
|
|
.ForMember(detail => detail.StartMediaNode, opt => opt.MapFrom(user => user.StartMediaId))
|
|
|
|
|
|
.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
|
|
|
|
}
|