Closed puneetjindal closed 4 years ago
@nodece @zhmushan can you answer this question?
You can set the adapter as a global variable. e.g.:
const a = await SequelizeAdapter.newAdapter(...);
function returnEnforcer () {
return casbin.newEnforcer('rbac_model.conf', a);
}
...
You are creating too many enforcer, the best way is inject enforcer
to Casbin
, look like:
class Casbin {
constructor(e) {
this.e = e;
}
async addPolicies (rules) {
// Check the permission.
// Modify the policy.
// await e.addPolicy(...);
// await e.removePolicy(...);
const added = await this.e.addNamedPolicies('p', rules)
return added
// Save the policy back to DB.
// await this.e..savePolicy();
}
async removePolicies (rules) {
const removed = await this.e.removeNamedPolicies('p', rules)
return removed
}
async getPolicy (clientProjectFilter = null) {
const policy = await this.e.getPolicy()
return policy
}
async enforce (request) {
return this.e.enforce(request.sub, request.dom, request.obj, request.act)
}
}
const e = await returnEnforcer()
const casbin = new Casbin(e)
Thanks for really quick response. it works!
Hi, I have just started integrating casbin into my saas multi tenant app for authorization management. I am building REST APIs which can be used by app's frontend roles and permission management module.
For this I have create multiple apis:-
....
a glimpse of how i am using node sequelize adapter as below
**Issue with this is that it creates a new connection each time i call above functions from respective APIs, but somehow i need to understand if i can use my global connection pool already created for other modules as well. Or atleast a pool can be created dedicated for casbin interaction with mysql.
Please let me know if i am thinking on the correct lines and if above is possible then how can i achieve the connection pool so that i can optimize my connections with sql