mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Move RoleRuleProvider to RoleCondition
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user