我需要使用规则引擎来实现系统中的角色权限(这可能是过火了吗?)然而,权限本身是复杂和复杂的。我对如何授予访问权限或不使用规则引擎感到困惑。
我还怀疑我应该使用的设计,以便以可伸缩和可维护的方式实现它。因此,任何帮助设计或向我解释如何使用规则引擎将是很好的。
使用鼻涕、mongoDB、node.js作为后端。
我在考虑创建一个封装Nools的规则引擎实例(可能是一个反模式的内平台?)在我的node.js应用程序的引导程序中,让它成为一个全局变量。
类似于:
'use strict';
var nools = require('nools');
var flows = require('./rule-engine.flows');
// A flow is a container of rules, so each flow could be a category of rules
// In the same flow could have some more specific subcategories would be actionGroups?
// I'm creating a ruleEngine instance that would contain nools but I'm not sure
// if that would be a good practice, I have that to maybe encapsulate boilerplate code
// or some straight forward operations in the future.. don't sure of this.
var ruleEngine = function(){
this.nools = nools;
};
ruleEngine.prototype.getFlowSession = function(flow){
if(this.nools.hasFlow(flow)){
return this.nools.getFlow(flow).getSession();
}else{
var fl = this.nools.flow(flow, flows[flow]);
return fl.getSession();
}
};
ruleEngine.prototype.createRule = function(flow, rule){
if(this.nools.hasFlow(flow)){
// implementation to add rules to a flow
}
};
ruleEngine.prototype.editRule = function(flow, rule, update){};
ruleEngine.prototype.retractRule = function(flow, rule){};
//could be global object, or cache object but certainly should be a single object.
if(!GLOBAL.ruleEngine){
var ruleEngineInstance = new ruleEngine();
GLOBAL.ruleEngine = ruleEngineInstance;
}
//module.exports = GLOBAL.ruleEngine;规则-工程流程:
'use strict';
var flowName = function(flow){
// query the rules to database or to cache.. then add the rules to the flow.
// query bla bla function(results){
for(Var i=0; i<results.length; i++)
flow.rule(results[i].name, results[i].constraints, results[i].action);
// alternately, I could just from the bootstrap create a flow,
// and create a function to add, modify or retract rules of a specific flow.
// What would be the better design approach ? or combine two approach ?
// bring from database the first time, and later use ruleModify,
// ruleCreate or rule retract functions.
};
module.exports = {
flowName: flowName,
// each would be a flow that would be a category of rules for the system
flowName2: flowName2
};如何使用它来实现权限,是通过事件与规则引擎和外部应用程序/代码通信的唯一方法?
这些是我创建的一些规则(同时也是用来创建模拟缓存规则或MongoDB规则的MongoDB规则)。
var results = [
{
name: 'userAllow',
constraints: [Object, 'obj', 'obj.systemRole === \'user\''],
action: function(facts, session, next){
session.emit('event:userAllow', {data: 'user is allow'});
next();
}
},
{
name: 'userNotAllow',
constraints: [Object, 'obj', 'obj.systemRole !== \'user\''],
action: function(facts, session, next){
session.emit('event:userNotAllow', {data: 'user is not allow'});
next();
}
},
{
name: 'adminAllow',
constraints: [Object, 'obj', 'obj.systemRole === \'admin\''],
action: function(facts, session, next){
session.emit('event:adminAllow', {data: 'admin is allow!'});
next();
}
},
{
name: 'adminNotAllow',
constraints: [Object, 'obj', 'obj.systemRole !== \'admin\''],
action: function(facts, session, next){
session.emit('event:adminNotAllow', {data: 'admin not allow'});
next();
}
}
];因此,有了这几条规则,我只想在user.systemRole是管理员时授予访问权限。我应该以下列方式使用事件吗?
系统中的X方法:
//validate delete with ruleEngine... supposed only admin would be able to delete
var self = this;
var session = ruleEngine.getFlowSession('flowName');
session.assert({systemRole: User.role}); //User.role = 'user' || 'admin'
session.on('event:adminAllow', function(d){
console.log('do the job because the user is admin');
// Delete implementation.
});
session.on('event:adminNotAllow', function(d){
console.log('User not allow because is not admin');
});
session.on('fire',function(name){
console.log(name);
});
session.match().then(function(){
session.dispose();
});到目前为止,我对这个实现有一些问题。事件可以触发不止一次,我不能允许它在delete操作或create操作或诸如此类的操作中触发两次。
因此,除了我需要修复的错误(不确定如何修复)编辑:
我评论了我的规则的最后一个next(),然后触发了一次事件。我还有其他疑问:
提前感谢您的帮助。
发布于 2014-11-04 05:38:37
你承诺要用鼻涕吗?如果不是,使用acl创建访问控制系统有一个简单得多的选项(IMHO)。
访问控制系统基于三个方面:角色;资源和权限。定义某些角色和资源,然后简单地为每个资源设置每个角色的权限。例如,您可以具有角色"admin“,并设置权限”能修改“上的资源”系统配置“。然后,您所需要做的就是根据需要将用户分配给角色。
如果您愿意,我很乐意提供一些示例代码,但您可以查看我在创建nodejs访问控制系统上编写的教程。
https://stackoverflow.com/questions/26717377
复制相似问题