Exemplo n.º 1
0
 /// <summary>
 /// Called when a blog is deleted.
 /// </summary>
 public void OnBlogDeleted(BlogPost blog)
 {
     if (BlogDeleted != null)
     {
         BlogDeleted(new SingleItemEventArgs<BlogPost>(blog));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RenderBlogPostViewModel" /> class.
        /// </summary>
        /// <param name="blogPost">The blog post.</param>
        public RenderBlogPostViewModel(BlogPost blogPost = null)
        {
            if (blogPost != null)
            {
                if (blogPost.Author != null)
                {
                    Author = new RenderBlogPostAuthorViewModel(blogPost.Author);
                }

                ActivationDate = blogPost.ActivationDate;
                ExpirationDate = blogPost.ExpirationDate;
            }
        }
Exemplo n.º 3
0
 protected override void PrepareForUpdateChildrenMasterPages(bool isNew, BlogPost entity, BlogPostViewModel model, out IList<Guid> newMasterIds,
     out IList<Guid> oldMasterIds, out IList<Guid> childrenPageIds, out IList<MasterPage> existingChildrenMasterPages)
 {
     var modelExt = model as BlogPostViewModelExtender;
     if (!isNew && modelExt != null)
     {
         masterPageService.PrepareForUpdateChildrenMasterPages(entity, modelExt.MasterPageId, 
             out newMasterIds, out oldMasterIds, out childrenPageIds, out existingChildrenMasterPages);
     }
     else
     {
         newMasterIds = null;
         oldMasterIds = null;
         childrenPageIds = null;
         existingChildrenMasterPages = null; 
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="RenderBlogPostViewModel" /> class.
        /// </summary>
        /// <param name="blogPost">The blog post.</param>
        /// <param name="content">The content.</param>
        public RenderBlogPostViewModel(BlogPost blogPost = null, BlogPostContent content = null)
        {
            if (content != null)
            {
                ActivationDate = content.ActivationDate;
                ExpirationDate = content.ExpirationDate;
            }

            if (blogPost != null)
            {
                if (blogPost.Author != null)
                {
                    Author = new RenderBlogPostAuthorViewModel(blogPost.Author);
                }

                if (content == null)
                {
                    ActivationDate = blogPost.ActivationDate;
                    ExpirationDate = blogPost.ExpirationDate;
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates the test view model.
        /// </summary>
        /// <returns>Test view model</returns>
        private RenderPageViewModel CreateTestViewModel(bool extendModel = false)
        {
            var entity = new BlogPost
            {
                Title = "Fake Page Title",
                PageUrl = "/Fake/Page/Url/s",
                Id = new Guid("DB4C3C70-F5F3-44A1-9472-6155A9A77D89"),
                CreatedOn = new DateTime(2010, 11, 15),
                ModifiedOn = new DateTime(2012, 12, 3),
                CreatedByUser = "******",
                ModifiedByUser = "******",
                MetaTitle = "Fake Page Meta Title",
                MetaKeywords = "Fake Page Meta Keywords",
                MetaDescription = "Fake Page MetaDescription",
                ActivationDate = new DateTime(2012, 5, 12),
                ExpirationDate = new DateTime(2013, 4, 18)
            };

            if (extendModel)
            {
                entity.Categories = new List<PageCategory>() { new PageCategory() { Category = new Category { Name = "Fake Category Name" }, Page = entity } };
                entity.Author = new Author { Name = "Fake Author Name" };
                entity.Image = new MediaImage { PublicUrl = "/Fake/Main/Image/Url/" };
                entity.SecondaryImage = new MediaImage { PublicUrl = "/Fake/Secondary/Image/Url/" };
                entity.FeaturedImage = new MediaImage { PublicUrl = "/Fake/Featured/Image/Url/" };
                entity.ActivationDate = new DateTime();

                var content = new BlogPostContent { ActivationDate = new DateTime(2012, 5, 12), ExpirationDate = new DateTime(2013, 4, 18) };
                var pageContent = new PageContent { Content = content, Page = entity };
                entity.PageContents = new List<PageContent> { pageContent };
            }

            var model = new RenderPageViewModel(entity)
                            {
                                Options = new List<IOptionValue>
                                              {
                                                  new OptionValueViewModel
                                                      {
                                                          OptionKey = OptionNames.Text,
                                                          OptionValue = "Fake Option Value",
                                                          Type = OptionType.Text
                                                      },

                                                  new OptionValueViewModel
                                                      {
                                                          OptionKey = OptionNames.Float,
                                                          OptionValue = 10.123456M,
                                                          Type = OptionType.Float
                                                      },

                                                  new OptionValueViewModel
                                                      {
                                                          OptionKey = OptionNames.Date,
                                                          OptionValue = new DateTime(2009, 4, 27),
                                                          Type = OptionType.DateTime
                                                      }
                                              }
                            };

            if (extendModel)
            {
                model.Contents = new List<PageContentProjection>();
                model.Contents.Add(
                    new PageContentProjection(
                        entity.PageContents[0],
                        entity.PageContents[0].Content,
                        new BlogPostContentAccessor((BlogPostContent)entity.PageContents[0].Content, new List<IOptionValue>())));
                model.ExtendWithPageData(entity);
                model.ExtendWithBlogData(entity);
            }

            return model;
        }
 private List<BlogPost> GetFakeBlogPosts()
 {
     var blog = new BlogPost
     {
         Id = Guid.NewGuid(),
         Author = new Author { Id = Guid.NewGuid(), Name = "Test Author" },
         PageUrl = "/test/url/",
         Title = "Test title",
         MetaTitle = "Test Meta Title",
         CreatedOn = new DateTime(2012, 10, 9),
         ModifiedOn = new DateTime(2012, 10, 15),
         Description = "Intro Text",
         PageContents =
             new List<PageContent>
             {
                 new PageContent { Content = new BlogPostContent { Html = "Unpbulished content <p>with HTML</p>" } },
                 new PageContent { Content = new BlogPostContent { Html = "Test content <p>with HTML</p>", Status = ContentStatus.Published } }
             }
     };
     blog.Categories = new List<PageCategory>() { new PageCategory() { Category = new Category { Id = Guid.NewGuid(), Name = "Test Category" }, Page = blog } };
     return new List<BlogPost>
            {
                 blog
            };
 }
        /// <summary>
        /// Adds the default access rules to blog post entity.
        /// </summary>
        /// <param name="blogPost">The blog post.</param>
        /// <param name="principal">The principal.</param>
        /// <param name="masterPage">The master page.</param>
        protected void AddDefaultAccessRules(BlogPost blogPost, IPrincipal principal, Page masterPage)
        {
            IEnumerable<IAccessRule> accessRules;

            if (masterPage != null)
            {
                accessRules = masterPage.AccessRules;
            }
            else
            {
                accessRules = accessControlService.GetDefaultAccessList(principal);
            }

            accessControlService.UpdateAccessControl(blogPost, accessRules.ToList());
        }
 protected virtual void MapExtraProperties(bool isNew, BlogPost entity, BlogPostContent content, PageContent pageContent, BlogPostViewModel model, IPrincipal principal)
 {
     entity.Version = model.Version;
 }
        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)
                    {
                        var hasAnyChildren = contentService.CheckIfContentHasDeletingChildren(blogPost.Id, content.Id, request.Content);
                        if (hasAnyChildren)
                        {
                            var message = PagesGlobalization.SaveContent_ContentHasChildrenContents_RegionDeleteConfirmationMessage;
                            var logMessage = string.Format("User is trying to delete content regions which has children contents. Confirmation is required. ContentId: {0}, PageId: {1}",
                                    content.Id, blogPost.Id);
                            throw new ConfirmationRequestException(() => message, logMessage);
                        }
                    }

                    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 };
            }
        }
Exemplo n.º 10
0
        protected override void MapExtraProperties(bool isNew, BlogPost entity, BlogPostContent content, PageContent pageContent, BlogPostViewModel model, IPrincipal principal)
        {
            var currentVersion = entity.Version;
            base.MapExtraProperties(isNew, entity, content, pageContent, model, principal);

            var modelExt = model as BlogPostViewModelExtender;
            if (modelExt != null)
            {
                // Restore version if not set from the extended model
                if (model.Version <= 0)
                {
                    entity.Version = currentVersion;
                }

                entity.SecondaryImage = modelExt.SecondaryImageId.HasValue ? repository.AsProxy<MediaImage>(modelExt.SecondaryImageId.Value) : null;
                entity.FeaturedImage = modelExt.FeaturedImageId.HasValue ? repository.AsProxy<MediaImage>(modelExt.FeaturedImageId.Value) : null;
                entity.IsArchived = modelExt.IsArchived;
                entity.UseNoFollow = modelExt.UseNoFollow;
                entity.UseNoIndex = modelExt.UseNoIndex;
                entity.MetaKeywords = modelExt.MetaKeywords;
                entity.MetaDescription = modelExt.MetaDescription;

                // If creating new and content / page content / region ids are set, enforce them to be set explicitly
                if (isNew && !model.ContentId.HasDefaultValue() && modelExt.PageContentId.HasValue && modelExt.RegionId.HasValue)
                {
                    pageContent.Id = modelExt.PageContentId.Value;
                    pageContent.Region = repository.AsProxy<Region>(modelExt.RegionId.Value);
                }

                // Set blog post Id, if it's set
                if (isNew && !model.Id.HasDefaultValue())
                {
                    entity.Id = model.Id;
                }

                // PublishedOn
                if (isNew && entity.Status == PageStatus.Published && modelExt.PublishedOn.HasValue)
                {
                    entity.PublishedOn = modelExt.PublishedOn.Value;
                }

                // Set layout / master page
                if (modelExt.MasterPageId.HasValue)
                {
                    entity.Layout = null;
                    if (isNew)
                    {
                        entity.MasterPage = repository
                            .AsQueryable<Page>(p => p.Id == modelExt.MasterPageId.Value)
                            .FetchMany(p => p.AccessRules)
                            .ToList()
                            .FirstOne();

                        if (modelExt.AccessRules == null)
                        {
                            AddDefaultAccessRules(entity, principal, entity.MasterPage);
                        }
                        masterPageService.SetPageMasterPages(entity, entity.MasterPage.Id);
                    }
                    else
                    {
                        entity.MasterPage = repository.AsProxy<Page>(modelExt.MasterPageId.Value);
                    }
                }
                else if (modelExt.LayoutId.HasValue)
                {
                    entity.Layout = repository.AsProxy<Layout>(modelExt.LayoutId.Value);
                    entity.MasterPage = null;
                    if (isNew && modelExt.AccessRules == null)
                    {
                        AddDefaultAccessRules(entity, principal, null);
                    }
                }

                // Add access rules from the request
                if (modelExt.AccessRules != null)
                {
                    if (entity.AccessRules == null)
                    {
                        entity.AccessRules = new List<AccessRule>();
                    }
                    else
                    {
                        entity.AccessRules.RemoveDuplicateEntities();
                    }
                    var accessRules = modelExt.AccessRules
                        .Select(r => (IAccessRule)new AccessRule
                        {
                            AccessLevel = (AccessLevel)(int)r.AccessLevel,
                            Identity = r.Identity,
                            IsForRole = r.IsForRole
                        }).ToList();

                    accessControlService.UpdateAccessControl(entity, accessRules);
                }
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Updates the status.
 /// </summary>
 /// <param name="blogPost">The blog post.</param>
 /// <param name="desirableStatus">The desirable status.</param>
 /// <exception cref="CmsException">If <c>desirableStatus</c> is not supported.</exception>
 private void UpdateStatus(BlogPost blogPost, ContentStatus desirableStatus)
 {
     switch (desirableStatus)
     {
         case ContentStatus.Published:
             blogPost.Status = PageStatus.Published;
             blogPost.PublishedOn = DateTime.Now;
             break;
         case ContentStatus.Draft:
             blogPost.Status = PageStatus.Unpublished;
             break;
         case ContentStatus.Preview:
             blogPost.Status = PageStatus.Preview;
             break;
         default:
             throw new CmsException(string.Format("Blog post does not support status: {0}.", desirableStatus));
     }
 }
Exemplo n.º 12
0
        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 };
            }
        }
Exemplo n.º 13
0
        private void FillMetaInfo(BlogPost blogPost)
        {
            if (string.IsNullOrEmpty(blogPost.MetaDescription) && !string.IsNullOrEmpty(blogPost.Description))
            {
                blogPost.MetaDescription = blogPost.Description;
            }

            if (string.IsNullOrEmpty(blogPost.MetaKeywords))
            {
                if (blogPost.PageTags != null && blogPost.PageTags.IsNotEmpty())
                {
                    blogPost.MetaKeywords += string.Join(", ", blogPost.PageTags.Select(x => x.Tag).Select(y => y.Name).ToList());
                }

                if (blogPost.Categories != null && blogPost.Categories.IsNotEmpty())
                {
                    if (!string.IsNullOrEmpty(blogPost.MetaKeywords))
                    {
                        blogPost.MetaKeywords += ", ";
                    }
                    blogPost.MetaKeywords += string.Join(", ", blogPost.Categories.Select(x => x.Category).Select(y => y.Name).ToList());
                }
            }
        }
Exemplo n.º 14
0
        protected void WriteStartBlogMLPost(BlogPost post)
        {
            WriteStartElement("post");
            WriteNodeAttributes(post.Id.ToString(), post.CreatedOn, post.ModifiedOn, post.Status == PageStatus.Published);
            WriteAttributeString("post-url", post.PageUrl);
            WriteAttributeStringRequired("type", "normal");
            WriteAttributeStringRequired("hasexcerpt", (!string.IsNullOrWhiteSpace(post.Description)).ToString());
            WriteAttributeStringRequired("views", "0");
            WriteContent("title", BlogMLContent.Create(post.MetaTitle ?? post.Title, ContentTypes.Text));
            WriteContent("post-name", BlogMLContent.Create(post.Title, ContentTypes.Text));

            if (!string.IsNullOrWhiteSpace(post.Description))
            {
                WriteBlogMLContent("excerpt", BlogMLContent.Create(post.Description, ContentTypes.Text));
            }

            var content = post.PageContents.Where(pc => pc.Content is BlogPostContent).Select(pc => pc.Content).FirstOrDefault();
            if (content != null)
            {
                WriteBlogMLContent("content", BlogMLContent.Create(((BlogPostContent)content).Html, ContentTypes.Text));
            }
        }
Exemplo n.º 15
0
        protected override void GetBlogPostAndContentEntities(BlogPostViewModel model, IPrincipal principal, string[] roles, 
            ref bool isNew, out BlogPostContent content, out PageContent pageContent , out BlogPost blogPost)
        {
            var modelExt = model as BlogPostViewModelExtender;
            if (!isNew && modelExt != null)
            {
                content = null;
                pageContent = null;

                blogPost = repository
                    .AsQueryable<BlogPost>(bp => bp.Id == model.Id)
                    .FetchMany(bp => bp.AccessRules)
                    .FetchMany(bp => bp.PageTags)
                    .ThenFetch(pt => pt.Tag)
                    .ToList()
                    .FirstOrDefault();

                if (blogPost != null)
                {
                    if (configuration.Security.AccessControlEnabled)
                    {
                        accessControlService.DemandAccess(blogPost, principal, AccessLevel.ReadWrite, roles);
                    }

                    if (modelExt.PageContentId.HasValue)
                    {
                        content = repository
                            .AsQueryable<BlogPostContent>(c => c.PageContents.Any(x => x.Page.Id == model.Id
                                && !x.IsDeleted && x.Id == modelExt.PageContentId.Value
                                && !x.IsDeleted && c.Id == model.ContentId))
                            .ToFuture()
                            .FirstOrDefault();

                        if (content == null)
                        {
                            const string message = "Cannot find a blog post content by specified blog post content and Id and page content Id.";
                            var logMessage = string.Format("{0} BlogId: {1}, BlogPostContentId: {2}, PageContentId: {3}", 
                                message, model.Id, model.ContentId, modelExt.PageContentId);
                            throw new ValidationException(() => message, logMessage);
                        }

                        pageContent = repository.First<PageContent>(pc => pc.Id == modelExt.PageContentId.Value);
                    }
                    else
                    {
                        content = repository
                            .AsQueryable<BlogPostContent>(c => c.PageContents.Any(x => x.Page.Id == model.Id && !x.IsDeleted) && !c.IsDeleted)
                            .ToFuture()
                            .FirstOrDefault();

                        if (content != null)
                        {
                            var contentRef = content;
                            pageContent = repository.FirstOrDefault<PageContent>(c => c.Page.Id == model.Id && !c.IsDeleted && c.Content == contentRef);
                        }
                    }
                }

                isNew = blogPost == null;
                if (isNew)
                {
                    blogPost = new BlogPost();
                    pageContent = new PageContent { Page = blogPost };
                }
            }
            else
            {
                base.GetBlogPostAndContentEntities(model, principal, roles, ref isNew, out content, out pageContent, out blogPost);
            }
        }
Exemplo n.º 16
0
        protected override IList<Tag> SaveTags(BlogPost blogPost, BlogPostViewModel request)
        {
            if (request.Tags != null)
            {
                return base.SaveTags(blogPost, request);
            }

            return null;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Saves the blog post.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="principal">The principal.</param>
        /// <returns>
        /// Saved blog post entity
        /// </returns>
        public BlogPost SaveBlogPost(BlogPostViewModel request, IPrincipal principal)
        {
            string[] roles;
            if (request.DesirableStatus == ContentStatus.Published)
            {
                accessControlService.DemandAccess(principal, RootModuleConstants.UserRoles.PublishContent);
                roles = new[] { RootModuleConstants.UserRoles.PublishContent };
            }
            else
            {
                accessControlService.DemandAccess(principal, RootModuleConstants.UserRoles.EditContent);
                roles = new[] { RootModuleConstants.UserRoles.EditContent };
            }

            Layout layout;
            Page masterPage;
            LoadLayout(out layout, out masterPage);

            if (masterPage != null)
            {
                var level = accessControlService.GetAccessLevel(masterPage, principal);
                if (level < AccessLevel.Read)
                {
                    var message = BlogGlobalization.SaveBlogPost_FailedToSave_InaccessibleMasterPage;
                    const string logMessage = "Failed to save blog post. Selected master page for page layout is inaccessible.";
                    throw new ValidationException(() => message, logMessage);
                }
            }

            var region = LoadRegion(layout, masterPage);
            var isNew = request.Id.HasDefaultValue();
            var userCanEdit = securityService.IsAuthorized(RootModuleConstants.UserRoles.EditContent);

            // UnitOfWork.BeginTransaction(); // NOTE: this causes concurrent data exception.

            BlogPost blogPost;
            BlogPostContent content = null;
            PageContent pageContent = null;
            Pages.Models.Redirect redirectCreated = 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 (cmsConfiguration.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)
                    {
                        var hasAnyChildren = contentService.CheckIfContentHasDeletingChildren(blogPost.Id, content.Id, request.Content);
                        if (hasAnyChildren)
                        {
                            var message = PagesGlobalization.SaveContent_ContentHasChildrenContents_RegionDeleteConfirmationMessage;
                            var logMessage = string.Format("User is trying to delete content regions which has children contents. Confirmation is required. ContentId: {0}, PageId: {1}",
                                    content.Id, blogPost.Id);
                            throw new ConfirmationRequestException(() => message, logMessage);
                        }
                    }

                    pageContent = repository.FirstOrDefault<PageContent>(c => c.Page == blogPost && !c.IsDeleted && c.Content == content);
                }

                if (userCanEdit && !string.Equals(blogPost.PageUrl, request.BlogUrl) && request.BlogUrl != null)
                {
                    request.BlogUrl = urlService.FixUrl(request.BlogUrl);
                    pageService.ValidatePageUrl(request.BlogUrl, request.Id);
                    if (request.RedirectFromOldUrl)
                    {
                        var redirect = redirectService.CreateRedirectEntity(blogPost.PageUrl, request.BlogUrl);
                        if (redirect != null)
                        {
                            repository.Save(redirect);
                            redirectCreated = redirect;
                        }
                    }

                    blogPost.PageUrl = urlService.FixUrl(request.BlogUrl);
                }
            }
            else
            {
                blogPost = new BlogPost();
            }

            if (pageContent == null)
            {
                pageContent = new PageContent { Region = region, Page = blogPost };
            }

            // Push to change modified data each time.
            blogPost.ModifiedOn = DateTime.Now;
            blogPost.Version = request.Version;

            if (userCanEdit)
            {
                blogPost.Title = request.Title;
                blogPost.Description = request.IntroText;
                blogPost.Author = request.AuthorId.HasValue ? repository.AsProxy<Author>(request.AuthorId.Value) : null;
                blogPost.Category = request.CategoryId.HasValue ? repository.AsProxy<Category>(request.CategoryId.Value) : null;
                blogPost.Image = (request.Image != null && request.Image.ImageId.HasValue) ? repository.AsProxy<MediaImage>(request.Image.ImageId.Value) : null;
                if (isNew || request.DesirableStatus == ContentStatus.Published)
                {
                    blogPost.ActivationDate = request.LiveFromDate;
                    blogPost.ExpirationDate = TimeHelper.FormatEndDate(request.LiveToDate);
                }
            }

            if (isNew)
            {
                if (!string.IsNullOrWhiteSpace(request.BlogUrl))
                {
                    blogPost.PageUrl = urlService.FixUrl(request.BlogUrl);
                    pageService.ValidatePageUrl(blogPost.PageUrl);
                }
                else
                {
                    blogPost.PageUrl = CreateBlogPermalink(request.Title);
                }

                blogPost.MetaTitle = request.MetaTitle ?? request.Title;
                if (masterPage != null)
                {
                    blogPost.MasterPage = masterPage;
                    masterPageService.SetPageMasterPages(blogPost, masterPage.Id);
                }
                else
                {
                    blogPost.Layout = layout;
                }
                UpdateStatus(blogPost, request.DesirableStatus);
                AddDefaultAccessRules(blogPost, principal, masterPage);
            }
            else if (request.DesirableStatus == ContentStatus.Published
                || blogPost.Status == PageStatus.Preview)
            {
                // Update only if publishing or current status is preview.
                // Else do not change, because it may change from published to draft status 
                UpdateStatus(blogPost, request.DesirableStatus);
            }

            // Create content.
            var newContent = new BlogPostContent
            {
                Id = content != null ? content.Id : Guid.Empty,
                Name = request.Title,
                Html = request.Content ?? string.Empty,
                EditInSourceMode = request.EditInSourceMode,
                ActivationDate = request.LiveFromDate,
                ExpirationDate = TimeHelper.FormatEndDate(request.LiveToDate)
            };

            // Preserve content if user is not authorized to change it.
            if (!userCanEdit)
            {
                if (content == null)
                {
                    throw new SecurityException("Forbidden: Access is denied."); // User has no rights to create new content.
                }

                var contentToPublish = (BlogPostContent)(content.History != null
                    ? content.History.FirstOrDefault(c => c.Status == ContentStatus.Draft) ?? content
                    : content);

                newContent.Name = contentToPublish.Name;
                newContent.Html = contentToPublish.Html;
            }

            content = (BlogPostContent)contentService.SaveContentWithStatusUpdate(newContent, request.DesirableStatus);
            pageContent.Content = content;

            blogPost.PageUrlHash = blogPost.PageUrl.UrlHash();
            blogPost.UseCanonicalUrl = request.UseCanonicalUrl;

            repository.Save(blogPost);
            repository.Save(content);
            repository.Save(pageContent);

            pageContent.Content = content;
            blogPost.PageContents = new [] {pageContent};

            IList<Tag> newTags = null;
            if (userCanEdit)
            {
                tagService.SavePageTags(blogPost, request.Tags, out newTags);
            }

            // Commit
            unitOfWork.Commit();

            // Notify about new or updated blog post.
            if (isNew)
            {
                Events.BlogEvents.Instance.OnBlogCreated(blogPost);
            }
            else
            {
                Events.BlogEvents.Instance.OnBlogUpdated(blogPost);

            }

            // Notify about new created tags.
            Events.RootEvents.Instance.OnTagCreated(newTags);

            // Notify about redirect creation.
            if (redirectCreated != null)
            {
                Events.PageEvents.Instance.OnRedirectCreated(redirectCreated);
            }

            return blogPost;
        }
Exemplo n.º 18
0
        protected virtual IList<Tag> SaveTags(BlogPost blogPost, BlogPostViewModel request)
        {
            IList<Tag> newTags;
            tagService.SavePageTags(blogPost, request.Tags, out newTags);

            return newTags;
        }
Exemplo n.º 19
0
        /// <summary>
        /// Adds the default access rules to blog post entity.
        /// </summary>
        /// <param name="blogPost">The blog post.</param>
        /// <param name="principal">The principal.</param>
        /// <param name="masterPage">The master page.</param>
        private void AddDefaultAccessRules(BlogPost blogPost, IPrincipal principal, Page masterPage)
        {
            // Set default access rules
            blogPost.AccessRules = new List<AccessRule>();

            if (masterPage != null)
            {
                blogPost.AccessRules = masterPage
                    .AccessRules
                    .Select(x => new AccessRule
                        {
                            Identity = x.Identity,
                            AccessLevel = x.AccessLevel,
                            IsForRole = x.IsForRole
                        })
                    .ToList();
            }
            else
            {
                var list = accessControlService.GetDefaultAccessList(principal);
                foreach (var rule in list)
                {
                    blogPost.AccessRules.Add(new AccessRule
                        {
                            Identity = rule.Identity,
                            AccessLevel = rule.AccessLevel,
                            IsForRole = rule.IsForRole
                        });
                }
            }
        }
Exemplo n.º 20
0
 protected virtual void PrepareForUpdateChildrenMasterPages(bool isNew, BlogPost entity, BlogPostViewModel model, out IList<Guid> newMasterIds,
     out IList<Guid> oldMasterIds, out IList<Guid> childrenPageIds, out IList<MasterPage> existingChildrenMasterPages)
 {
     newMasterIds = null;
     oldMasterIds = null;
     childrenPageIds = null;
     existingChildrenMasterPages = null;
 }