2017-07-19 13:42:47 +02:00
using System ;
2017-09-12 16:22:16 +02:00
using System.Collections.Generic ;
2019-03-24 12:01:23 +01:00
using System.Globalization ;
2017-09-12 16:22:16 +02:00
using System.Linq ;
2017-07-19 13:42:47 +02:00
using Umbraco.Core ;
2017-09-12 16:22:16 +02:00
using Umbraco.Core.Cache ;
2018-04-06 13:51:54 +10:00
using Umbraco.Core.Configuration ;
2019-12-18 13:05:34 +01:00
using Umbraco.Core.IO ;
2019-03-24 12:01:23 +01:00
using Umbraco.Core.Mapping ;
2017-07-19 13:42:47 +02:00
using Umbraco.Core.Models.Membership ;
using Umbraco.Web.Models.ContentEditing ;
using Umbraco.Core.Models ;
2018-01-15 11:32:30 +01:00
using Umbraco.Core.Models.Entities ;
2019-04-08 10:05:21 +02:00
using Umbraco.Core.Models.Sections ;
2017-07-19 13:42:47 +02:00
using Umbraco.Core.Services ;
2019-12-20 17:36:44 +01:00
using Umbraco.Core.Strings ;
2018-10-29 17:27:33 +11:00
using Umbraco.Web.Actions ;
2019-01-17 13:20:19 +11:00
using Umbraco.Web.Services ;
2020-03-24 09:37:46 +01:00
using Umbraco.Core.Media ;
2018-10-29 17:27:33 +11:00
2017-07-19 13:42:47 +02:00
namespace Umbraco.Web.Models.Mapping
{
2020-02-17 09:15:48 +01:00
public class UserMapDefinition : IMapDefinition
2017-07-19 13:42:47 +02:00
{
2019-03-24 12:01:23 +01:00
private readonly ISectionService _sectionService ;
private readonly IEntityService _entityService ;
private readonly IUserService _userService ;
private readonly ILocalizedTextService _textService ;
private readonly ActionCollection _actions ;
private readonly AppCaches _appCaches ;
private readonly IGlobalSettings _globalSettings ;
2019-12-18 13:05:34 +01:00
private readonly IMediaFileSystem _mediaFileSystem ;
2019-12-20 17:36:44 +01:00
private readonly IShortStringHelper _shortStringHelper ;
2020-02-11 11:43:54 -08:00
private readonly IImageUrlGenerator _imageUrlGenerator ;
2018-01-15 11:32:30 +01:00
2019-04-03 10:39:49 +02:00
public UserMapDefinition ( ILocalizedTextService textService , IUserService userService , IEntityService entityService , ISectionService sectionService ,
2020-02-11 11:43:54 -08:00
AppCaches appCaches , ActionCollection actions , IGlobalSettings globalSettings , IMediaFileSystem mediaFileSystem , IShortStringHelper shortStringHelper ,
IImageUrlGenerator imageUrlGenerator )
2017-07-19 13:42:47 +02:00
{
2019-03-24 12:01:23 +01:00
_sectionService = sectionService ;
_entityService = entityService ;
_userService = userService ;
_textService = textService ;
_actions = actions ;
_appCaches = appCaches ;
_globalSettings = globalSettings ;
2019-12-18 13:05:34 +01:00
_mediaFileSystem = mediaFileSystem ;
2019-12-20 17:36:44 +01:00
_shortStringHelper = shortStringHelper ;
2020-02-11 11:43:54 -08:00
_imageUrlGenerator = imageUrlGenerator ;
2019-03-24 12:01:23 +01:00
}
2019-04-03 10:39:49 +02:00
public void DefineMaps ( UmbracoMapper mapper )
2019-03-24 12:01:23 +01:00
{
2019-12-20 17:36:44 +01:00
mapper . Define < UserGroupSave , IUserGroup > ( ( source , context ) = > new UserGroup ( _shortStringHelper ) { CreateDate = DateTime . UtcNow } , Map ) ;
2019-03-25 09:03:46 +01:00
mapper . Define < UserInvite , IUser > ( Map ) ;
2019-03-26 18:47:35 +01:00
mapper . Define < IProfile , ContentEditing . UserProfile > ( ( source , context ) = > new ContentEditing . UserProfile ( ) , Map ) ;
2019-03-26 10:39:50 +01:00
mapper . Define < IReadOnlyUserGroup , UserGroupBasic > ( ( source , context ) = > new UserGroupBasic ( ) , Map ) ;
mapper . Define < IUserGroup , UserGroupBasic > ( ( source , context ) = > new UserGroupBasic ( ) , Map ) ;
mapper . Define < IUserGroup , AssignedUserGroupPermissions > ( ( source , context ) = > new AssignedUserGroupPermissions ( ) , Map ) ;
mapper . Define < EntitySlim , AssignedContentPermissions > ( ( source , context ) = > new AssignedContentPermissions ( ) , Map ) ;
mapper . Define < IUserGroup , UserGroupDisplay > ( ( source , context ) = > new UserGroupDisplay ( ) , Map ) ;
mapper . Define < IUser , UserBasic > ( ( source , context ) = > new UserBasic ( ) , Map ) ;
mapper . Define < IUser , UserDetail > ( ( source , context ) = > new UserDetail ( ) , Map ) ;
2019-03-24 12:01:23 +01:00
// used for merging existing UserSave to an existing IUser instance - this will not create an IUser instance!
2019-03-25 09:03:46 +01:00
mapper . Define < UserSave , IUser > ( Map ) ;
2019-03-24 12:01:23 +01:00
// important! Currently we are never mapping to multiple UserDisplay objects but if we start doing that
2017-09-19 15:51:47 +02:00
// this will cause an N+1 and we'll need to change how this works.
2019-03-26 10:39:50 +01:00
mapper . Define < IUser , UserDisplay > ( ( source , context ) = > new UserDisplay ( ) , Map ) ;
2019-03-24 12:01:23 +01:00
}
2017-09-19 15:51:47 +02:00
2019-03-24 12:01:23 +01:00
// mappers
2017-09-12 16:22:16 +02:00
2019-03-26 10:39:50 +01:00
private static void Map ( UserGroupSave source , IUserGroup target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
if ( ! ( target is UserGroup ttarget ) )
throw new NotSupportedException ( $"{nameof(target)} must be a UserGroup." ) ;
Map ( source , ttarget ) ;
2018-03-27 10:04:07 +02:00
}
2017-07-19 13:42:47 +02:00
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
private static void Map ( UserGroupSave source , UserGroup target )
2018-03-27 10:04:07 +02:00
{
2019-03-24 12:01:23 +01:00
target . StartMediaId = source . StartMediaId ;
target . StartContentId = source . StartContentId ;
target . Icon = source . Icon ;
target . Alias = source . Alias ;
target . Name = source . Name ;
target . Permissions = source . DefaultPermissions ;
target . Key = source . Key ;
var id = GetIntId ( source . Id ) ;
if ( id > 0 )
target . Id = id ;
target . ClearAllowedSections ( ) ;
foreach ( var section in source . Sections )
target . AddAllowedSection ( section ) ;
2017-09-12 16:22:16 +02:00
}
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
// Umbraco.Code.MapAll -Id -TourData -StartContentIds -StartMediaIds -Language -Username
// Umbraco.Code.MapAll -PasswordQuestion -SessionTimeout -EmailConfirmedDate -InvitedDate
// Umbraco.Code.MapAll -SecurityStamp -Avatar -ProviderUserKey -RawPasswordValue
// Umbraco.Code.MapAll -RawPasswordAnswerValue -Comments -IsApproved -IsLockedOut -LastLoginDate
// Umbraco.Code.MapAll -LastPasswordChangeDate -LastLockoutDate -FailedPasswordAttempts
2019-03-26 10:39:50 +01:00
private void Map ( UserInvite source , IUser target , MapperContext context )
2017-09-19 15:51:47 +02:00
{
2019-03-24 12:01:23 +01:00
target . Email = source . Email ;
target . Key = source . Key ;
target . Name = source . Name ;
target . IsApproved = false ;
target . ClearGroups ( ) ;
var groups = _userService . GetUserGroupsByAlias ( source . UserGroups . ToArray ( ) ) ;
foreach ( var group in groups )
target . AddGroup ( group . ToReadOnlyGroup ( ) ) ;
}
2017-09-19 15:51:47 +02:00
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll -CreateDate -UpdateDate -DeleteDate
// Umbraco.Code.MapAll -TourData -SessionTimeout -EmailConfirmedDate -InvitedDate -SecurityStamp -Avatar
// Umbraco.Code.MapAll -ProviderUserKey -RawPasswordValue -RawPasswordAnswerValue -PasswordQuestion -Comments
// Umbraco.Code.MapAll -IsApproved -IsLockedOut -LastLoginDate -LastPasswordChangeDate -LastLockoutDate
// Umbraco.Code.MapAll -FailedPasswordAttempts
2019-03-26 10:39:50 +01:00
private void Map ( UserSave source , IUser target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
target . Name = source . Name ;
2019-03-26 18:47:35 +01:00
target . StartContentIds = source . StartContentIds ? ? Array . Empty < int > ( ) ;
target . StartMediaIds = source . StartMediaIds ? ? Array . Empty < int > ( ) ;
2019-03-24 12:01:23 +01:00
target . Language = source . Culture ;
target . Email = source . Email ;
target . Key = source . Key ;
target . Username = source . Username ;
target . Id = source . Id ;
target . ClearGroups ( ) ;
var groups = _userService . GetUserGroupsByAlias ( source . UserGroups . ToArray ( ) ) ;
foreach ( var group in groups )
target . AddGroup ( group . ToReadOnlyGroup ( ) ) ;
}
2017-09-19 15:51:47 +02:00
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll
2019-03-26 10:39:50 +01:00
private static void Map ( IProfile source , ContentEditing . UserProfile target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
target . Name = source . Name ;
target . UserId = source . Id ;
2017-09-19 15:51:47 +02:00
}
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll -ContentStartNode -UserCount -MediaStartNode -Key -Sections
2019-09-26 16:32:58 +02:00
// Umbraco.Code.MapAll -Notifications -Udi -Trashed -AdditionalData -IsSystemUserGroup
2019-03-26 10:39:50 +01:00
private void Map ( IReadOnlyUserGroup source , UserGroupBasic target , MapperContext context )
2017-09-12 16:22:16 +02:00
{
2019-03-24 12:01:23 +01:00
target . Alias = source . Alias ;
target . Icon = source . Icon ;
target . Id = source . Id ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
2019-09-26 18:39:54 +02:00
target . IsSystemUserGroup = source . IsSystemUserGroup ( ) ;
MapUserGroupBasic ( target , source . AllowedSections , source . StartContentId , source . StartMediaId , context ) ;
2019-03-24 12:01:23 +01:00
}
2017-09-12 16:22:16 +02:00
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll -ContentStartNode -MediaStartNode -Sections -Notifications
2019-09-26 16:32:58 +02:00
// Umbraco.Code.MapAll -Udi -Trashed -AdditionalData -IsSystemUserGroup
2019-03-26 10:39:50 +01:00
private void Map ( IUserGroup source , UserGroupBasic target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
target . Alias = source . Alias ;
target . Icon = source . Icon ;
target . Id = source . Id ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
target . UserCount = source . UserCount ;
2019-09-26 18:39:54 +02:00
target . IsSystemUserGroup = source . IsSystemUserGroup ( ) ;
MapUserGroupBasic ( target , source . AllowedSections , source . StartContentId , source . StartMediaId , context ) ;
2019-03-24 12:01:23 +01:00
}
2019-04-06 18:35:07 +02:00
// Umbraco.Code.MapAll -Udi -Trashed -AdditionalData -AssignedPermissions
2019-03-26 10:39:50 +01:00
private void Map ( IUserGroup source , AssignedUserGroupPermissions target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
2019-04-06 08:18:40 +02:00
target . Id = source . Id ;
2019-03-24 12:01:23 +01:00
target . Alias = source . Alias ;
target . Icon = source . Icon ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
target . DefaultPermissions = MapUserGroupDefaultPermissions ( source ) ;
2017-07-20 11:21:28 +02:00
2019-03-24 12:01:23 +01:00
if ( target . Icon . IsNullOrWhiteSpace ( ) )
2019-06-07 17:02:55 +01:00
target . Icon = Constants . Icons . UserGroup ;
2019-03-24 12:01:23 +01:00
}
// Umbraco.Code.MapAll -Trashed -Alias -AssignedPermissions
2019-03-26 10:39:50 +01:00
private static void Map ( EntitySlim source , AssignedContentPermissions target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
target . Icon = MapContentTypeIcon ( source ) ;
target . Id = source . Id ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = source . ParentId ;
target . Path = source . Path ;
target . Udi = Udi . Create ( ObjectTypes . GetUdiType ( source . NodeObjectType ) , source . Key ) ;
if ( source . NodeObjectType = = Constants . ObjectTypes . Member & & target . Icon . IsNullOrWhiteSpace ( ) )
2019-06-07 17:02:55 +01:00
target . Icon = Constants . Icons . Member ;
2019-03-24 12:01:23 +01:00
}
// Umbraco.Code.MapAll -ContentStartNode -MediaStartNode -Sections -Notifications -Udi
2019-09-26 18:39:54 +02:00
// Umbraco.Code.MapAll -Trashed -AdditionalData -Users -AssignedPermissions
2019-03-26 10:39:50 +01:00
private void Map ( IUserGroup source , UserGroupDisplay target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
target . Alias = source . Alias ;
target . DefaultPermissions = MapUserGroupDefaultPermissions ( source ) ;
target . Icon = source . Icon ;
target . Id = source . Id ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
target . UserCount = source . UserCount ;
2019-09-26 18:39:54 +02:00
target . IsSystemUserGroup = source . IsSystemUserGroup ( ) ;
2019-03-24 12:01:23 +01:00
2019-09-26 18:39:54 +02:00
MapUserGroupBasic ( target , source . AllowedSections , source . StartContentId , source . StartMediaId , context ) ;
2019-03-24 12:01:23 +01:00
//Important! Currently we are never mapping to multiple UserGroupDisplay objects but if we start doing that
// this will cause an N+1 and we'll need to change how this works.
var users = _userService . GetAllInGroup ( source . Id ) ;
2019-04-08 16:38:18 +02:00
target . Users = context . MapEnumerable < IUser , UserBasic > ( users ) ;
2019-03-24 12:01:23 +01:00
//Deal with assigned permissions:
var allContentPermissions = _userService . GetPermissions ( source , true )
. ToDictionary ( x = > x . EntityId , x = > x ) ;
IEntitySlim [ ] contentEntities ;
if ( allContentPermissions . Keys . Count = = 0 )
2017-09-12 16:22:16 +02:00
{
2019-03-24 12:01:23 +01:00
contentEntities = Array . Empty < IEntitySlim > ( ) ;
2017-09-12 16:22:16 +02:00
}
2019-03-24 12:01:23 +01:00
else
2017-09-12 16:22:16 +02:00
{
2019-03-24 12:01:23 +01:00
// a group can end up with way more than 2000 assigned permissions,
// so we need to break them into groups in order to avoid breaking
// the entity service due to too many Sql parameters.
var list = new List < IEntitySlim > ( ) ;
foreach ( var idGroup in allContentPermissions . Keys . InGroupsOf ( 2000 ) )
list . AddRange ( _entityService . GetAll ( UmbracoObjectTypes . Document , idGroup . ToArray ( ) ) ) ;
contentEntities = list . ToArray ( ) ;
2017-09-12 16:22:16 +02:00
}
2019-03-24 12:01:23 +01:00
var allAssignedPermissions = new List < AssignedContentPermissions > ( ) ;
foreach ( var entity in contentEntities )
2017-09-12 16:22:16 +02:00
{
2019-03-24 12:01:23 +01:00
var contentPermissions = allContentPermissions [ entity . Id ] ;
2019-04-07 11:26:47 +02:00
var assignedContentPermissions = context . Map < AssignedContentPermissions > ( entity ) ;
2019-03-24 12:01:23 +01:00
assignedContentPermissions . AssignedPermissions = AssignedUserGroupPermissions . ClonePermissions ( target . 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 ) ;
2017-09-12 16:22:16 +02:00
}
2019-03-24 12:01:23 +01:00
target . AssignedPermissions = allAssignedPermissions ;
2017-09-12 16:22:16 +02:00
}
2019-03-24 12:01:23 +01:00
// Umbraco.Code.MapAll -Notifications -Udi -Icon -IsCurrentUser -Trashed -ResetPasswordValue
// Umbraco.Code.MapAll -Alias -AdditionalData
2019-03-26 10:39:50 +01:00
private void Map ( IUser source , UserDisplay target , MapperContext context )
2017-09-12 16:22:16 +02:00
{
2019-03-24 12:01:23 +01:00
target . AvailableCultures = _textService . GetSupportedCultures ( ) . ToDictionary ( x = > x . Name , x = > x . DisplayName ) ;
2020-02-11 11:43:54 -08:00
target . Avatars = source . GetUserAvatarUrls ( _appCaches . RuntimeCache , _mediaFileSystem , _imageUrlGenerator ) ;
2019-04-07 11:26:47 +02:00
target . CalculatedStartContentIds = GetStartNodes ( source . CalculateContentStartNodeIds ( _entityService ) , UmbracoObjectTypes . Document , "content/contentRoot" , context ) ;
target . CalculatedStartMediaIds = GetStartNodes ( source . CalculateMediaStartNodeIds ( _entityService ) , UmbracoObjectTypes . Media , "media/mediaRoot" , context ) ;
2019-03-24 12:01:23 +01:00
target . CreateDate = source . CreateDate ;
target . Culture = source . GetUserCulture ( _textService , _globalSettings ) . ToString ( ) ;
target . Email = source . Email ;
target . EmailHash = source . Email . ToLowerInvariant ( ) . Trim ( ) . GenerateHash ( ) ;
target . FailedPasswordAttempts = source . FailedPasswordAttempts ;
target . Id = source . Id ;
target . Key = source . Key ;
target . LastLockoutDate = source . LastLockoutDate ;
target . LastLoginDate = source . LastLoginDate = = default ? null : ( DateTime ? ) source . LastLoginDate ;
target . LastPasswordChangeDate = source . LastPasswordChangeDate ;
target . Name = source . Name ;
target . Navigation = CreateUserEditorNavigation ( ) ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
2019-04-07 11:26:47 +02:00
target . StartContentIds = GetStartNodes ( source . StartContentIds . ToArray ( ) , UmbracoObjectTypes . Document , "content/contentRoot" , context ) ;
target . StartMediaIds = GetStartNodes ( source . StartMediaIds . ToArray ( ) , UmbracoObjectTypes . Media , "media/mediaRoot" , context ) ;
2019-03-24 12:01:23 +01:00
target . UpdateDate = source . UpdateDate ;
2019-04-08 10:05:21 +02:00
target . UserGroups = context . MapEnumerable < IReadOnlyUserGroup , UserGroupBasic > ( source . Groups ) ;
2019-03-24 12:01:23 +01:00
target . Username = source . Username ;
target . UserState = source . UserState ;
}
// Umbraco.Code.MapAll -Notifications -IsCurrentUser -Udi -Icon -Trashed -Alias -AdditionalData
2019-03-26 10:39:50 +01:00
private void Map ( IUser source , UserBasic target , MapperContext context )
2019-03-24 12:01:23 +01: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.
2020-02-11 11:43:54 -08:00
target . Avatars = source . GetUserAvatarUrls ( _appCaches . RuntimeCache , _mediaFileSystem , _imageUrlGenerator ) ;
2019-03-24 12:01:23 +01:00
target . Culture = source . GetUserCulture ( _textService , _globalSettings ) . ToString ( ) ;
target . Email = source . Email ;
2019-08-07 10:31:08 -07:00
target . EmailHash = source . Email . ToLowerInvariant ( ) . Trim ( ) . GenerateHash ( ) ;
2019-03-24 12:01:23 +01:00
target . Id = source . Id ;
target . Key = source . Key ;
target . LastLoginDate = source . LastLoginDate = = default ? null : ( DateTime ? ) source . LastLoginDate ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
2019-04-08 10:05:21 +02:00
target . UserGroups = context . MapEnumerable < IReadOnlyUserGroup , UserGroupBasic > ( source . Groups ) ;
2019-03-24 12:01:23 +01:00
target . Username = source . Username ;
target . UserState = source . UserState ;
}
// Umbraco.Code.MapAll -SecondsUntilTimeout
2019-03-26 10:39:50 +01:00
private void Map ( IUser source , UserDetail target , MapperContext context )
2019-03-24 12:01:23 +01:00
{
target . AllowedSections = source . AllowedSections ;
2020-02-11 11:43:54 -08:00
target . Avatars = source . GetUserAvatarUrls ( _appCaches . RuntimeCache , _mediaFileSystem , _imageUrlGenerator ) ;
2019-03-24 12:01:23 +01:00
target . Culture = source . GetUserCulture ( _textService , _globalSettings ) . ToString ( ) ;
target . Email = source . Email ;
target . EmailHash = source . Email . ToLowerInvariant ( ) . Trim ( ) . GenerateHash ( ) ;
target . Name = source . Name ;
target . StartContentIds = source . CalculateContentStartNodeIds ( _entityService ) ;
target . StartMediaIds = source . CalculateMediaStartNodeIds ( _entityService ) ;
target . UserId = source . Id ;
//we need to map the legacy UserType
//the best we can do here is to return the user's first user group as a IUserType object
//but we should attempt to return any group that is the built in ones first
target . UserGroups = source . Groups . Select ( x = > x . Alias ) . ToArray ( ) ;
}
// helpers
2019-09-26 18:39:54 +02:00
private void MapUserGroupBasic ( UserGroupBasic target , IEnumerable < string > sourceAllowedSections , int? sourceStartContentId , int? sourceStartMediaId , MapperContext context )
2019-03-24 12:01:23 +01:00
{
var allSections = _sectionService . GetSections ( ) ;
2019-04-08 10:05:21 +02:00
target . Sections = context . MapEnumerable < ISection , Section > ( allSections . Where ( x = > sourceAllowedSections . Contains ( x . Alias ) ) ) ;
2019-03-24 12:01:23 +01:00
if ( sourceStartMediaId > 0 )
2019-04-07 11:26:47 +02:00
target . MediaStartNode = context . Map < EntityBasic > ( _entityService . Get ( sourceStartMediaId . Value , UmbracoObjectTypes . Media ) ) ;
2019-03-24 12:01:23 +01:00
else if ( sourceStartMediaId = = - 1 )
target . MediaStartNode = CreateRootNode ( _textService . Localize ( "media/mediaRoot" ) ) ;
if ( sourceStartContentId > 0 )
2019-04-07 11:26:47 +02:00
target . ContentStartNode = context . Map < EntityBasic > ( _entityService . Get ( sourceStartContentId . Value , UmbracoObjectTypes . Document ) ) ;
2019-03-24 12:01:23 +01:00
else if ( sourceStartContentId = = - 1 )
target . ContentStartNode = CreateRootNode ( _textService . Localize ( "content/contentRoot" ) ) ;
if ( target . Icon . IsNullOrWhiteSpace ( ) )
2019-06-07 17:02:55 +01:00
target . Icon = Constants . Icons . UserGroup ;
2019-03-24 12:01:23 +01:00
}
private IDictionary < string , IEnumerable < Permission > > MapUserGroupDefaultPermissions ( IUserGroup source )
{
Permission GetPermission ( IAction action )
= > new Permission
{
Category = action . Category . IsNullOrWhiteSpace ( )
? _textService . Localize ( $"actionCategories/{Constants.Conventions.PermissionCategories.OtherCategory}" )
: _textService . Localize ( $"actionCategories/{action.Category}" ) ,
Name = _textService . Localize ( $"actions/{action.Alias}" ) ,
Description = _textService . Localize ( $"actionDescriptions/{action.Alias}" ) ,
Icon = action . Icon ,
Checked = source . Permissions ! = null & & source . Permissions . Contains ( action . Letter . ToString ( CultureInfo . InvariantCulture ) ) ,
PermissionCode = action . Letter . ToString ( CultureInfo . InvariantCulture )
} ;
return _actions
. Where ( x = > x . CanBePermissionAssigned )
. Select ( GetPermission )
. GroupBy ( x = > x . Category )
. ToDictionary ( x = > x . Key , x = > ( IEnumerable < Permission > ) x . ToArray ( ) ) ;
}
2019-11-05 15:05:51 +11:00
private static string MapContentTypeIcon ( IEntitySlim entity )
= > entity is IContentEntitySlim contentEntity ? contentEntity . ContentTypeIcon : null ;
2019-03-24 12:01:23 +01:00
2019-04-07 11:26:47 +02:00
private IEnumerable < EntityBasic > GetStartNodes ( int [ ] startNodeIds , UmbracoObjectTypes objectType , string localizedKey , MapperContext context )
2019-03-24 12:01:23 +01:00
{
if ( startNodeIds . Length < = 0 )
return Enumerable . Empty < EntityBasic > ( ) ;
var startNodes = new List < EntityBasic > ( ) ;
if ( startNodeIds . Contains ( - 1 ) )
startNodes . Add ( CreateRootNode ( _textService . Localize ( localizedKey ) ) ) ;
var mediaItems = _entityService . GetAll ( objectType , startNodeIds ) ;
2019-04-08 16:38:18 +02:00
startNodes . AddRange ( context . MapEnumerable < IEntitySlim , EntityBasic > ( mediaItems ) ) ;
2019-03-24 12:01:23 +01:00
return startNodes ;
}
private IEnumerable < EditorNavigation > CreateUserEditorNavigation ( )
{
return new [ ]
2017-09-12 16:22:16 +02:00
{
2019-03-24 12:01:23 +01:00
new EditorNavigation
{
Active = true ,
Alias = "details" ,
Icon = "icon-umb-users" ,
Name = _textService . Localize ( "general/user" ) ,
View = "views/users/views/user/details.html"
}
2017-09-12 16:22:16 +02:00
} ;
2017-07-20 11:21:28 +02:00
}
2017-07-19 13:42:47 +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 ;
2017-07-20 11:21:28 +02:00
}
2019-03-24 12:01:23 +01:00
private EntityBasic CreateRootNode ( string name )
{
return new EntityBasic
{
Name = name ,
Path = "-1" ,
Icon = "icon-folder" ,
Id = - 1 ,
Trashed = false ,
ParentId = - 1
} ;
}
2017-07-19 13:42:47 +02:00
}
2017-07-20 11:21:28 +02:00
}