mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 02:44:52 +08:00
Adding simple permissions attached to menu items
--HG-- extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4042808
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Security;
|
||||
using Orchard.Security.Permissions;
|
||||
using Orchard.UI.Navigation;
|
||||
|
||||
namespace Orchard.Tests.UI.Navigation {
|
||||
@@ -10,15 +12,21 @@ namespace Orchard.Tests.UI.Navigation {
|
||||
public class NavigationManagerTests {
|
||||
[Test]
|
||||
public void EmptyMenuIfNameDoesntMatch() {
|
||||
var manager = new NavigationManager(new[] { new StubProvider() });
|
||||
var manager = new NavigationManager(new[] { new StubProvider() }, new StubAuth());
|
||||
|
||||
var menuItems = manager.BuildMenu("primary");
|
||||
Assert.That(menuItems.Count(), Is.EqualTo(0));
|
||||
}
|
||||
|
||||
public class StubAuth : IAuthorizationService {
|
||||
public bool CheckAccess(IUser user, Permission permission) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NavigationManagerShouldUseProvidersToBuildNamedMenu() {
|
||||
var manager = new NavigationManager(new[] { new StubProvider() });
|
||||
var manager = new NavigationManager(new[] { new StubProvider() }, new StubAuth());
|
||||
|
||||
var menuItems = manager.BuildMenu("admin");
|
||||
Assert.That(menuItems.Count(), Is.EqualTo(2));
|
||||
@@ -30,7 +38,7 @@ namespace Orchard.Tests.UI.Navigation {
|
||||
|
||||
[Test]
|
||||
public void NavigationManagerShouldMergeAndOrderNavigation() {
|
||||
var manager = new NavigationManager(new INavigationProvider[] { new StubProvider(), new Stub2Provider() });
|
||||
var manager = new NavigationManager(new INavigationProvider[] { new StubProvider(), new Stub2Provider() }, new StubAuth());
|
||||
|
||||
var menuItems = manager.BuildMenu("admin");
|
||||
Assert.That(menuItems.Count(), Is.EqualTo(3));
|
||||
|
@@ -8,7 +8,7 @@ namespace Orchard.CmsPages {
|
||||
builder.Add("Pages", "1",
|
||||
menu => menu
|
||||
.Add("Manage Pages", "1.0", item => item.Action("Index", "Admin", new { area = "Orchard.CmsPages" }))
|
||||
.Add("Add a Page", "1.1", item => item.Action("Create", "Admin", new { area = "Orchard.CmsPages" }))
|
||||
.Add("Add a Page", "1.1", item => item.Action("Create", "Admin", new { area = "Orchard.CmsPages" }).Permission(Permissions.CreatePages))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Security.Permissions;
|
||||
|
||||
namespace Orchard.UI.Navigation {
|
||||
public class MenuItem {
|
||||
public MenuItem() {
|
||||
Permissions = Enumerable.Empty<Permission>();
|
||||
}
|
||||
|
||||
public string Text { get; set; }
|
||||
public string Position { get; set; }
|
||||
public RouteValueDictionary RouteValues { get; set; }
|
||||
public IEnumerable<MenuItem> Items { get; set; }
|
||||
public IEnumerable<Permission> Permissions { get; set; }
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Routing;
|
||||
using Orchard.Security.Permissions;
|
||||
|
||||
namespace Orchard.UI.Navigation {
|
||||
public class NavigationBuilder {
|
||||
@@ -56,6 +57,11 @@ namespace Orchard.UI.Navigation {
|
||||
return this;
|
||||
}
|
||||
|
||||
public NavigationItemBuilder Permission(Permission permission) {
|
||||
_item.Permissions = _item.Permissions.Concat(new[]{permission});
|
||||
return this;
|
||||
}
|
||||
|
||||
public new IEnumerable<MenuItem> Build() {
|
||||
_item.Items = base.Build();
|
||||
return new[] { _item };
|
||||
|
@@ -1,7 +1,9 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Security;
|
||||
|
||||
namespace Orchard.UI.Navigation {
|
||||
public interface INavigationManager : IDependency {
|
||||
@@ -10,13 +12,32 @@ namespace Orchard.UI.Navigation {
|
||||
|
||||
public class NavigationManager : INavigationManager {
|
||||
private readonly IEnumerable<INavigationProvider> _providers;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
|
||||
public NavigationManager(IEnumerable<INavigationProvider> providers) {
|
||||
public NavigationManager(IEnumerable<INavigationProvider> providers, IAuthorizationService authorizationService) {
|
||||
_providers = providers;
|
||||
_authorizationService = authorizationService;
|
||||
}
|
||||
|
||||
public IUser CurrentUser { get; set; }
|
||||
|
||||
public IEnumerable<MenuItem> BuildMenu(string menuName) {
|
||||
return Merge(AllSources(menuName)).ToArray();
|
||||
return Reduce(Merge(AllSources(menuName))).ToArray();
|
||||
}
|
||||
|
||||
private IEnumerable<MenuItem> Reduce(IEnumerable<MenuItem> items) {
|
||||
foreach(var item in items) {
|
||||
if (!item.Permissions.Any() ||
|
||||
item.Permissions.Any(x=>_authorizationService.CheckAccess(CurrentUser, x))) {
|
||||
yield return new MenuItem {
|
||||
Items = Reduce(item.Items),
|
||||
Permissions = item.Permissions,
|
||||
Position = item.Position,
|
||||
RouteValues = item.RouteValues,
|
||||
Text = item.Text,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<IEnumerable<MenuItem>> AllSources(string menuName) {
|
||||
@@ -46,7 +67,8 @@ namespace Orchard.UI.Navigation {
|
||||
Text = items.First().Text,
|
||||
RouteValues = items.First().RouteValues,
|
||||
Items = Merge(items.Select(x => x.Items)).ToArray(),
|
||||
Position = SelectBestPositionValue(items.Select(x => x.Position))
|
||||
Position = SelectBestPositionValue(items.Select(x => x.Position)),
|
||||
Permissions = items.SelectMany(x=>x.Permissions),
|
||||
};
|
||||
return joined;
|
||||
}
|
||||
|
Reference in New Issue
Block a user