private TEntity GetWidgetForSave <TEntity>(TEntity widgetContent, EditWidgetViewModel model, bool createIfNotExists, out bool isCreatingNew) where TEntity : Widget { TEntity widget, originalWidget = null; var createNewWithId = false; isCreatingNew = model.Id.HasDefaultValue(); if (createIfNotExists || !isCreatingNew) { originalWidget = repository.FirstOrDefault <TEntity>(model.Id); isCreatingNew = originalWidget == null; createNewWithId = isCreatingNew && !model.Id.HasDefaultValue(); } var dynamicContentContainer = widgetContent as IDynamicContentContainer; if (dynamicContentContainer != null && !isCreatingNew && !model.IsUserConfirmed) { contentService.CheckIfContentHasDeletingChildrenWithException(null, widgetContent.Id, dynamicContentContainer.Html); CheckIfContentHasDeletingWidgetsWithDynamicRegions(originalWidget, dynamicContentContainer.Html); } if (model.DesirableStatus == ContentStatus.Published) { if (isCreatingNew) { if (model.PublishedOn.HasValue) { widgetContent.PublishedOn = model.PublishedOn; } if (!string.IsNullOrEmpty(model.PublishedByUser)) { widgetContent.PublishedByUser = model.PublishedByUser; } } else { widgetContent.PublishedOn = originalWidget.PublishedOn; widgetContent.PublishedByUser = originalWidget.PublishedByUser; } } if (createNewWithId) { widget = widgetContent; contentService.UpdateDynamicContainer(widget); widget.Status = model.DesirableStatus; widget.Id = model.Id; } else { widget = (TEntity)contentService.SaveContentWithStatusUpdate(widgetContent, model.DesirableStatus); } return(widget); }
protected virtual void GetBlogPostAndContentEntities(BlogPostViewModel request, IPrincipal principal, string[] roles, ref bool isNew, out BlogPostContent content, out PageContent pageContent, out BlogPost blogPost) { content = null; pageContent = null; // Loading blog post and it's content, or creating new, if such not exists if (!isNew) { var blogPostFuture = repository .AsQueryable <BlogPost>(b => b.Id == request.Id) .ToFuture(); content = repository .AsQueryable <BlogPostContent>(c => c.PageContents.Any(x => x.Page.Id == request.Id && !x.IsDeleted)) .ToFuture() .FirstOrDefault(); blogPost = blogPostFuture.FirstOne(); if (configuration.Security.AccessControlEnabled) { accessControlService.DemandAccess(blogPost, principal, AccessLevel.ReadWrite, roles); } if (content != null) { // Check if user has confirmed the deletion of content if (!request.IsUserConfirmed && blogPost.IsMasterPage) { contentService.CheckIfContentHasDeletingChildrenWithException(blogPost.Id, content.Id, request.Content); } var bpRef = blogPost; var contentRef = content; pageContent = repository.FirstOrDefault <PageContent>(c => c.Page == bpRef && !c.IsDeleted && c.Content == contentRef); } } else { blogPost = new BlogPost(); } if (pageContent == null) { pageContent = new PageContent { Page = blogPost }; } }
public ChangedContentResultViewModel Execute(SavePageHtmlContentCommandRequest request) { var model = request.Content; if (model.DesirableStatus == ContentStatus.Published) { AccessControlService.DemandAccess(Context.Principal, RootModuleConstants.UserRoles.PublishContent); } if (model.Id == default(Guid) || model.DesirableStatus != ContentStatus.Published) { AccessControlService.DemandAccess(Context.Principal, RootModuleConstants.UserRoles.EditContent); } PageContent pageContent; var isNew = model.Id.HasDefaultValue(); if (!isNew) { var query = Repository .AsQueryable <PageContent>() .Where(f => f.Id == model.Id && !f.IsDeleted) .AsQueryable(); if (!model.IsUserConfirmed) { query = query.Fetch(f => f.Page); } if (configuration.Security.AccessControlEnabled) { query = query.Fetch(f => f.Page).ThenFetchMany(f => f.AccessRules); } pageContent = query.ToList().FirstOne(); // Check if user has confirmed the deletion of content if (!model.IsUserConfirmed && pageContent.Page.IsMasterPage) { contentService.CheckIfContentHasDeletingChildrenWithException(model.PageId, model.ContentId, model.PageContent); } } else { pageContent = new PageContent(); pageContent.Order = contentService.GetPageContentNextOrderNumber(model.PageId, model.ParentPageContentId); if (configuration.Security.AccessControlEnabled) { pageContent.Page = Repository .AsQueryable <Root.Models.Page>(p => p.Id == model.PageId) .FetchMany(p => p.AccessRules) .ToList() .FirstOne(); } } // Demand access if (configuration.Security.AccessControlEnabled) { AccessControlService.DemandAccess(pageContent.Page, Context.Principal, AccessLevel.ReadWrite); } // Get page as proxy, if page is not retrieved yet if (!configuration.Security.AccessControlEnabled) { pageContent.Page = Repository.AsProxy <Root.Models.Page>(model.PageId); } pageContent.Region = Repository.AsProxy <Region>(model.RegionId); if (!model.ParentPageContentId.HasDefaultValue()) { pageContent.Parent = Repository.AsProxy <PageContent>(model.ParentPageContentId); } else { pageContent.Parent = null; } var contentToSave = new HtmlContent { Id = model.ContentId, ActivationDate = model.LiveFrom, ExpirationDate = TimeHelper.FormatEndDate(model.LiveTo), Name = model.ContentName, Html = model.PageContent ?? string.Empty, UseCustomCss = model.EnabledCustomCss, CustomCss = model.CustomCss, UseCustomJs = model.EanbledCustomJs, CustomJs = model.CustomJs, EditInSourceMode = model.EditInSourceMode }; // Preserve content if user is not authorized to change it. if (!SecurityService.IsAuthorized(RootModuleConstants.UserRoles.EditContent) && model.Id != default(Guid)) { var originalContent = Repository.First <HtmlContent>(model.ContentId); var contentToPublish = (HtmlContent)(originalContent.History != null ? originalContent.History.FirstOrDefault(c => c.Status == ContentStatus.Draft) ?? originalContent : originalContent); contentToSave.Name = contentToPublish.Name; contentToSave.Html = contentToPublish.Html; contentToSave.UseCustomCss = contentToPublish.UseCustomCss; contentToSave.CustomCss = contentToPublish.CustomCss; contentToSave.UseCustomJs = contentToPublish.UseCustomJs; contentToSave.CustomJs = contentToPublish.CustomJs; contentToSave.EditInSourceMode = contentToPublish.EditInSourceMode; } UnitOfWork.BeginTransaction(); pageContent.Content = contentService.SaveContentWithStatusUpdate( contentToSave, model.DesirableStatus); optionsService.SaveChildContentOptions(pageContent.Content, request.ChildContentOptionValues, model.DesirableStatus); if (pageContent.Content.ContentRegions != null && pageContent.Content.ContentRegions.Count > 0) { if (!pageContent.Page.IsMasterPage) { var logMessage = string.Format("Dynamic regions are not allowed. Page: {0}, Content: {1}", pageContent.Page, pageContent.Id); throw new ValidationException(() => PagesGlobalization.SaveContent_DynamicRegionsAreNotAllowed_Message, logMessage); } } Repository.Save(pageContent); UnitOfWork.Commit(); // Notify. if (model.DesirableStatus != ContentStatus.Preview) { if (isNew) { Events.PageEvents.Instance.OnHtmlContentCreated((HtmlContent)pageContent.Content); Events.PageEvents.Instance.OnPageContentInserted(pageContent); } else { Events.PageEvents.Instance.OnHtmlContentUpdated((HtmlContent)pageContent.Content); } } var contentData = (pageContent.Content.History != null ? pageContent.Content.History.FirstOrDefault(c => c.Status == ContentStatus.Draft) ?? pageContent.Content : pageContent.Content); var response = new ChangedContentResultViewModel { PageContentId = pageContent.Id, ContentId = contentData.Id, RegionId = pageContent.Region.Id, PageId = pageContent.Page.Id, DesirableStatus = request.Content.DesirableStatus, Title = contentData.Name, ContentVersion = contentData.Version, PageContentVersion = pageContent.Version, ContentType = HtmlContentAccessor.ContentWrapperType }; if (request.Content.IncludeChildRegions) { response.Regions = widgetService.GetWidgetChildRegionViewModels(contentData); } return(response); }