mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00
Merge perf -> dev
--HG-- branch : dev
This commit is contained in:
@@ -3,14 +3,35 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Autofac;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Caching;
|
||||
using Orchard.Environment.Extensions.Models;
|
||||
using Orchard.Roles.Models;
|
||||
using Orchard.Roles.Services;
|
||||
using Orchard.Security.Permissions;
|
||||
using Orchard.Tests.Stubs;
|
||||
|
||||
namespace Orchard.Tests.Modules.Roles.Services {
|
||||
[TestFixture]
|
||||
public class RoleServiceTests : DatabaseEnabledTestsBase{
|
||||
public class RoleServiceTests : DatabaseEnabledTestsBase {
|
||||
public override void Register(ContainerBuilder builder) {
|
||||
builder.RegisterType<RoleService>().As<IRoleService>();
|
||||
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
|
||||
builder.RegisterType<Signals>().As<ISignals>();
|
||||
builder.RegisterType<TestPermissionProvider>().As<IPermissionProvider>();
|
||||
}
|
||||
|
||||
public class TestPermissionProvider : IPermissionProvider {
|
||||
public Feature Feature {
|
||||
get { return new Feature { Descriptor = new FeatureDescriptor { Id = "RoleServiceTests" } }; }
|
||||
}
|
||||
|
||||
public IEnumerable<Permission> GetPermissions() {
|
||||
return "alpha,beta,gamma,delta".Split(',').Select(name => new Permission { Name = name });
|
||||
}
|
||||
|
||||
public IEnumerable<PermissionStereotype> GetDefaultStereotypes() {
|
||||
return Enumerable.Empty<PermissionStereotype>();
|
||||
}
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
@@ -34,5 +55,36 @@ namespace Orchard.Tests.Modules.Roles.Services {
|
||||
Assert.That(roles, Has.Some.Property("Name").EqualTo("two"));
|
||||
Assert.That(roles, Has.Some.Property("Name").EqualTo("three"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PermissionChangesShouldBeVisibleImmediately() {
|
||||
|
||||
var service = _container.Resolve<IRoleService>();
|
||||
|
||||
ClearSession();
|
||||
{
|
||||
service.CreateRole("test");
|
||||
var roleId = service.GetRoleByName("test").Id;
|
||||
service.UpdateRole(roleId, "test", new[] { "alpha", "beta", "gamma" });
|
||||
}
|
||||
|
||||
ClearSession();
|
||||
{
|
||||
var result = service.GetPermissionsForRoleByName("test");
|
||||
Assert.That(result.Count(), Is.EqualTo(3));
|
||||
}
|
||||
|
||||
ClearSession();
|
||||
{
|
||||
var roleId = service.GetRoleByName("test").Id;
|
||||
service.UpdateRole(roleId, "test", new[] { "alpha", "beta", "gamma", "delta" });
|
||||
}
|
||||
|
||||
ClearSession();
|
||||
{
|
||||
var result = service.GetPermissionsForRoleByName("test");
|
||||
Assert.That(result.Count(), Is.EqualTo(4));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -13,5 +13,7 @@ namespace Orchard.Roles.Services {
|
||||
void DeleteRole(int id);
|
||||
IDictionary<string, IEnumerable<Permission>> GetInstalledPermissions();
|
||||
IEnumerable<string> GetPermissionsForRole(int id);
|
||||
|
||||
IEnumerable<string> GetPermissionsForRoleByName(string name);
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.Caching;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
@@ -11,16 +12,24 @@ using Orchard.Security.Permissions;
|
||||
namespace Orchard.Roles.Services {
|
||||
[UsedImplicitly]
|
||||
public class RoleService : IRoleService {
|
||||
private const string SignalName = "Orchard.Roles.Services.RoleService";
|
||||
|
||||
private readonly IRepository<RoleRecord> _roleRepository;
|
||||
private readonly IRepository<PermissionRecord> _permissionRepository;
|
||||
private readonly IEnumerable<IPermissionProvider> _permissionProviders;
|
||||
private readonly ICacheManager _cacheManager;
|
||||
private readonly ISignals _signals;
|
||||
|
||||
public RoleService(IRepository<RoleRecord> roleRepository,
|
||||
IRepository<PermissionRecord> permissionRepository,
|
||||
IEnumerable<IPermissionProvider> permissionProviders) {
|
||||
IEnumerable<IPermissionProvider> permissionProviders,
|
||||
ICacheManager cacheManager,
|
||||
ISignals signals) {
|
||||
_roleRepository = roleRepository;
|
||||
_permissionRepository = permissionRepository;
|
||||
_permissionProviders = permissionProviders;
|
||||
_cacheManager = cacheManager;
|
||||
_signals = signals;
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
@@ -43,6 +52,7 @@ namespace Orchard.Roles.Services {
|
||||
|
||||
public void CreateRole(string roleName) {
|
||||
_roleRepository.Create(new RoleRecord { Name = roleName });
|
||||
TriggerSignal();
|
||||
}
|
||||
|
||||
public void CreatePermissionForRole(string roleName, string permissionName) {
|
||||
@@ -56,6 +66,7 @@ namespace Orchard.Roles.Services {
|
||||
RoleRecord roleRecord = GetRoleByName(roleName);
|
||||
PermissionRecord permissionRecord = _permissionRepository.Get(x => x.Name == permissionName);
|
||||
roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });
|
||||
TriggerSignal();
|
||||
}
|
||||
|
||||
public void UpdateRole(int id, string roleName, IEnumerable<string> rolePermissions) {
|
||||
@@ -73,6 +84,7 @@ namespace Orchard.Roles.Services {
|
||||
}
|
||||
PermissionRecord permissionRecord = _permissionRepository.Get(x => x.Name == permission);
|
||||
roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });
|
||||
TriggerSignal();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +112,7 @@ namespace Orchard.Roles.Services {
|
||||
|
||||
public void DeleteRole(int id) {
|
||||
_roleRepository.Delete(GetRole(id));
|
||||
TriggerSignal();
|
||||
}
|
||||
|
||||
public IDictionary<string, IEnumerable<Permission>> GetInstalledPermissions() {
|
||||
@@ -107,13 +120,13 @@ namespace Orchard.Roles.Services {
|
||||
foreach (var permissionProvider in _permissionProviders) {
|
||||
var featureName = permissionProvider.Feature.Descriptor.Id;
|
||||
var permissions = permissionProvider.GetPermissions();
|
||||
foreach(var permission in permissions) {
|
||||
foreach (var permission in permissions) {
|
||||
var category = permission.Category;
|
||||
|
||||
string title = String.IsNullOrWhiteSpace(category) ? T("{0} Feature", featureName).Text : T(category).Text;
|
||||
|
||||
if ( installedPermissions.ContainsKey(title) )
|
||||
installedPermissions[title] = installedPermissions[title].Concat( new [] {permission} );
|
||||
if (installedPermissions.ContainsKey(title))
|
||||
installedPermissions[title] = installedPermissions[title].Concat(new[] { permission });
|
||||
else
|
||||
installedPermissions.Add(title, new[] { permission });
|
||||
}
|
||||
@@ -130,5 +143,28 @@ namespace Orchard.Roles.Services {
|
||||
}
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetPermissionsForRoleByName(string name) {
|
||||
return _cacheManager.Get(name, ctx => {
|
||||
MonitorSignal(ctx);
|
||||
return GetPermissionsForRoleByNameInner(name);
|
||||
});
|
||||
}
|
||||
|
||||
IEnumerable<string> GetPermissionsForRoleByNameInner(string name) {
|
||||
var roleRecord = GetRoleByName(name);
|
||||
if (roleRecord == null)
|
||||
return Enumerable.Empty<string>();
|
||||
return GetPermissionsForRole(roleRecord.Id);
|
||||
}
|
||||
|
||||
|
||||
private void MonitorSignal(AcquireContext<string> ctx) {
|
||||
ctx.Monitor(_signals.When(SignalName));
|
||||
}
|
||||
|
||||
private void TriggerSignal() {
|
||||
_signals.Trigger(SignalName);
|
||||
}
|
||||
}
|
||||
}
|
@@ -71,10 +71,7 @@ namespace Orchard.Roles.Services {
|
||||
}
|
||||
|
||||
foreach (var role in rolesToExamine) {
|
||||
RoleRecord roleRecord = _roleService.GetRoleByName(role);
|
||||
if ( roleRecord == null )
|
||||
continue;
|
||||
foreach (var permissionName in _roleService.GetPermissionsForRole(roleRecord.Id)) {
|
||||
foreach (var permissionName in _roleService.GetPermissionsForRoleByName(role)) {
|
||||
string possessedName = permissionName;
|
||||
if (grantingNames.Any(grantingName => String.Equals(possessedName, grantingName, StringComparison.OrdinalIgnoreCase))) {
|
||||
context.Granted = true;
|
||||
|
Reference in New Issue
Block a user