mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Close #2027 : Add role rule in Orchard.Widgets module
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
<IISExpressAnonymousAuthentication />
|
<IISExpressAnonymousAuthentication />
|
||||||
<IISExpressWindowsAuthentication />
|
<IISExpressWindowsAuthentication />
|
||||||
<IISExpressUseClassicPipelineMode />
|
<IISExpressUseClassicPipelineMode />
|
||||||
|
<UseGlobalApplicationHostFile />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@@ -101,6 +102,7 @@
|
|||||||
<Compile Include="Models\UserRolesPartRecord.cs" />
|
<Compile Include="Models\UserRolesPartRecord.cs" />
|
||||||
<Compile Include="Permissions.cs" />
|
<Compile Include="Permissions.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="RuleEngine\RoleRuleProvider.cs" />
|
||||||
<Compile Include="Services\IRoleService.cs" />
|
<Compile Include="Services\IRoleService.cs" />
|
||||||
<Compile Include="Services\RolesBasedAuthorizationService.cs" />
|
<Compile Include="Services\RolesBasedAuthorizationService.cs" />
|
||||||
<Compile Include="Services\RoleService.cs" />
|
<Compile Include="Services\RoleService.cs" />
|
||||||
|
@@ -0,0 +1,36 @@
|
|||||||
|
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