2013-08-12 15:06:12 +02:00
using System ;
2017-06-05 23:25:33 +02:00
using System.Collections.Generic ;
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-06-28 23:38:42 +10:00
using umbraco.BusinessLogic.Actions ;
2017-05-26 02:15:37 +10:00
using Umbraco.Core.IO ;
2015-03-24 20:17:37 +11:00
using Umbraco.Core.Models ;
2017-06-05 23:25:33 +02:00
using Umbraco.Core.Models.EntityBase ;
2015-03-24 20:17:37 +11:00
using Umbraco.Core.Models.Identity ;
using Umbraco.Core.Security ;
2017-06-06 02:05:17 +02:00
using Umbraco.Core.Services ;
using UserProfile = Umbraco . Web . Models . ContentEditing . UserProfile ;
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-06-06 02:05:17 +02:00
{
2017-06-20 15:10:42 +10:00
2017-06-20 14:33:24 +10:00
config . CreateMap < UserGroupSave , IUserGroup >()
2017-06-20 15:10:42 +10:00
. ConstructUsing (( UserGroupSave save ) => new UserGroup () { CreateDate = DateTime . Now })
2017-06-29 14:39:11 +10:00
. IgnoreDeletableEntityCommonProperties ()
. ForMember ( dest => dest . Id , map => map . Condition ( source => GetIntId ( source . Id ) > 0 ))
2017-07-17 18:23:20 +10:00
. ForMember ( dest => dest . Id , map => map . MapFrom ( source => GetIntId ( source . Id )))
2017-07-05 19:25:48 +10:00
. ForMember ( dest => dest . Permissions , map => map . MapFrom ( source => source . DefaultPermissions ))
2017-06-20 14:33:24 +10:00
. AfterMap (( save , userGroup ) =>
{
userGroup . ClearAllowedSections ();
foreach ( var section in save . Sections )
2017-06-29 01:00:52 +10:00
{
2017-06-20 14:33:24 +10:00
userGroup . AddAllowedSection ( section );
}
2017-06-29 01:00:52 +10:00
});
2017-06-20 14:33:24 +10:00
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 >()
2017-06-29 14:39:11 +10:00
. IgnoreDeletableEntityCommonProperties ()
2017-07-06 13:14:24 +02:00
. ForMember ( dest => dest . Id , map => map . Condition ( source => GetIntId ( source . Id ) > 0 ))
. ForMember ( detail => detail . SessionTimeout , opt => opt . Ignore ())
. ForMember ( detail => detail . EmailConfirmedDate , opt => opt . Ignore ())
. ForMember ( detail => detail . InvitedDate , opt => opt . Ignore ())
. ForMember ( detail => detail . SecurityStamp , opt => opt . Ignore ())
. ForMember ( detail => detail . Avatar , opt => opt . Ignore ())
. ForMember ( detail => detail . ProviderUserKey , opt => opt . Ignore ())
. ForMember ( detail => detail . RawPasswordValue , opt => opt . Ignore ())
. ForMember ( detail => detail . RawPasswordAnswerValue , opt => opt . Ignore ())
. ForMember ( detail => detail . PasswordQuestion , opt => opt . Ignore ())
. ForMember ( detail => detail . Comments , opt => opt . Ignore ())
. ForMember ( detail => detail . IsApproved , opt => opt . Ignore ())
. ForMember ( detail => detail . IsLockedOut , opt => opt . Ignore ())
. ForMember ( detail => detail . LastLoginDate , opt => opt . Ignore ())
. ForMember ( detail => detail . LastPasswordChangeDate , opt => opt . Ignore ())
. ForMember ( detail => detail . LastLockoutDate , opt => opt . Ignore ())
. ForMember ( detail => detail . FailedPasswordAttempts , opt => opt . Ignore ())
2017-05-25 12:17:32 +10:00
. ForMember ( user => user . Language , expression => expression . MapFrom ( save => save . Culture ))
. AfterMap (( save , user ) =>
2017-06-06 02:05:17 +02:00
{
2017-05-25 12:17:32 +10:00
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-06-06 02:05:17 +02:00
});
2017-05-25 12:17:32 +10:00
2017-06-29 14:39:11 +10:00
config . CreateMap < UserInvite , IUser >()
. IgnoreDeletableEntityCommonProperties ()
. ForMember ( detail => detail . Id , opt => opt . Ignore ())
. ForMember ( detail => detail . StartContentIds , opt => opt . Ignore ())
. ForMember ( detail => detail . StartMediaIds , opt => opt . Ignore ())
. ForMember ( detail => detail . Language , opt => opt . Ignore ())
. ForMember ( detail => detail . Username , opt => opt . Ignore ())
. ForMember ( detail => detail . PasswordQuestion , opt => opt . Ignore ())
. ForMember ( detail => detail . SessionTimeout , opt => opt . Ignore ())
. ForMember ( detail => detail . EmailConfirmedDate , opt => opt . Ignore ())
. ForMember ( detail => detail . InvitedDate , opt => opt . Ignore ())
. ForMember ( detail => detail . SecurityStamp , opt => opt . Ignore ())
. ForMember ( detail => detail . Avatar , opt => opt . Ignore ())
. ForMember ( detail => detail . ProviderUserKey , opt => opt . Ignore ())
. ForMember ( detail => detail . RawPasswordValue , opt => opt . Ignore ())
. ForMember ( detail => detail . RawPasswordAnswerValue , opt => opt . Ignore ())
. ForMember ( detail => detail . Comments , opt => opt . Ignore ())
. ForMember ( detail => detail . IsApproved , opt => opt . Ignore ())
. ForMember ( detail => detail . IsLockedOut , opt => opt . Ignore ())
. ForMember ( detail => detail . LastLoginDate , opt => opt . Ignore ())
. ForMember ( detail => detail . LastPasswordChangeDate , opt => opt . Ignore ())
. ForMember ( detail => detail . LastLockoutDate , opt => opt . Ignore ())
2017-07-06 13:14:24 +02:00
. ForMember ( detail => detail . FailedPasswordAttempts , opt => opt . Ignore ())
//all invited users will not be approved, completing the invite will approve the user
2017-06-13 18:38:16 +02:00
. ForMember ( user => user . IsApproved , expression => expression . UseValue ( false ))
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-06-06 00:02:23 +02:00
});
2017-05-24 19:01:01 +10:00
2017-06-06 02:05:17 +02:00
config . CreateMap < IReadOnlyUserGroup , UserGroupBasic >()
2017-07-06 14:41:05 +02:00
. ForMember ( detail => detail . ContentStartNode , opt => opt . Ignore ())
2017-06-20 13:19:14 +10:00
. ForMember ( detail => detail . UserCount , opt => opt . Ignore ())
2017-07-06 14:41:05 +02:00
. ForMember ( detail => detail . MediaStartNode , opt => opt . Ignore ())
2017-06-06 00:13:46 +02:00
. ForMember ( detail => detail . Key , opt => opt . Ignore ())
2017-06-06 00:02:23 +02:00
. ForMember ( detail => detail . Sections , opt => opt . Ignore ())
. 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 ))
2017-06-06 00:02:23 +02:00
. ForMember ( detail => detail . Path , opt => opt . MapFrom ( userGroup => "-1," + userGroup . Id ))
. ForMember ( detail => detail . AdditionalData , opt => opt . Ignore ())
. AfterMap (( group , display ) =>
{
2017-06-06 02:05:17 +02:00
MapUserGroupBasic ( applicationContext . Services , group , display );
});
2017-06-06 00:02:23 +02:00
2017-06-06 02:05:17 +02:00
config . CreateMap < IUserGroup , UserGroupBasic >()
2017-07-06 14:41:05 +02:00
. ForMember ( detail => detail . ContentStartNode , opt => opt . Ignore ())
. ForMember ( detail => detail . MediaStartNode , opt => opt . Ignore ())
2017-06-06 00:02:23 +02:00
. ForMember ( detail => detail . Sections , opt => opt . Ignore ())
. ForMember ( detail => detail . Notifications , opt => opt . Ignore ())
. 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 ( userGroup => "-1," + userGroup . Id ))
. ForMember ( detail => detail . AdditionalData , opt => opt . Ignore ())
. AfterMap (( group , display ) =>
{
2017-06-06 02:05:17 +02:00
MapUserGroupBasic ( applicationContext . Services , group , display );
});
2017-06-06 00:02:23 +02:00
2017-06-29 01:00:52 +10:00
//create a map to assign a user group's default permissions to the AssignedUserGroupPermissions instance
config . CreateMap < IUserGroup , AssignedUserGroupPermissions >()
. ForMember ( detail => detail . Udi , opt => opt . Ignore ())
2017-06-30 16:32:01 +10:00
. ForMember ( detail => detail . Trashed , opt => opt . Ignore ())
2017-06-29 01:00:52 +10:00
. ForMember ( detail => detail . AdditionalData , opt => opt . Ignore ())
. ForMember ( detail => detail . Id , opt => opt . MapFrom ( group => group . Id ))
. ForMember ( detail => detail . ParentId , opt => opt . UseValue (- 1 ))
. ForMember ( detail => detail . Path , opt => opt . MapFrom ( userGroup => "-1," + userGroup . Id ))
2017-07-04 15:07:11 +10:00
. ForMember ( detail => detail . DefaultPermissions , expression => expression . ResolveUsing ( new UserGroupDefaultPermissionsResolver ( applicationContext . Services . TextService )))
2017-06-30 16:32:01 +10:00
//these will be manually mapped and by default they are null
. ForMember ( detail => detail . AssignedPermissions , opt => opt . Ignore ())
2017-06-29 01:00:52 +10:00
. AfterMap (( group , display ) =>
{
if ( display . Icon . IsNullOrWhiteSpace ())
{
display . Icon = "icon-users" ;
}
});
2017-07-04 15:07:11 +10:00
config . CreateMap < UmbracoEntity , AssignedContentPermissions >()
. ForMember ( x => x . Udi , expression => expression . MapFrom ( x => Udi . Create ( UmbracoObjectTypesExtensions . GetUdiType ( x . NodeObjectTypeId ), x . Key )))
. ForMember ( basic => basic . Icon , expression => expression . MapFrom ( entity => entity . ContentTypeIcon ))
. ForMember ( dto => dto . Trashed , expression => expression . Ignore ())
. ForMember ( x => x . Alias , expression => expression . Ignore ())
2017-07-04 15:40:33 +10:00
. ForMember ( x => x . AssignedPermissions , expression => expression . Ignore ())
2017-07-04 15:07:11 +10:00
. AfterMap (( entity , basic ) =>
{
if ( entity . NodeObjectTypeId == Constants . ObjectTypes . MemberGuid && basic . Icon . IsNullOrWhiteSpace ())
{
basic . Icon = "icon-user" ;
}
2017-07-06 13:14:24 +02:00
});
2017-07-04 15:07:11 +10:00
2017-06-06 02:05:17 +02:00
config . CreateMap < IUserGroup , UserGroupDisplay >()
2017-07-06 14:41:05 +02:00
. ForMember ( detail => detail . ContentStartNode , opt => opt . Ignore ())
. ForMember ( detail => detail . MediaStartNode , opt => opt . Ignore ())
2017-06-06 02:05:17 +02:00
. ForMember ( detail => detail . Sections , opt => opt . Ignore ())
. ForMember ( detail => detail . Notifications , opt => opt . Ignore ())
. 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 ( userGroup => "-1," + userGroup . Id ))
. ForMember ( detail => detail . AdditionalData , opt => opt . Ignore ())
2017-06-29 01:00:52 +10:00
. ForMember ( detail => detail . Users , opt => opt . Ignore ())
2017-07-04 15:07:11 +10:00
. ForMember ( detail => detail . DefaultPermissions , expression => expression . ResolveUsing ( new UserGroupDefaultPermissionsResolver ( applicationContext . Services . TextService )))
2017-07-04 15:40:33 +10:00
. ForMember ( detail => detail . AssignedPermissions , opt => opt . Ignore ())
2017-06-06 02:05:17 +02:00
. AfterMap (( group , display ) =>
{
MapUserGroupBasic ( applicationContext . Services , group , display );
2017-06-29 12:38:33 +10:00
2017-07-06 13:14:24 +02:00
//Important! Currently we are never mapping to multiple UserGroupDisplay objects but if we start doing that
2017-06-29 12:38:33 +10:00
// this will cause an N+1 and we'll need to change how this works.
2017-06-06 02:05:17 +02:00
var users = applicationContext . Services . UserService . GetAllInGroup ( group . Id );
display . Users = Mapper . Map < IEnumerable < UserBasic >>( users );
2017-07-06 13:14:24 +02:00
//Deal with assigned permissions:
2017-07-04 15:40:33 +10:00
var allContentPermissions = applicationContext . Services . UserService . GetPermissions ( group , true )
. ToDictionary ( x => x . EntityId , x => x );
2017-07-05 18:23:51 +10:00
2017-07-06 13:14:24 +02:00
var contentEntities = allContentPermissions . Keys . Count == 0
? new IUmbracoEntity [ 0 ]
: applicationContext . Services . EntityService . GetAll ( UmbracoObjectTypes . Document , allContentPermissions . Keys . ToArray ());
2017-07-04 15:40:33 +10:00
var allAssignedPermissions = new List < AssignedContentPermissions >();
foreach ( var entity in contentEntities )
{
var contentPermissions = allContentPermissions [ entity . Id ];
var assignedContentPermissions = Mapper . Map < AssignedContentPermissions >( entity );
assignedContentPermissions . AssignedPermissions = AssignedUserGroupPermissions . ClonePermissions ( display . DefaultPermissions );
//since there is custom permissions assigned to this node for this group, we need to clear all of the default permissions
//and we'll re-check it if it's one of the explicitly assigned ones
foreach ( var permission in assignedContentPermissions . AssignedPermissions . SelectMany ( x => x . Value ))
{
permission . Checked = false ;
permission . Checked = contentPermissions . AssignedPermissions . Contains ( permission . PermissionCode , StringComparer . InvariantCulture );
}
allAssignedPermissions . Add ( assignedContentPermissions );
}
display . AssignedPermissions = allAssignedPermissions ;
2017-06-06 00:02:23 +02:00
});
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-06-05 23:25:33 +02:00
. ForMember ( detail => detail . LastLoginDate , opt => opt . MapFrom ( user => user . LastLoginDate == default ( DateTime ) ? null : ( DateTime ?) user . LastLoginDate ))
2017-06-29 13:13:32 +10:00
. ForMember ( detail => detail . UserGroups , opt => opt . MapFrom ( user => user . Groups ))
2017-06-06 01:06:19 +02:00
. ForMember ( detail => detail . StartContentIds , opt => opt . UseValue ( Enumerable . Empty < EntityBasic >()))
. ForMember ( detail => detail . StartMediaIds , opt => opt . UseValue ( Enumerable . Empty < EntityBasic >()))
2017-06-05 23:25:33 +02:00
. ForMember ( detail => detail . Culture , opt => opt . MapFrom ( user => user . GetUserCulture ( applicationContext . Services . TextService )))
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 ,
2017-06-14 22:30:36 +02:00
opt => opt . MapFrom ( user => user . Email . ToLowerInvariant (). Trim (). GenerateHash ()))
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 ())
2017-06-29 01:00:52 +10:00
. ForMember ( detail => detail . IsCurrentUser , opt => opt . Ignore ())
2017-06-05 23:25:33 +02:00
. ForMember ( detail => detail . Trashed , opt => opt . Ignore ())
2017-06-23 14:58:00 +10:00
. ForMember ( detail => detail . ResetPasswordValue , opt => opt . Ignore ())
2017-06-05 23:25:33 +02:00
. ForMember ( detail => detail . Alias , opt => opt . Ignore ())
. ForMember ( detail => detail . Trashed , opt => opt . Ignore ())
. ForMember ( detail => detail . AdditionalData , opt => opt . Ignore ())
. AfterMap (( user , display ) =>
2017-06-29 13:13:32 +10:00
{
//Important! Currently we are never mapping to multiple UserDisplay objects but if we start doing that
// this will cause an N+1 and we'll need to change how this works.
2017-06-06 00:49:36 +02:00
var startContentIds = user . StartContentIds . ToArray ();
if ( startContentIds . Length > 0 )
2017-06-29 13:13:32 +10:00
{
//TODO: Update GetAll to be able to pass in a parameter like on the normal Get to NOT load in the entire object!
2017-06-06 00:49:36 +02:00
var contentItems = applicationContext . Services . EntityService . GetAll ( UmbracoObjectTypes . Document , startContentIds );
display . StartContentIds = Mapper . Map < IEnumerable < IUmbracoEntity >, IEnumerable < EntityBasic >>( contentItems );
}
2017-07-06 12:48:47 +02:00
var startMediaIds = user . StartMediaIds . ToArray ();
2017-06-06 00:49:36 +02:00
if ( startMediaIds . Length > 0 )
{
2017-07-06 12:48:47 +02:00
var mediaItems = applicationContext . Services . EntityService . GetAll ( UmbracoObjectTypes . Media , startMediaIds );
2017-06-06 02:05:17 +02:00
display . StartMediaIds = Mapper . Map < IEnumerable < IUmbracoEntity >, IEnumerable < EntityBasic >>( mediaItems );
2017-06-06 00:49:36 +02:00
}
2017-06-06 02:05:17 +02:00
});
config . CreateMap < IUser , UserBasic >()
2017-06-29 13:13:32 +10:00
//Loading in the user avatar's requires an external request if they don't have a local file avatar, this means that initial load of paging may incur a cost
//Alternatively, if this is annoying the back office UI would need to be updated to request the avatars for the list of users separately so it doesn't look
//like the load time is waiting.
. ForMember ( detail =>
detail . Avatars ,
opt => opt . MapFrom ( user => user . GetCurrentUserAvatarUrls ( applicationContext . Services . UserService , applicationContext . ApplicationCache . RuntimeCache )))
2017-06-06 02:05:17 +02:00
. ForMember ( detail => detail . Username , opt => opt . MapFrom ( user => user . Username ))
2017-06-29 13:13:32 +10:00
. ForMember ( detail => detail . UserGroups , opt => opt . MapFrom ( user => user . Groups ))
2017-06-06 02:05:17 +02:00
. ForMember ( detail => detail . LastLoginDate , opt => opt . MapFrom ( user => user . LastLoginDate == default ( DateTime ) ? null : ( DateTime ?) user . LastLoginDate ))
. 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 . ParentId , opt => opt . UseValue (- 1 ))
. ForMember ( detail => detail . Path , opt => opt . MapFrom ( user => "-1," + user . Id ))
. ForMember ( detail => detail . Notifications , opt => opt . Ignore ())
2017-06-29 13:13:32 +10:00
. ForMember ( detail => detail . IsCurrentUser , opt => opt . Ignore ())
2017-06-06 02:05:17 +02:00
. 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-07-17 18:23:20 +10:00
. ForMember ( detail => detail . StartContentIds , opt => opt . MapFrom ( user => user . AllStartContentIds ))
. ForMember ( detail => detail . StartMediaIds , opt => opt . MapFrom ( user => user . AllStartMediaIds ))
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 ,
2017-05-10 13:35:28 -07:00
opt => opt . MapFrom ( user => user . Email . ToLowerInvariant (). Trim (). GenerateHash ()))
2017-05-26 11:17:06 +10:00
. ForMember ( detail => detail . SecondsUntilTimeout , opt => opt . Ignore ());
2013-10-15 18:46:44 +11:00
2017-06-06 02:05:17 +02:00
config . CreateMap < IProfile , UserProfile >()
2013-08-12 15:06:12 +02:00
. 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-07-17 18:23:20 +10:00
. ForMember ( detail => detail . StartContentNodes , opt => opt . MapFrom ( user => user . AllStartContentIds ))
. ForMember ( detail => detail . StartMediaNodes , opt => opt . MapFrom ( user => user . AllStartMediaIds ))
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
2017-06-06 02:05:17 +02:00
}
private void MapUserGroupBasic ( ServiceContext services , dynamic group , UserGroupBasic display )
{
var allSections = services . SectionService . GetSections ();
2017-06-13 15:47:39 +02:00
display . Sections = allSections . Where ( x => Enumerable . Contains ( group . AllowedSections , x . Alias )). Select ( Mapper . Map < ContentEditing . Section >);
2017-07-17 18:23:20 +10:00
2017-06-06 02:05:17 +02:00
if ( group . StartMediaId > 0 )
{
2017-07-06 14:41:05 +02:00
display . MediaStartNode = Mapper . Map < EntityBasic >(
2017-06-20 14:33:24 +10:00
services . EntityService . Get ( group . StartMediaId , UmbracoObjectTypes . Media ));
2017-06-06 02:05:17 +02:00
}
2017-07-17 18:23:20 +10:00
else if ( group . StartMediaId == - 1 )
{
//create the root node
2017-07-17 19:40:45 +10:00
display . MediaStartNode = RootNode ( "Media Root" , "icon-picture" );
2017-07-17 18:23:20 +10:00
}
2017-06-06 02:05:17 +02:00
if ( group . StartContentId > 0 )
{
2017-07-06 14:41:05 +02:00
display . ContentStartNode = Mapper . Map < EntityBasic >(
2017-06-20 14:33:24 +10:00
services . EntityService . Get ( group . StartContentId , UmbracoObjectTypes . Document ));
2017-06-06 02:05:17 +02:00
}
2017-07-17 18:23:20 +10:00
else if ( group . StartContentId == - 1 )
{
//create the root node
2017-07-17 19:40:45 +10:00
display . ContentStartNode = RootNode ( "Content Root" , "icon-document" );
2017-07-17 18:23:20 +10:00
}
2017-06-06 02:05:17 +02:00
if ( display . Icon . IsNullOrWhiteSpace ())
{
display . Icon = "icon-users" ;
}
2017-06-14 22:30:36 +02:00
}
2017-06-06 02:05:17 +02:00
2017-07-17 19:40:45 +10:00
private EntityBasic RootNode ( string name , string icon )
2017-07-17 18:23:20 +10:00
{
return new EntityBasic
{
2017-07-17 19:40:45 +10:00
Name = name ,
2017-07-17 18:23:20 +10:00
Path = "-1" ,
2017-07-17 19:40:45 +10:00
Icon = icon ,
2017-07-17 18:23:20 +10:00
Id = - 1 ,
Trashed = false ,
ParentId = - 1
};
}
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
}