mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00

- The ResultFilter uses the rule manager to filter out layers and widgets. - Added rules to default layers. - RuleManager with callback injection into the dlr scripting sandbox. - RuleProvider implementations for Url and Authenticated. Anonymous is just "not Authenticated". - RuleContext. - Renaming Layer.Rule to Layer.LayerRule. NHibernate bug for SqlCe most likely, couldn't escape "Rule" as expected. - Unit tests. --HG-- branch : dev
58 lines
2.2 KiB
C#
58 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Orchard.Scripting;
|
|
using Orchard.UI.Widgets;
|
|
|
|
namespace Orchard.Widgets.RuleEngine {
|
|
public class RuleManager : IRuleManager {
|
|
private readonly IEnumerable<IRuleProvider> _ruleProviders;
|
|
private readonly IScriptingManager _scriptingManager;
|
|
|
|
public RuleManager(IEnumerable<IRuleProvider> ruleProviders, IScriptingManager scriptingManager) {
|
|
_ruleProviders = ruleProviders;
|
|
_scriptingManager = scriptingManager;
|
|
}
|
|
|
|
public bool Matches(string expression) {
|
|
_scriptingManager.SetVariable("callbacks", new CallbackApi(this));
|
|
dynamic execContext = _scriptingManager.Eval(@"
|
|
class ExecContext
|
|
def initialize(callbacks)
|
|
@callbacks = callbacks;
|
|
end
|
|
|
|
def execute(text)
|
|
instance_eval(text.to_s);
|
|
end
|
|
|
|
def method_missing(name, *args, &block)
|
|
@callbacks.send(name, args, &block);
|
|
end
|
|
end
|
|
ExecContext.new(callbacks)");
|
|
return execContext.execute(expression);
|
|
}
|
|
|
|
public class CallbackApi {
|
|
private readonly RuleManager _ruleManager;
|
|
|
|
public CallbackApi(RuleManager ruleManager) {
|
|
_ruleManager = ruleManager;
|
|
}
|
|
|
|
public object send(string name, IList<object> args) {
|
|
return _ruleManager.Evaluate(name, args);
|
|
}
|
|
}
|
|
|
|
private object Evaluate(string name, IList<object> args) {
|
|
RuleContext ruleContext = new RuleContext {FunctionName = name, Arguments = args.ToArray()};
|
|
|
|
foreach (var ruleProvider in _ruleProviders) {
|
|
ruleProvider.Process(ruleContext);
|
|
}
|
|
|
|
return ruleContext.Result;
|
|
}
|
|
}
|
|
} |