mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Merge
--HG-- branch : dev
This commit is contained in:
@@ -3,14 +3,35 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using Orchard.Caching;
|
||||||
|
using Orchard.Environment.Extensions.Models;
|
||||||
using Orchard.Roles.Models;
|
using Orchard.Roles.Models;
|
||||||
using Orchard.Roles.Services;
|
using Orchard.Roles.Services;
|
||||||
|
using Orchard.Security.Permissions;
|
||||||
|
using Orchard.Tests.Stubs;
|
||||||
|
|
||||||
namespace Orchard.Tests.Modules.Roles.Services {
|
namespace Orchard.Tests.Modules.Roles.Services {
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class RoleServiceTests : DatabaseEnabledTestsBase {
|
public class RoleServiceTests : DatabaseEnabledTestsBase {
|
||||||
public override void Register(ContainerBuilder builder) {
|
public override void Register(ContainerBuilder builder) {
|
||||||
builder.RegisterType<RoleService>().As<IRoleService>();
|
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 {
|
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("two"));
|
||||||
Assert.That(roles, Has.Some.Property("Name").EqualTo("three"));
|
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);
|
void DeleteRole(int id);
|
||||||
IDictionary<string, IEnumerable<Permission>> GetInstalledPermissions();
|
IDictionary<string, IEnumerable<Permission>> GetInstalledPermissions();
|
||||||
IEnumerable<string> GetPermissionsForRole(int id);
|
IEnumerable<string> GetPermissionsForRole(int id);
|
||||||
|
|
||||||
|
IEnumerable<string> GetPermissionsForRoleByName(string name);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using Orchard.Caching;
|
||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.Localization;
|
using Orchard.Localization;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
@@ -11,16 +12,24 @@ using Orchard.Security.Permissions;
|
|||||||
namespace Orchard.Roles.Services {
|
namespace Orchard.Roles.Services {
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public class RoleService : IRoleService {
|
public class RoleService : IRoleService {
|
||||||
|
private const string SignalName = "Orchard.Roles.Services.RoleService";
|
||||||
|
|
||||||
private readonly IRepository<RoleRecord> _roleRepository;
|
private readonly IRepository<RoleRecord> _roleRepository;
|
||||||
private readonly IRepository<PermissionRecord> _permissionRepository;
|
private readonly IRepository<PermissionRecord> _permissionRepository;
|
||||||
private readonly IEnumerable<IPermissionProvider> _permissionProviders;
|
private readonly IEnumerable<IPermissionProvider> _permissionProviders;
|
||||||
|
private readonly ICacheManager _cacheManager;
|
||||||
|
private readonly ISignals _signals;
|
||||||
|
|
||||||
public RoleService(IRepository<RoleRecord> roleRepository,
|
public RoleService(IRepository<RoleRecord> roleRepository,
|
||||||
IRepository<PermissionRecord> permissionRepository,
|
IRepository<PermissionRecord> permissionRepository,
|
||||||
IEnumerable<IPermissionProvider> permissionProviders) {
|
IEnumerable<IPermissionProvider> permissionProviders,
|
||||||
|
ICacheManager cacheManager,
|
||||||
|
ISignals signals) {
|
||||||
_roleRepository = roleRepository;
|
_roleRepository = roleRepository;
|
||||||
_permissionRepository = permissionRepository;
|
_permissionRepository = permissionRepository;
|
||||||
_permissionProviders = permissionProviders;
|
_permissionProviders = permissionProviders;
|
||||||
|
_cacheManager = cacheManager;
|
||||||
|
_signals = signals;
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
T = NullLocalizer.Instance;
|
T = NullLocalizer.Instance;
|
||||||
}
|
}
|
||||||
@@ -43,6 +52,7 @@ namespace Orchard.Roles.Services {
|
|||||||
|
|
||||||
public void CreateRole(string roleName) {
|
public void CreateRole(string roleName) {
|
||||||
_roleRepository.Create(new RoleRecord { Name = roleName });
|
_roleRepository.Create(new RoleRecord { Name = roleName });
|
||||||
|
TriggerSignal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreatePermissionForRole(string roleName, string permissionName) {
|
public void CreatePermissionForRole(string roleName, string permissionName) {
|
||||||
@@ -56,6 +66,7 @@ namespace Orchard.Roles.Services {
|
|||||||
RoleRecord roleRecord = GetRoleByName(roleName);
|
RoleRecord roleRecord = GetRoleByName(roleName);
|
||||||
PermissionRecord permissionRecord = _permissionRepository.Get(x => x.Name == permissionName);
|
PermissionRecord permissionRecord = _permissionRepository.Get(x => x.Name == permissionName);
|
||||||
roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });
|
roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });
|
||||||
|
TriggerSignal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateRole(int id, string roleName, IEnumerable<string> rolePermissions) {
|
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);
|
PermissionRecord permissionRecord = _permissionRepository.Get(x => x.Name == permission);
|
||||||
roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });
|
roleRecord.RolesPermissions.Add(new RolesPermissionsRecord { Permission = permissionRecord, Role = roleRecord });
|
||||||
|
TriggerSignal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,6 +112,7 @@ namespace Orchard.Roles.Services {
|
|||||||
|
|
||||||
public void DeleteRole(int id) {
|
public void DeleteRole(int id) {
|
||||||
_roleRepository.Delete(GetRole(id));
|
_roleRepository.Delete(GetRole(id));
|
||||||
|
TriggerSignal();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IDictionary<string, IEnumerable<Permission>> GetInstalledPermissions() {
|
public IDictionary<string, IEnumerable<Permission>> GetInstalledPermissions() {
|
||||||
@@ -130,5 +143,28 @@ namespace Orchard.Roles.Services {
|
|||||||
}
|
}
|
||||||
return permissions;
|
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) {
|
foreach (var role in rolesToExamine) {
|
||||||
RoleRecord roleRecord = _roleService.GetRoleByName(role);
|
foreach (var permissionName in _roleService.GetPermissionsForRoleByName(role)) {
|
||||||
if ( roleRecord == null )
|
|
||||||
continue;
|
|
||||||
foreach (var permissionName in _roleService.GetPermissionsForRole(roleRecord.Id)) {
|
|
||||||
string possessedName = permissionName;
|
string possessedName = permissionName;
|
||||||
if (grantingNames.Any(grantingName => String.Equals(possessedName, grantingName, StringComparison.OrdinalIgnoreCase))) {
|
if (grantingNames.Any(grantingName => String.Equals(possessedName, grantingName, StringComparison.OrdinalIgnoreCase))) {
|
||||||
context.Granted = true;
|
context.Granted = true;
|
||||||
|
Reference in New Issue
Block a user