async Task IAsyncActionFilter.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            pageService        = HttpContext.RequestServices.GetRequiredService <IPageService>();
            pageEditingService = HttpContext.RequestServices.GetRequiredService <IPageContentService>();

            if (!Request.Query.TryGetValue("editId", out Microsoft.Extensions.Primitives.StringValues editIdValue) || !Guid.TryParse(editIdValue[0], out Guid editId))
            {
                context.Result = BadRequest();
                return;
            }

            editSession = await pageEditingService.FindEditByIdAsync(editId, HttpContext.RequestAborted);

            if (editSession == null)
            {
                context.Result = BadRequest();
                return;
            }

            page = await pageService.FindPageByIdAsync(editSession.PageId);

            if (page == null)
            {
                context.Result = BadRequest();
                return;
            }

            var content = await pageEditingService.GetContentAsync(editSession, HttpContext.RequestAborted);

            rootContentContext = new ContentContext(page, content, HttpContext.RequestServices, true);

            string modelPath = string.Empty;

            if (Request.Query.TryGetValue("path", out Microsoft.Extensions.Primitives.StringValues pathValue))
            {
                modelPath = pathValue[0];
            }

            contentContext = rootContentContext.Navigate(modelPath);
            if (contentContext == null)
            {
                context.Result = BadRequest();
                return;
            }

            if (!Request.Query.TryGetValue("field", out Microsoft.Extensions.Primitives.StringValues fieldNameValue))
            {
                context.Result = BadRequest();
                return;
            }
            string fieldName = fieldNameValue[0];

            if (!contentContext.Explorer.Metadata.TryGetField(fieldName, out field))
            {
                context.Result = BadRequest();
                return;
            }

            await next();
        }
Beispiel #2
0
        public Task DiscardEditAsync(IPageEdit editSession, CancellationToken cancellationToken = default)
        {
            if (editSession == null)
            {
                throw new ArgumentNullException(nameof(editSession));
            }

            return(editSessionRepository.DeleteEditAsync(editSession, cancellationToken));
        }
        public async Task <IDictionary <string, object> > GetContentAsync(IPageEdit pageEdit, CancellationToken cancellationToken = default)
        {
            var document = await(await documents.FindAsync(it => it.Id == pageEdit.Id, cancellationToken: cancellationToken)).FirstOrDefaultAsync(cancellationToken);

            if (document == null)
            {
                return(null);
            }

            return(MongoDbHelper.BsonDocumentToDictionary(document.Content));
        }
Beispiel #4
0
        public Task <string> GetUriAsync(IPageEdit pageEditSession, CancellationToken cancellationToken = default)
        {
            if (pageEditSession == null)
            {
                throw new ArgumentNullException(nameof(pageEditSession));
            }

            var url = linkGenerator.GetUriByPage(httpContextAccessor.HttpContext, contentPageOptions.ContentPageName, null, new { editId = pageEditSession.Id.ToString().ToLower() });

            return(Task.FromResult(url));
        }
        public async Task SetContentAsync(IPageEdit pageEdit, IDictionary <string, object> contentData, CancellationToken cancellationToken = default)
        {
            var contentDataDocument = MongoDbHelper.DictionaryToBsonDocument(contentData);
            var updateDefinition    = Builders <PageEditDocument> .Update.Set(it => it.Content, contentDataDocument);

            var updateResult = await documents.UpdateOneAsync(it => it.Id == pageEdit.Id, updateDefinition, cancellationToken : cancellationToken);

            if (updateResult.MatchedCount != 1)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #6
0
        public async Task SetContentAsync(IPageEdit editSession, object content, CancellationToken cancellationToken = default)
        {
            if (editSession == null)
            {
                throw new ArgumentNullException(nameof(editSession));
            }

            var page = await pageService.FindPageByIdAsync(editSession.PageId);

            var pageMetadata = await pageService.GetPageTypeAsync(page);

            var contentData = pageMetadata.ContentMetadata.ConvertContentModelToDictionary(content);

            await editSessionRepository.SetContentAsync(editSession, contentData, cancellationToken);
        }
Beispiel #7
0
        public async Task <object> GetContentAsync(IPageEdit editSession, CancellationToken cancellationToken = default)
        {
            if (editSession == null)
            {
                throw new ArgumentNullException(nameof(editSession));
            }

            var page = await pageService.FindPageByIdAsync(editSession.PageId);

            var pageMetadataProvider = await pageService.GetPageTypeAsync(page);

            var pageContentData = await editSessionRepository.GetContentAsync(editSession, cancellationToken);

            return(pageMetadataProvider.ContentMetadata.ConvertDictionaryToContentModel(pageContentData));
        }
Beispiel #8
0
        public async Task CommitEditAsync(IPageEdit editSession, CancellationToken cancellationToken = default)
        {
            if (editSession == null)
            {
                throw new ArgumentNullException(nameof(editSession));
            }

            var page = await pageService.FindPageByIdAsync(editSession.PageId);

            var pageMetadata = await pageService.GetPageTypeAsync(page);

            var newContentData = await editSessionRepository.GetContentAsync(editSession);

            var pageContentModel = pageMetadata.ContentMetadata.ConvertDictionaryToContentModel(newContentData);

            await pageService.SetPageContentAsync(page, pageContentModel);

            await editSessionRepository.DeleteEditAsync(editSession, cancellationToken);
        }
 private static string GetId(IPageEdit editPage)
 {
     return(GetId(editPage.PageId, editPage.UserId));
 }
        public Task SetContentAsync(IPageEdit pageEdit, IDictionary <string, object> contentData, CancellationToken cancellationToken = default)
        {
            ((PageEdit)pageEdit).Content = contentData;

            return(Task.CompletedTask);
        }
 public Task <IDictionary <string, object> > GetContentAsync(IPageEdit pageEdit, CancellationToken cancellationToken = default)
 {
     return(Task.FromResult(((PageEdit)pageEdit).Content));
 }
        public Task DeleteEditAsync(IPageEdit pageEdit, CancellationToken cancellationToken = default)
        {
            edits.Remove(GetId(pageEdit));

            return(Task.CompletedTask);
        }
 public async Task DeleteEditAsync(IPageEdit pageEdit, CancellationToken cancellationToken = default)
 {
     await documents.FindOneAndDeleteAsync(it => it.Id == pageEdit.Id);
 }
        protected override async Task OnPageRequestAsync(PageRequestContext context)
        {
            PageService = HttpContext.RequestServices.GetRequiredService <IPageService>();

            if (Request.Query.TryGetValue("editId", out string editIdValue))
            {
                if (!Guid.TryParse(editIdValue, out Guid editId))
                {
                    context.Result = BadRequest();
                    return;
                }

                var pageEditingService = HttpContext.RequestServices.GetRequiredService <IPageContentService>();
                editSession = await pageEditingService.FindEditByIdAsync(editId);

                if (editSession == null)
                {
                    context.Result = NotFound();
                    return;
                }

                page = await PageService.FindPageByIdAsync(editSession.PageId);

                if (page == null)
                {
                    context.Result = NotFound();
                    return;
                }

                var accessProvider = HttpContext.RequestServices.GetRequiredService <Identity.IAccessProvider>();

                if (!await accessProvider.CheckAccessAsync() || await accessProvider.GetUserIdAsync() != editSession.UserId)
                {
                    var pageLinkGenerator = HttpContext.RequestServices.GetRequiredService <IPageLinkGenerator>();

                    context.Result = RedirectPermanent(await pageLinkGenerator.GetPathAsync(page));
                    return;
                }
            }
            else
            {
                var routeData = RouteData;

                var pagePath = string.Empty;
                if (routeData.Values.TryGetValue("url", out object urlValue) && urlValue != null)
                {
                    pagePath = (string)urlValue;
                }

                var url = await PageService.FindPageUrlAsync(pagePath);

                if (url == null)
                {
                    context.Result = NotFound();
                    return;
                }

                if (url.PageId.HasValue)
                {
                    page = await PageService.FindPageByIdAsync(url.PageId.Value);

                    if (page == null)
                    {
                        context.Result = NotFound();
                        return;
                    }

                    if (!page.IsPublished)
                    {
                        var accessProvider = HttpContext.RequestServices.GetRequiredService <Identity.IAccessProvider>();
                        if (!await accessProvider.CheckAccessAsync())
                        {
                            context.Result = NotFound();
                            return;
                        }
                    }
                }
                else
                {
                    var pageLinkGenerator = HttpContext.RequestServices.GetRequiredService <IPageLinkGenerator>();
                    var redirectUrl       = await pageLinkGenerator.GetPathAsync(url.Redirect.Path);

                    if (url.Redirect.IsPermament)
                    {
                        context.Result = RedirectPermanent(redirectUrl);
                    }
                    else
                    {
                        context.Result = Redirect(redirectUrl);
                    }
                    return;
                }
            }

            PageMetadata = await PageService.GetPageTypeAsync(page, HttpContext.RequestAborted);

            pageSeo = await PageService.GetPageSeoOptionsAsync(page, HttpContext.RequestAborted);

            if (editSession != null)
            {
                var pageEditingService = HttpContext.RequestServices.GetRequiredService <IPageContentService>();
                PageContent = await pageEditingService.GetContentAsync(editSession, HttpContext.RequestAborted);
            }
            else
            {
                PageContent = await PageService.GetPageContentAsync(page, HttpContext.RequestAborted);
            }
            if (PageContent == null)
            {
                throw new InvalidOperationException();
            }

            ContentContext = new ContentContext(page, PageContent, HttpContext.RequestServices, editSession != null);

            Status       = page.IsPublished ? Models.PageStatus.Published : Models.PageStatus.Draft;
            ParentPageId = await PageService.GetParentPageIdAsync(page, HttpContext.RequestAborted);
        }