Modified CreateRouteValues when no blog Id is specified to avoid creation of orphaned posts (#8255)

This commit is contained in:
MarcoViglione-Laser
2019-08-01 21:05:42 +02:00
committed by Sébastien Ros
parent b6a4ef609d
commit ec9673a1ca
3 changed files with 58 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Orchard.Blogs.Extensions;
@@ -98,6 +99,18 @@ namespace Orchard.Blogs.Controllers {
return Redirect(Url.BlogPostEdit(blogPost));
}
public ActionResult CreateWithoutBlog() {
var blogs = _blogService.Get().ToArray();
if (blogs.Count() == 0) {
Services.Notifier.Warning(T("To create a BlogPost you need to create a blog first. You have been redirected to the Blog creation page."));
return RedirectToAction("Create", "BlogAdmin", new { area = "Orchard.Blogs" });
} else {
Services.Notifier.Warning(T("To create a BlogPost you need to choose a blog first. You have been redirected to the Blog selection page."));
return RedirectToAction("List", "BlogAdmin", new { area = "Orchard.Blogs" });
}
}
//todo: the content shape template has extra bits that the core contents module does not (remove draft functionality)
//todo: - move this extra functionality there or somewhere else that's appropriate?
public ActionResult Edit(int blogId, int postId) {

View File

@@ -5,13 +5,18 @@ using Orchard.Blogs.Services;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Common.Models;
using Orchard.Security;
namespace Orchard.Blogs.Handlers {
public class BlogPostPartHandler : ContentHandler {
private readonly IAuthorizationService _authorizationService;
private readonly IBlogService _blogService;
private readonly IWorkContextAccessor _workContextAccessor;
public BlogPostPartHandler(IBlogService blogService, IBlogPostService blogPostService, RequestContext requestContext) {
public BlogPostPartHandler(IAuthorizationService authorizationService, IBlogService blogService, IBlogPostService blogPostService, RequestContext requestContext, IWorkContextAccessor workContextAccessor) {
_authorizationService = authorizationService;
_blogService = blogService;
_workContextAccessor = workContextAccessor;
OnGetDisplayShape<BlogPostPart>(SetModelProperties);
OnGetEditorShape<BlogPostPart>(SetModelProperties);
@@ -45,8 +50,29 @@ namespace Orchard.Blogs.Handlers {
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
var blogPost = context.ContentItem.As<BlogPostPart>();
if (blogPost == null) {
return;
}
int blogId = 0;
// BlogPart can be null if this is a new Blog Post item.
if (blogPost == null || blogPost.BlogPart == null) {
if (blogPost.BlogPart == null) {
var blogs = _blogService.Get().Where(x => _authorizationService.TryCheckAccess(Permissions.MetaListBlogs, _workContextAccessor.GetContext().CurrentUser, x)).ToArray();
if (blogs.Count() == 1) {
var singleBlog = blogs.ElementAt(0);
if (singleBlog != null) blogId = singleBlog.Id;
}
} else {
blogId = blogPost.BlogPart.Id;
}
if (blogId == 0) {
context.Metadata.CreateRouteValues = new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "BlogPostAdmin"},
{"Action", "CreateWithoutBlog"}
};
return;
}
@@ -54,21 +80,21 @@ namespace Orchard.Blogs.Handlers {
{"Area", "Orchard.Blogs"},
{"Controller", "BlogPostAdmin"},
{"Action", "Create"},
{"blogId", blogPost.BlogPart.Id}
{"blogId", blogId}
};
context.Metadata.EditorRouteValues = new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "BlogPostAdmin"},
{"Action", "Edit"},
{"postId", context.ContentItem.Id},
{"blogId", blogPost.BlogPart.Id}
{"blogId", blogId}
};
context.Metadata.RemoveRouteValues = new RouteValueDictionary {
{"Area", "Orchard.Blogs"},
{"Controller", "BlogPostAdmin"},
{"Action", "Delete"},
{"postId", context.ContentItem.Id},
{"blogId", blogPost.BlogPart.Id}
{"blogId", blogId}
};
}
}

View File

@@ -93,6 +93,20 @@ namespace Orchard.Blogs {
},
new MvcRouteHandler())
},
new RouteDescriptor {
Route = new Route(
"Admin/Blogs/Posts/CreateWithoutBlog",
new RouteValueDictionary {
{"area", "Orchard.Blogs"},
{"controller", "BlogPostAdmin"},
{"action", "CreateWithoutBlog"}
},
new RouteValueDictionary (),
new RouteValueDictionary {
{"area", "Orchard.Blogs"}
},
new MvcRouteHandler())
},
new RouteDescriptor {
Route = new Route(
"Admin/Blogs/{blogId}/Posts/{postId}/Edit",