Exemplo n.º 1
0
        private static PropertyGroup MapSaveGroup <TPropertyType>(PropertyGroupBasic <TPropertyType> sourceGroup,
                                                                  IEnumerable <PropertyGroup> destOrigGroups, MapperContext context)
            where TPropertyType : PropertyTypeBasic
        {
            PropertyGroup destGroup;

            if (sourceGroup.Id > 0)
            {
                // update an existing group
                // ensure it is still there, then map/update
                destGroup = destOrigGroups.FirstOrDefault(x => x.Id == sourceGroup.Id);
                if (destGroup != null)
                {
                    context.Map(sourceGroup, destGroup);
                    return(destGroup);
                }

                // force-clear the ID as it does not match anything
                sourceGroup.Id = 0;
            }

            // insert a new group, or update an existing group that has
            // been deleted in the meantime and we need to re-create
            // map/create
            destGroup = context.Map <PropertyGroup>(sourceGroup);
            return(destGroup);
        }
Exemplo n.º 2
0
        private static IPropertyType MapSaveProperty(PropertyTypeBasic sourceProperty,
                                                     IEnumerable <IPropertyType> destOrigProperties, MapperContext context)
        {
            IPropertyType destProperty;

            if (sourceProperty.Id > 0)
            {
                // updating an existing property
                // ensure it is still there, then map/update
                destProperty = destOrigProperties.FirstOrDefault(x => x.Id == sourceProperty.Id);
                if (destProperty != null)
                {
                    context.Map(sourceProperty, destProperty);
                    return(destProperty);
                }

                // force-clear the ID as it does not match anything
                sourceProperty.Id = 0;
            }

            // insert a new property, or update an existing property that has
            // been deleted in the meantime and we need to re-create
            // map/create
            destProperty = context.Map <IPropertyType>(sourceProperty);
            return(destProperty);
        }
Exemplo n.º 3
0
 private void Map(MembershipUser source, MemberDisplay target, MapperContext context)
 {
     //first convert to IMember
     var member = context.Map<IMember>(source);
     //then convert to MemberDisplay
     context.Map<IMember, MemberDisplay>(member);
 }
Exemplo n.º 4
0
        // helpers

        private void MapUserGroupBasic(UserGroupBasic target, IEnumerable <string> sourceAllowedSections, int?sourceStartContentId, int?sourceStartMediaId, MapperContext context)
        {
            var allSections = _sectionService.GetSections();

            target.Sections = context.MapEnumerable <ISection, Section>(allSections.Where(x => sourceAllowedSections.Contains(x.Alias)));

            if (sourceStartMediaId > 0)
            {
                target.MediaStartNode = context.Map <EntityBasic>(_entityService.Get(sourceStartMediaId.Value, UmbracoObjectTypes.Media));
            }
            else if (sourceStartMediaId == -1)
            {
                target.MediaStartNode = CreateRootNode(_textService.Localize("media/mediaRoot"));
            }

            if (sourceStartContentId > 0)
            {
                target.ContentStartNode = context.Map <EntityBasic>(_entityService.Get(sourceStartContentId.Value, UmbracoObjectTypes.Document));
            }
            else if (sourceStartContentId == -1)
            {
                target.ContentStartNode = CreateRootNode(_textService.Localize("content/contentRoot"));
            }

            if (target.Icon.IsNullOrWhiteSpace())
            {
                target.Icon = "icon-users";
            }
        }
Exemplo n.º 5
0
        public IEnumerable <ContentVariantDisplay> Map(IContent source, MapperContext context)
        {
            var result = new List <ContentVariantDisplay>();

            if (!source.ContentType.VariesByCulture())
            {
                //this is invariant so just map the IContent instance to ContentVariationDisplay
                result.Add(context.Map <ContentVariantDisplay>(source));
            }
            else
            {
                var allLanguages = _localizationService.GetAllLanguages().OrderBy(x => x.Id).ToList();
                if (allLanguages.Count == 0)
                {
                    return(Enumerable.Empty <ContentVariantDisplay>());                        //this should never happen
                }
                var langs = context.MapEnumerable <ILanguage, Language>(allLanguages).ToList();

                //create a variant for each language, then we'll populate the values
                var variants = langs.Select(x =>
                {
                    //We need to set the culture in the mapping context since this is needed to ensure that the correct property values
                    //are resolved during the mapping
                    context.SetCulture(x.IsoCode);
                    return(context.Map <ContentVariantDisplay>(source));
                }).ToList();

                for (int i = 0; i < langs.Count; i++)
                {
                    var x       = langs[i];
                    var variant = variants[i];

                    variant.Language = x;
                    variant.Name     = source.GetCultureName(x.IsoCode);
                }

                //Put the default language first in the list & then sort rest by a-z
                var defaultLang = variants.SingleOrDefault(x => x.Language.IsDefault);

                //Remove the default language from the list for now
                variants.Remove(defaultLang);

                //Sort the remaining languages a-z
                variants = variants.OrderBy(x => x.Name).ToList();

                //Insert the default language as the first item
                variants.Insert(0, defaultLang);

                return(variants);
            }
            return(result);
        }
Exemplo n.º 6
0
        // no MapAll - take care
        private void Map(IContentType source, DocumentTypeDisplay target, MapperContext context)
        {
            MapTypeToDisplayBase <DocumentTypeDisplay, PropertyTypeDisplay>(source, target);

            target.AllowCultureVariant = source.VariesByCulture();
            target.AllowSegmentVariant = source.VariesBySegment();
            target.ContentApps         = _commonMapper.GetContentApps(source);

            //sync templates
            target.AllowedTemplates = context.MapEnumerable <ITemplate, EntityBasic>(source.AllowedTemplates);

            if (source.DefaultTemplate != null)
            {
                target.DefaultTemplate = context.Map <EntityBasic>(source.DefaultTemplate);
            }

            //default listview
            target.ListViewEditorName = Constants.Conventions.DataTypes.ListViewPrefix + "Content";

            if (string.IsNullOrEmpty(source.Alias))
            {
                return;
            }

            var name = Constants.Conventions.DataTypes.ListViewPrefix + source.Alias;

            if (_dataTypeService.GetDataType(name) != null)
            {
                target.ListViewEditorName = name;
            }
        }
Exemplo n.º 7
0
    public ContentTypeBasic?GetContentType(IContentBase source, MapperContext context)
    {
        IContentTypeComposition?contentType      = _contentTypeBaseServiceProvider.GetContentTypeOf(source);
        ContentTypeBasic?       contentTypeBasic = context.Map <IContentTypeComposition, ContentTypeBasic>(contentType);

        return(contentTypeBasic);
    }
Exemplo n.º 8
0
    public IEnumerable <TVariant> Map <TVariant>(IContent source, MapperContext context)
        where TVariant : ContentVariantDisplay
    {
        var variesByCulture = source.ContentType.VariesByCulture();
        var variesBySegment = source.ContentType.VariesBySegment();

        List <TVariant> variants = new ();

        if (!variesByCulture && !variesBySegment)
        {
            // this is invariant so just map the IContent instance to ContentVariationDisplay
            TVariant?variantDisplay = context.Map <TVariant>(source);
            if (variantDisplay is not null)
            {
                variants.Add(variantDisplay);
            }
        }
        else if (variesByCulture && !variesBySegment)
        {
            IEnumerable <ContentEditing.Language> languages = GetLanguages(context);
            variants = languages
                       .Select(language => CreateVariantDisplay <TVariant>(context, source, language, null))
                       .WhereNotNull()
                       .ToList();
        }
        else if (variesBySegment && !variesByCulture)
        {
            // Segment only
            IEnumerable <string?> segments = GetSegments(source);
            variants = segments
                       .Select(segment => CreateVariantDisplay <TVariant>(context, source, null, segment))
                       .WhereNotNull()
                       .ToList();
        }
        else
        {
            // Culture and segment
            var languages = GetLanguages(context).ToList();
            var segments  = GetSegments(source).ToList();

            if (languages.Count == 0 || segments.Count == 0)
            {
                // This should not happen
                throw new InvalidOperationException("No languages or segments available");
            }

            variants = languages
                       .SelectMany(language => segments
                                   .Select(segment => CreateVariantDisplay <TVariant>(context, source, language, segment)))
                       .WhereNotNull()
                       .ToList();
        }

        return(SortVariants(variants));
    }
    // no MapAll - take care
    private void Map(DocumentTypeSave source, DocumentTypeDisplay target, MapperContext context)
    {
        MapTypeToDisplayBase <DocumentTypeSave, PropertyTypeBasic, DocumentTypeDisplay, PropertyTypeDisplay>(
            source,
            target,
            context);

        // sync templates
        IEnumerable <string?> destAllowedTemplateAliases = target.AllowedTemplates.Select(x => x.Alias);

        // if the dest is set and it's the same as the source, then don't change
        if (source.AllowedTemplates is not null &&
            destAllowedTemplateAliases.SequenceEqual(source.AllowedTemplates) == false)
        {
            IEnumerable <ITemplate>?templates = _fileService.GetTemplates(source.AllowedTemplates.ToArray());
            target.AllowedTemplates = source.AllowedTemplates
                                      .Select(x =>
            {
                ITemplate?template = templates?.SingleOrDefault(t => t.Alias == x);
                return(template != null
                        ? context.Map <EntityBasic>(template)
                        : null);
            })
                                      .WhereNotNull()
                                      .ToArray();
        }

        if (source.DefaultTemplate.IsNullOrWhiteSpace() == false)
        {
            // if the dest is set and it's the same as the source, then don't change
            if (target.DefaultTemplate == null || source.DefaultTemplate != target.DefaultTemplate.Alias)
            {
                ITemplate?template = _fileService.GetTemplate(source.DefaultTemplate);
                target.DefaultTemplate = template == null ? null : context.Map <EntityBasic>(template);
            }
        }
        else
        {
            target.DefaultTemplate = null;
        }
    }
    // no MapAll - take care
    private void Map(IContentType source, DocumentTypeDisplay target, MapperContext context)
    {
        MapTypeToDisplayBase <DocumentTypeDisplay, PropertyTypeDisplay>(source, target);

        if (source is IContentTypeWithHistoryCleanup sourceWithHistoryCleanup)
        {
            target.HistoryCleanup = new HistoryCleanupViewModel
            {
                PreventCleanup = sourceWithHistoryCleanup.HistoryCleanup?.PreventCleanup ?? false,
                KeepAllVersionsNewerThanDays =
                    sourceWithHistoryCleanup.HistoryCleanup?.KeepAllVersionsNewerThanDays,
                KeepLatestVersionPerDayForDays =
                    sourceWithHistoryCleanup.HistoryCleanup?.KeepLatestVersionPerDayForDays,
                GlobalKeepAllVersionsNewerThanDays =
                    _contentSettings.ContentVersionCleanupPolicy.KeepAllVersionsNewerThanDays,
                GlobalKeepLatestVersionPerDayForDays =
                    _contentSettings.ContentVersionCleanupPolicy.KeepLatestVersionPerDayForDays,
                GlobalEnableCleanup = _contentSettings.ContentVersionCleanupPolicy.EnableCleanup,
            };
        }

        target.AllowCultureVariant = source.VariesByCulture();
        target.AllowSegmentVariant = source.VariesBySegment();
        target.ContentApps         = _commonMapper.GetContentAppsForEntity(source);

        // sync templates
        if (source.AllowedTemplates is not null)
        {
            target.AllowedTemplates =
                context.MapEnumerable <ITemplate, EntityBasic>(source.AllowedTemplates).WhereNotNull();
        }

        if (source.DefaultTemplate != null)
        {
            target.DefaultTemplate = context.Map <EntityBasic>(source.DefaultTemplate);
        }

        // default listview
        target.ListViewEditorName = Constants.Conventions.DataTypes.ListViewPrefix + "Content";

        if (string.IsNullOrEmpty(source.Alias))
        {
            return;
        }

        var name = Constants.Conventions.DataTypes.ListViewPrefix + source.Alias;

        if (_dataTypeService.GetDataType(name) != null)
        {
            target.ListViewEditorName = name;
        }
    }
        private ContentVariantDisplay CreateVariantDisplay(MapperContext context, IContent content, Language language, string segment)
        {
            context.SetCulture(language?.IsoCode);
            context.SetSegment(segment);

            var variantDisplay = context.Map <ContentVariantDisplay>(content);

            variantDisplay.Segment  = segment;
            variantDisplay.Language = language;
            variantDisplay.Name     = content.GetCultureName(language?.IsoCode);

            return(variantDisplay);
        }
Exemplo n.º 12
0
        private TVariant CreateVariantDisplay <TVariant>(MapperContext context, IContent content, ContentEditing.Language language, string segment) where TVariant : ContentVariantDisplay
        {
            context.SetCulture(language?.IsoCode);
            context.SetSegment(segment);

            var variantDisplay = context.Map <TVariant>(content);

            variantDisplay.Segment     = segment;
            variantDisplay.Language    = language;
            variantDisplay.Name        = content.GetCultureName(language?.IsoCode);
            variantDisplay.DisplayName = GetDisplayName(language, segment);

            return(variantDisplay);
        }
Exemplo n.º 13
0
        public ContentTypeBasic GetContentType(IContentBase source, MapperContext context)
        {
            // TODO: We can resolve the UmbracoContext from the IValueResolver options!
            // OMG
            if (HttpContext.Current != null && Composing.Current.UmbracoContext != null && Composing.Current.UmbracoContext.Security.CurrentUser != null &&
                Composing.Current.UmbracoContext.Security.CurrentUser.AllowedSections.Any(x => x.Equals(Constants.Applications.Settings)))
            {
                var contentType      = _contentTypeBaseServiceProvider.GetContentTypeOf(source);
                var contentTypeBasic = context.Map <IContentTypeComposition, ContentTypeBasic>(contentType);

                return(contentTypeBasic);
            }
            //no access
            return(null);
        }
Exemplo n.º 14
0
        public IEnumerable <ContentVariantDisplay> Map(IContent source, MapperContext context)
        {
            var variesByCulture = source.ContentType.VariesByCulture();
            var variesBySegment = source.ContentType.VariesBySegment();

            IList <ContentVariantDisplay> variants = new List <ContentVariantDisplay>();

            if (!variesByCulture && !variesBySegment)
            {
                // this is invariant so just map the IContent instance to ContentVariationDisplay
                var variantDisplay = context.Map <ContentVariantDisplay>(source);
                variants.Add(variantDisplay);
            }
            else if (variesByCulture && !variesBySegment)
            {
                var languages = GetLanguages(context);
                variants = languages
                           .Select(language => CreateVariantDisplay(context, source, language, null))
                           .ToList();
            }
            else if (variesBySegment && !variesByCulture)
            {
                // Segment only
                var segments = GetSegments(source);
                variants = segments
                           .Select(segment => CreateVariantDisplay(context, source, null, segment))
                           .ToList();
            }
            else
            {
                // Culture and segment
                var languages = GetLanguages(context).ToList();
                var segments  = GetSegments(source).ToList();

                if (languages.Count == 0 || segments.Count == 0)
                {
                    // This should not happen
                    throw new InvalidOperationException("No languages or segments available");
                }

                variants = languages
                           .SelectMany(language => segments
                                       .Select(segment => CreateVariantDisplay(context, source, language, segment)))
                           .ToList();
            }

            return(SortVariants(variants));
        }
Exemplo n.º 15
0
    public UserProfile?GetCreator(IContent source, MapperContext context)
    {
        IProfile?profile = source.GetWriterProfile(_userService);

        return(profile == null ? null : context.Map <IProfile, UserProfile>(profile));
    }
Exemplo n.º 16
0
 private void Map2(Thing7 source, Thing8 target, MapperContext context) =>
 target.Things = context.Map <IEnumerable <Thing2> >(source.Things);
Exemplo n.º 17
0
 private void Map(Thing5 source, Thing6 target, MapperContext context)
 {
     target.Fruit1 = context.Map <Thing6Enum>(source.Fruit1);
     target.Fruit2 = context.Map <Thing6Enum>(source.Fruit2);
     target.Fruit3 = context.Map <Thing6Enum>(source.Fruit3);
 }
Exemplo n.º 18
0
        public UserProfile GetOwner(IContentBase source, MapperContext context)
        {
            var profile = source.GetCreatorProfile(_userService);

            return(profile == null ? null : context.Map <IProfile, UserProfile>(profile));
        }
Exemplo n.º 19
0
        // Umbraco.Code.MapAll -ContentStartNode -MediaStartNode -Sections -Notifications -Udi
        // Umbraco.Code.MapAll -Trashed -AdditionalData -Users -AssignedPermissions
        private void Map(IUserGroup source, UserGroupDisplay target, MapperContext context)
        {
            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;

            MapUserGroupBasic(target, source.AllowedSections, source.StartContentId, source.StartMediaId, context);

            //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);

            target.Users = context.MapEnumerable <IUser, UserBasic>(users);

            //Deal with assigned permissions:

            var allContentPermissions = _userService.GetPermissions(source, true)
                                        .ToDictionary(x => x.EntityId, x => x);

            IEntitySlim[] contentEntities;
            if (allContentPermissions.Keys.Count == 0)
            {
                contentEntities = Array.Empty <IEntitySlim>();
            }
            else
            {
                // 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();
            }

            var allAssignedPermissions = new List <AssignedContentPermissions>();

            foreach (var entity in contentEntities)
            {
                var contentPermissions = allContentPermissions[entity.Id];

                var assignedContentPermissions = context.Map <AssignedContentPermissions>(entity);
                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);
            }

            target.AssignedPermissions = allAssignedPermissions;
        }