OptimalBits / node_acl

Access control lists for node applications
2.62k stars 369 forks source link

How to access acl in controllers or other custom middleware #163

Closed jbeckton closed 8 years ago

jbeckton commented 8 years ago

I have acl up and running, configured the instance in my app.js file. I have all my auth logic in an auth controller which contains methods called when a route is hit. But I am not clear on how to access the acl instance i configured in the app.js file.

jonmassot commented 8 years ago

what backstore/database are you using with ACL?

jbeckton commented 8 years ago

mongoose

I am new to Node. I am failing to understand some of the mechanics of express but I do know that I do not want to run acl = new acl(new acl.mongodbBackend(dbConnection.db, 'acl_', true)); in every .js file I need to use the acl module.

nikordaris commented 8 years ago

@jbeckton you want to look into module.exports in node. Export your instantiated acl object and require() it in each node js file you need.

jbeckton commented 8 years ago

Ok, I have module.exports = acl in my app.js then var acl = require('acl'); in my auth controller. in debug I can se that acl instance has some properties but when I call acl.userRoles() I get an error saying acl.userRoles is not a function.

jbeckton commented 8 years ago

I finally figured it out, thanks for the assistance!

Markkos89 commented 7 years ago

@jbeckton and how did you did it? :)

vijaykotha commented 6 years ago

Hi I am not able to find a way that acl itself finds the role of the user from response user json. This is my configuration

var acl = require('acl'); acl = new acl(new acl.memoryBackend()); acl.addUserRoles('12345', 'Analyst'); // Right now I am setting the role of the user id 12345 as Analyst explicitly. So I need to implement this in a way to find the user's role dynamically. acl.allow([ { roles:['Analyst', 'Supervisor'], allows:[ {resources:'/reports', permissions:'*'}, ] } ]);

clientRouter.get('/reports', passportutil.isAuthenticated, acl.middleware(), function(req, res) { res.sendFile(path.join(__dirname, 'html/reports.html')); });

Please help me out.