2014-11-03 5 views
1

시스템에서 역할 권한을 구현하기 위해 규칙 엔진을 사용해야한다는 요구 사항이 있습니다 (과부하가 될 수 있습니까?) 그러나 권한은 복잡하고 복잡합니다. 규칙 엔진을 사용하여 액세스 권한을 부여하거나 사용하지 않는 방법에 혼란스러워합니다.룰 엔진과 node.js가 권한을 처리하는 디자인 패턴

확장 성과 유지 보수가 가능한 방식으로 구현해야하는 디자인에 대해서도 의구심이 있습니다. 따라서 설계에 도움이되거나 규칙 엔진을 사용하는 방법을 설명해주는 것이 좋습니다.

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(); 
}); 

지금까지 내가이 구현 몇 가지 문제가있다 .. 이벤트가 두 번 이상 발사 할 수 있고 나는 그것을 삭제 작업에 두 번 발사 할 수 없습니다 또는 작업 만들기 또는 그런 것들.

내가 내 규칙() 마지막으로 다음의 댓글을 달았습니다, 그리고 이벤트가 한 번 발사됩니다 후 :

그래서 수정해야하는 오류 외에 편집 (어떻게하지 않도록 할).

  1. 좋은 관행이 깨진 적이 또는 안티 패턴 : 내가 다른 의심이?
  2. 확장 성과 유지 관리가 용이합니까?
  3. 규칙 엔진을 사용하는 일반적인 방법은 무엇입니까?
  4. 이 구현의 장단점?
  5. 더 좋은 방법이 있습니까?

미리 도움을 청하십시오.

답변

1

노울 사용에 전념하십니까? 그렇지 않은 경우 node_acl을 사용하여 액세스 제어 시스템을 만드는 데 훨씬 간단한 (IMHO) 옵션이 있습니다.

액세스 제어 시스템은 세 가지 역할, 즉 역할에 기반합니다. 리소스 및 사용 권한. 특정 역할 및 자원을 정의한 다음 각 자원에 대한 각 역할의 권한을 설정하기 만하면됩니다. 예를 들어 자원 "시스템 구성"에서 "admin"역할을 수행하고 "수정할 수 있음"권한을 설정할 수 있습니다. 그런 다음 필요에 따라 역할에 사용자를 할당하면됩니다.

원하는 경우 샘플 코드를 제공해 드리고 싶지만 creating an access control system for nodejs에 작성한 자습서를 확인하십시오.

+0

권한에 nools를 사용하지는 않았지만 시스템의 다른 요구 사항에 대해 규칙 엔진을 사용해야합니다 (규칙 엔진을 권한 - 요구 사항 -에도 사용할 수 있기를 바랍니다) ,하지만 그 라이브러리는 좋은 것처럼 보이고 내 권한 요구 사항에 충분히 좋을 수 있습니다. 그 라이브러리를 살펴 보겠습니다. 하지만 여전히 규칙 엔진을 구현해야하며 올바른지 알고 싶습니다. 도움과 공유 node_acl 주셔서 감사합니다. – rahpuser