public override async Task <IDisplayResult> UpdateAsync(BagPart part, UpdatePartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();
            var model = new BagPartEditViewModel {
                BagPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            var contentItems = new List <ContentItem>();

            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentItem = await _contentManager.NewAsync(model.ContentTypes[i]);

                var existing = part.ContentItems.FirstOrDefault(x => String.Equals(x.ContentItemId, model.Prefixes[i], StringComparison.OrdinalIgnoreCase));
                if (existing != null)
                {
                    contentItem.ContentItemId = model.Prefixes[i];
                }

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix : model.Prefixes[i]);

                contentItems.Add(contentItem);
            }

            part.ContentItems = contentItems;

            return(Edit(part, context));
        }
        public override async Task <IDisplayResult> UpdateAsync(BagPart part, UpdatePartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();
            var model = new BagPartEditViewModel {
                BagPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            var contentItems = new List <ContentItem>();

            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentItem = await _contentManager.NewAsync(model.ContentTypes[i]);

                var existingContentItem = part.ContentItems.FirstOrDefault(x => String.Equals(x.ContentItemId, model.Prefixes[i], StringComparison.OrdinalIgnoreCase));
                // When the content item already exists merge its elements to preverse nested content item ids.
                // All of the data for these merged items is then replaced by the model values on update, while a nested content item id is maintained.
                // This prevents nested items which rely on the content item id, i.e. the media attached field, losing their reference point.
                if (existingContentItem != null)
                {
                    contentItem.ContentItemId = model.Prefixes[i];
                    contentItem.Merge(existingContentItem);
                }

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix : model.Prefixes[i]);

                contentItems.Add(contentItem);
            }

            part.ContentItems = contentItems;

            return(Edit(part, context));
        }
        public override async Task <IDisplayResult> UpdateAsync(BagPart part, BuildPartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();
            var model = new BagPartEditViewModel {
                BagPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            part.ContentItems.Clear();

            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentItem = await _contentManager.NewAsync(model.ContentTypes[i]);

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix : model.Prefixes[i]);

                part.ContentItems.Add(contentItem);
            }

            return(Edit(part, context));
        }
Beispiel #4
0
        public override async Task <IDisplayResult> UpdateAsync(BagPart part, UpdatePartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();
            var contentDefinitionManager  = _serviceProvider.GetRequiredService <IContentDefinitionManager>();

            var model = new BagPartEditViewModel {
                BagPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            var contentItems = new List <ContentItem>();

            // Handle the content found in the request
            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentItem = await _contentManager.NewAsync(model.ContentTypes[i]);

                // assign the owner of the item to ensure we can validate access to it later.
                contentItem.Owner = GetCurrentOwner();

                // Try to match the requested id with an existing id
                var existingContentItem = part.ContentItems.FirstOrDefault(x => String.Equals(x.ContentItemId, model.ContentItems[i], StringComparison.OrdinalIgnoreCase));

                if (existingContentItem == null && !await AuthorizeAsync(contentDefinitionManager, CommonPermissions.EditContent, contentItem))
                {
                    // at this point the user is somehow trying to add content with no privileges. ignore the request
                    continue;
                }

                // When the content item already exists merge its elements to preserve nested content item ids.
                // All of the data for these merged items is then replaced by the model values on update, while a nested content item id is maintained.
                // This prevents nested items which rely on the content item id, i.e. the media attached field, losing their reference point.
                if (existingContentItem != null)
                {
                    if (!await AuthorizeAsync(contentDefinitionManager, CommonPermissions.EditContent, existingContentItem))
                    {
                        // at this point the user is somehow modifying existing content with no privileges.
                        // honor the existing data and ignore the data in the request
                        contentItems.Add(existingContentItem);

                        continue;
                    }

                    // at this point the user have privileges to edit, merge the data from the request
                    contentItem.ContentItemId = model.ContentItems[i];
                    contentItem.Merge(existingContentItem);
                }

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix : model.Prefixes[i]);

                contentItems.Add(contentItem);
            }

            // at the end, lets add existing readonly contents.
            foreach (var existingContentItem in part.ContentItems)
            {
                if (contentItems.Any(x => x.ContentItemId == existingContentItem.ContentItemId))
                {
                    // item was already added using the edit

                    continue;
                }

                if (await AuthorizeAsync(contentDefinitionManager, CommonPermissions.DeleteContent, existingContentItem))
                {
                    // at this point the user has permission to delete a securable item or the type isn't securable
                    // if the existsing content id isn't in the requested ids, don't add the content item... meaning the user deleted it
                    if (!model.ContentItems.Contains(existingContentItem.ContentItemId))
                    {
                        continue;
                    }
                }

                // since the content item isn't editable, lets add it so it's not removed from the collection
                contentItems.Add(existingContentItem);
            }

            // TODO, some how here contentItems should be sorted by a defined order
            part.ContentItems = contentItems;

            return(Edit(part, context));
        }