Move RoleRuleProvider to RoleCondition

This commit is contained in:
Thierry Fleury
2015-12-21 09:35:30 +01:00
parent 2d66897ce7
commit e519e2dc36
3 changed files with 37 additions and 37 deletions

View File

@@ -0,0 +1,32 @@
using Orchard.Conditions.Services;
using Orchard.ContentManagement;
using Orchard.Roles.Models;
using Orchard.Security;
using System;
using System.Linq;
namespace Orchard.Roles.Conditions {
public class RoleRuleProvider : IConditionProvider {
private readonly IAuthenticationService _authenticationService;
public RoleRuleProvider(IAuthenticationService authenticationService) {
_authenticationService = authenticationService;
}
public void Evaluate(ConditionEvaluationContext evaluationContext) {
if (!String.Equals(evaluationContext.FunctionName, "role", StringComparison.OrdinalIgnoreCase)) {
return;
}
var user = _authenticationService.GetAuthenticatedUser();
if (user == null) {
evaluationContext.Result = false;
return;
}
var roles = evaluationContext.Arguments.Cast<string>();
var userRoles = user.As<IUserRoles>();
evaluationContext.Result = userRoles != null ? userRoles.Roles.Intersect(roles).Count() > 0 : false;
}
}
}

View File

@@ -125,7 +125,7 @@
<Compile Include="Models\UserRolesPartRecord.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RuleEngine\RoleRuleProvider.cs" />
<Compile Include="Conditions\RoleCondition.cs" />
<Compile Include="Services\IRoleService.cs" />
<Compile Include="Services\RolesBasedAuthorizationService.cs" />
<Compile Include="Services\RoleService.cs" />
@@ -157,6 +157,10 @@
<Name>Orchard.Core</Name>
<Private>false</Private>
</ProjectReference>
<ProjectReference Include="..\Orchard.Conditions\Orchard.Conditions.csproj">
<Project>{98251eae-a41b-47b2-aa91-e28b8482da70}</Project>
<Name>Orchard.Conditions</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Forms\Orchard.Forms.csproj">
<Project>{642a49d7-8752-4177-80d6-bfbbcfad3de0}</Project>
<Name>Orchard.Forms</Name>

View File

@@ -1,36 +0,0 @@
using Orchard.ContentManagement;
using Orchard.Events;
using Orchard.Roles.Models;
using Orchard.Security;
using System;
using System.Linq;
namespace Orchard.Roles.RuleEngine {
public interface IRuleProvider : IEventHandler {
void Process(dynamic ruleContext);
}
public class RoleRuleProvider : IRuleProvider {
private readonly IAuthenticationService _authenticationService;
public RoleRuleProvider(IAuthenticationService authenticationService) {
_authenticationService = authenticationService;
}
public void Process(dynamic ruleContext) {
if (!String.Equals(ruleContext.FunctionName, "role", StringComparison.OrdinalIgnoreCase)) {
return;
}
var user = _authenticationService.GetAuthenticatedUser();
if (user == null) {
ruleContext.Result = false;
return;
}
var roles = ((object[])ruleContext.Arguments).Cast<string>();
var userRoles = user.As<IUserRoles>();
ruleContext.Result = userRoles != null ? userRoles.Roles.Intersect(roles).Count() > 0 : false;
}
}
}