我需要使用规则引擎来实现系统中的角色权限(这可能是一种过度杀伤吗?)但是权限本身就很复杂和复杂。我对如何授予访问权限或使用规则引擎感到困惑。
我也怀疑我应该使用的设计,以便以可扩展和可维护的方式实现它。所以设计中的任何帮助或向我解释如何使用规则引擎都会很棒。
使用nools,mongoDB,node.js作为后端。
我正在考虑在我的node.js应用程序的引导程序中创建一个封装Nools的规则引擎实例(可能是反模式inner-platform?)并将其作为全局变量。
类似的东西:
'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;
规则engine.flow:
'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规则的flowName)。
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是admin时授予访问权限。例如,我应该以下列方式使用事件吗?
系统中的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();
});
到目前为止,我对此实现存在一些问题。事件可能会多次触发,我不能允许它在删除操作或创建操作或类似事件上触发两次。
除了我需要解决的错误(不确定如何) 修改
我评论了我的规则的最后一个(),然后事件被触发一次。 我还有其他疑问:
提前感谢您的帮助。
答案 0 :(得分:1)
你是否致力于使用nools?如果没有,使用node_acl创建访问控制系统有一个更简单的(IMHO)选项。
门禁系统基于三个方面,即角色;资源和权限。您可以定义某些角色和资源,然后只需为每个资源设置每个角色的权限。例如,您可以拥有角色&#34; admin&#34;并设置权限&#34;可以修改&#34;在资源&#34;系统配置&#34;。然后,您需要做的就是根据需要将用户分配给角色。
如果您愿意,我很乐意提供一些示例代码,但您可以查看我在creating an access control system for nodejs上撰写的教程。