Running node_acl using mongoose on ExpressJS

I am building an application on ExpressJS (similar to a blog). I use mongoose to work with MongoDB.

I came to the point where I had to choose between different ACL modules, and decided to go with node_acl . What confuses me is that it uses mongodb modules instead of mongoose.

According to the docs on the GitHub ACL, you must use it as follows:

// Or Using the mongodb backend acl = new acl(new acl.mongodbBackend(dbInstance, prefix)); 

What will be the db instance if I use mongoose?

I use something like: Account = mongoose.model ("Account", new scheme ({...}));

+6
source share
3 answers

Above my head, I think you are looking for this:

http://mongoosejs.com/docs/api.html#connection_Connection-db

Example (not verified):

 var mongoose = require('mongoose'), acl = require('acl'); acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_')); 

(This, of course, assumes that you initialized Mongoose elsewhere with mongoose.connect ().)

+11
source

I also had to face these problems recently. And I tried many solutions in stackoverflow, but in vain. Finally, I found the cause of the problem. I just want to share my experience with solving this problem. Usually people share the db configuration and the acl config, so they cause this problem.

The source of the problem is the built-in function node.js - async. If you tried to register the connection status using:

 console.log(mongoose.connection.readyState); 

In your db.js you will find 1 (connected); while in your acl.js it will be 2 (connection) unless you make acl in the appropriate block, which ensures that mongodb is already connected.

If you follow the most voted and most recent answer, your code might look like this:

 var acl = require('acl'); var mongoose = require('../model/db'); mongoose.connection.on('connected', function(error){ if (error) throw error; //you must set up the db when mongoose is connected or your will not be able to write any document into it acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_')); }); 

And then you can set your permissions and roles. But do not forget to make them in the block where the connection with the Mongod is already established. So your code should look like this:

 var acl = require('acl'); var mongoose = require('../model/db'); mongoose.connection.on('connected', function(error){ if (error) throw error; //you must set up the db when mongoose is connected or your will not be able to write any document into it acl = new acl(new acl.mongodbBackend(mongoose.connection.db, 'acl_')); //Do acl.allow('role', ['resources'], ['actions'] here initACLPermissions(); //Do acl.addUserRolss('id', 'role') here initACLRoles(); }); 
+5
source

OK, so the answer is to bncc's comment that it does not work.

In my setup script to create the original acl database, I had to make sure mongoose opened the connection before I tried to write to it. This is usually not a problem for the application, but only in this case wrap all the acl commands in the db.connection.success file

 // Lets open database var cfgDB = require('../config/database') var acl = require('acl') var mongoose = require('mongoose') var dbconnection = mongoose.connect(cfgDB.mongoUrl, function(err) { if(err) console.log('MongoDb: Connection error: ' + err); }) mongoose.connection.on('open', function (ref) { console.log('Connected to mongo server.'); //var dbconnection = mongoose.connect('mongodb://localhost/acl-test', {}); console.log("Lets do this to " + dbconnection.connection.db) acl = new acl(new acl.mongodbBackend(dbconnection.connection.db, "acl_")); // initialize acl system storing data in the redis backend //acl = new acl(new acl.mongodbBackend(dbconnection, "acl_")); /* now assign permissions to roles */ // allow guests to view posts acl.allow("guest", "/index", "view"); // allow registered users to view and create posts //acl.allow("registered users", "post", ["view", "create"]); // allow administrators to perform any action on posts // acl.allow("administrator", "/", "*"); }); mongoose.connection.on('error', function (err) { console.log('Could not connect to mongo server!'); console.log(err); }); 
0
source

All Articles