Conditionally showing the page, post and blog edit links

- based on permissions, template inclusion determined by the respective driver
- added an Authorizer overload that takes no message for trimming authorizations (and anything else where sticking something into the notifier isn't necessary)

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-03-01 17:41:25 -08:00
parent 865fe96f6f
commit f0df9918ef
8 changed files with 51 additions and 33 deletions

View File

@@ -228,6 +228,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Dashboard\Views\Web.config" />
<Content Include="Themes\Styles\special.css" />
</ItemGroup>
<ItemGroup>
<Folder Include="Scheduling\Controllers\" />

View File

@@ -0,0 +1,3 @@
.manage a {
border:1px solid red;
}

View File

@@ -1,2 +1,6 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<% Html.RegisterScript("jquery-1.4.1.js"); // <- change to .min.js for use on a real site :) %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %><%
// a CSS file for styling things (e.g. content item edit buttons) for users with elevated privileges (in this case, anyone who is authenticated)
if (Request.IsAuthenticated) { Html.RegisterStyle("special.css"); }
Html.RegisterScript("jquery-1.4.1.js"); // <- change to .min.js for use on a real site :)
%>

View File

@@ -17,6 +17,8 @@ using Orchard.UI.Notify;
namespace Orchard.Blogs.Controllers {
[UsedImplicitly]
public class BlogDriver : ContentItemDriver<Blog> {
public IOrchardServices Services { get; set; }
public readonly static ContentType ContentType = new ContentType {
Name = "blog",
DisplayName = "Blog"
@@ -26,14 +28,13 @@ namespace Orchard.Blogs.Controllers {
private readonly IBlogService _blogService;
private readonly IBlogPostService _blogPostService;
private readonly IRoutableService _routableService;
private readonly IOrchardServices _orchardServices;
public BlogDriver(IContentManager contentManager, IBlogService blogService, IBlogPostService blogPostService, IRoutableService routableService, IOrchardServices orchardServices) {
public BlogDriver(IOrchardServices services, IContentManager contentManager, IBlogService blogService, IBlogPostService blogPostService, IRoutableService routableService) {
Services = services;
_contentManager = contentManager;
_blogService = blogService;
_blogPostService = blogPostService;
_routableService = routableService;
_orchardServices = orchardServices;
T = NullLocalizer.Instance;
}
@@ -81,7 +82,7 @@ namespace Orchard.Blogs.Controllers {
return Combined(
ContentItemTemplate("Items/Blogs.Blog").LongestMatch(displayType, "Summary", "DetailAdmin", "SummaryAdmin"),
ContentPartTemplate(blog, "Parts/Blogs.Blog.Manage").Location("primary:manage"),
Services.Authorizer.Authorize(Permissions.ManageBlogs) ? ContentPartTemplate(blog, "Parts/Blogs.Blog.Manage").Location("primary:manage") : null,
ContentPartTemplate(blog, "Parts/Blogs.Blog.Metadata").Location("primary:metadata"),
ContentPartTemplate(blog, "Parts/Blogs.Blog.Description").Location("primary"),
blogPosts == null ? null : ContentPartTemplate(blogPosts, "Parts/Blogs.BlogPost.List", "").Location("primary"));
@@ -134,7 +135,7 @@ namespace Orchard.Blogs.Controllers {
blog.Slug = _routableService.GenerateUniqueSlug(blog.Slug, slugsLikeThis);
if (originalSlug != blog.Slug)
_orchardServices.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously created blog so this blog now has the slug \"{1}\"",
Services.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously created blog so this blog now has the slug \"{1}\"",
originalSlug, blog.Slug));
}
}

View File

@@ -15,19 +15,19 @@ using Orchard.UI.Notify;
namespace Orchard.Blogs.Controllers {
[UsedImplicitly]
public class BlogPostDriver : ContentItemDriver<BlogPost> {
public IOrchardServices Services { get; set; }
private readonly IBlogPostService _blogPostService;
private readonly IRoutableService _routableService;
private readonly IOrchardServices _orchardServices;
public readonly static ContentType ContentType = new ContentType {
Name = "blogpost",
DisplayName = "Blog Post"
};
public BlogPostDriver(IBlogService blogService, IBlogPostService blogPostService, IRoutableService routableService, IOrchardServices orchardServices) {
public BlogPostDriver(IOrchardServices services, IBlogService blogService, IBlogPostService blogPostService, IRoutableService routableService) {
Services = services;
_blogPostService = blogPostService;
_routableService = routableService;
_orchardServices = orchardServices;
T = NullLocalizer.Instance;
}
@@ -66,7 +66,7 @@ namespace Orchard.Blogs.Controllers {
protected override DriverResult Display(BlogPost post, string displayType) {
return Combined(
ContentItemTemplate("Items/Blogs.BlogPost").LongestMatch(displayType, "Summary", "SummaryAdmin"),
ContentPartTemplate(post, "Parts/Blogs.BlogPost.Manage").Location("primary:manage"),
Services.Authorizer.Authorize(Permissions.EditOthersBlogPost) ? ContentPartTemplate(post, "Parts/Blogs.BlogPost.Manage").Location("primary:manage") : null,
ContentPartTemplate(post, "Parts/Blogs.BlogPost.Metadata").Location("primary:metadata"));
}
@@ -114,7 +114,7 @@ namespace Orchard.Blogs.Controllers {
//todo: (heskew) need better messages
if (slugsLikeThis.Count() > 0) {
//todo: (heskew) need better messages
_orchardServices.Notifier.Warning(T("A different blog post is already published with this same slug."));
Services.Notifier.Warning(T("A different blog post is already published with this same slug."));
if (post.ContentItem.VersionRecord == null || post.ContentItem.VersionRecord.Published)
{
@@ -123,7 +123,7 @@ namespace Orchard.Blogs.Controllers {
post.Slug = _routableService.GenerateUniqueSlug(post.Slug, slugsLikeThis);
if (originalSlug != post.Slug)
_orchardServices.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously created blog post so this post now has the slug \"{1}\"",
Services.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously created blog post so this post now has the slug \"{1}\"",
originalSlug, post.Slug));
}
}

View File

@@ -13,19 +13,19 @@ using Orchard.UI.Notify;
namespace Orchard.Pages.Controllers {
public class PageDriver : ContentItemDriver<Page> {
public IOrchardServices Services { get; set; }
private readonly IPageService _pageService;
private readonly IRoutableService _routableService;
private readonly IOrchardServices _orchardServices;
public readonly static ContentType ContentType = new ContentType {
Name = "page",
DisplayName = "Page"
};
public PageDriver(IPageService pageService, IRoutableService routableService, IOrchardServices orchardServices) {
public PageDriver(IOrchardServices services, IPageService pageService, IRoutableService routableService) {
Services = services;
_pageService = pageService;
_routableService = routableService;
_orchardServices = orchardServices;
T = NullLocalizer.Instance;
}
@@ -62,7 +62,7 @@ namespace Orchard.Pages.Controllers {
protected override DriverResult Display(Page page, string displayType) {
return Combined(
ContentItemTemplate("Items/Pages.Page").LongestMatch(displayType, "Summary", "SummaryAdmin"),
ContentPartTemplate(page, "Parts/Pages.Page.Manage").Location("primary:manage"),
Services.Authorizer.Authorize(Permissions.EditOthersPages) ? ContentPartTemplate(page, "Parts/Pages.Page.Manage").Location("primary:manage") : null,
ContentPartTemplate(page, "Parts/Pages.Page.Metadata").Location("primary:metadata"));
}
@@ -108,7 +108,7 @@ namespace Orchard.Pages.Controllers {
if (slugsLikeThis.Count() > 0) {
//todo: (heskew) need better messages
_orchardServices.Notifier.Warning(T("A different page is already published with this same slug."));
Services.Notifier.Warning(T("A different page is already published with this same slug."));
if (page.ContentItem.VersionRecord == null || page.ContentItem.VersionRecord.Published) {
var originalSlug = page.Slug;
@@ -117,7 +117,7 @@ namespace Orchard.Pages.Controllers {
//todo: (heskew) need better messages
if (originalSlug != page.Slug)
_orchardServices.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously published page so this page now has the slug \"{1}\"",
Services.Notifier.Warning(T("Slugs in conflict. \"{0}\" is already set for a previously published page so this page now has the slug \"{1}\"",
originalSlug, page.Slug));
}
}

View File

@@ -1,12 +1,15 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<ContentItemViewModel<BlogPost>>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Blogs.Extensions"%>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<h1><%=Html.TitleForPage(Model.Item.Title)%></h1>
<div class="metadata">
<%-- Sorry, Jon. I need to figure out how we can make this markup possible with the recent metadata/manage split.
We can still do it this way but there's isn't yet a story for UI conditional on permissions.
What I have in this template is as close as I can get at the moment. --%>
<%--<div class="metadata">
<% if (Model.Item.Creator != null) {
%><div class="posted"><%=_Encoded("Posted by {0} {1}", Model.Item.Creator.UserName, Html.PublishedWhen(Model.Item)) %><%-- | <a href="<%=Url.BlogPostEdit(Model.Item.Blog.Slug, Model.Item.Id) %>" class="ibutton edit"><%=_Encoded("Edit") %></a>--%></div><%
%><div class="posted"><%=_Encoded("Posted by {0} {1}", Model.Item.Creator.UserName, Html.PublishedWhen(Model.Item)) %><% -- | <a href="<%=Url.BlogPostEdit(Model.Item.Blog.Slug, Model.Item.Id) %>" class="ibutton edit"><%=_Encoded("Edit") %></a>-- %></div><%
} %>
</div>
<% Html.Zone("primary");
--%>
<% Html.Zone("primary", ":metadata :manage"); // <- flipping metadata and manage to get closer to the desired markup
Html.ZonesAny(); %>

View File

@@ -1,13 +1,12 @@
using System;
using JetBrains.Annotations;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.Localization;
using Orchard.Security.Permissions;
using Orchard.UI.Notify;
namespace Orchard.Security {
public interface IAuthorizer : IDependency {
bool Authorize(Permission permission);
bool Authorize(Permission permission, LocalizedString message);
bool Authorize(Permission permission, IContent content, LocalizedString message);
}
@@ -27,6 +26,10 @@ namespace Orchard.Security {
protected virtual IUser CurrentUser { get; [UsedImplicitly] private set; }
public Localizer T { get; set; }
public bool Authorize(Permission permission) {
return Authorize(permission, null, null);
}
public bool Authorize(Permission permission, LocalizedString message) {
return Authorize(permission, null, message);
}
@@ -35,14 +38,17 @@ namespace Orchard.Security {
if (_authorizationService.TryCheckAccess(permission, CurrentUser, content))
return true;
if (CurrentUser == null) {
_notifier.Error(T("{0}. Anonymous users do not have {1} permission.",
message, permission.Name));
}
else {
_notifier.Error(T("{0}. Current user, {2}, does not have {1} permission.",
message, permission.Name, CurrentUser.UserName));
if (message != null) {
if (CurrentUser == null) {
_notifier.Error(T("{0}. Anonymous users do not have {1} permission.",
message, permission.Name));
}
else {
_notifier.Error(T("{0}. Current user, {2}, does not have {1} permission.",
message, permission.Name, CurrentUser.UserName));
}
}
return false;
}