Domanda

I am building an application on ExpressJS (similar to a blog). I am using mongoose for working with MongoDB.

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

According to the docs on the ACL GitHub it has to be used this way:

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

What would be an instance of db if I'm using mongoose?

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

È stato utile?

Soluzione

Off the top of my head, I think you are looking for this:

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

Example (not tested):

var mongoose = require('mongoose'),
    acl = require('acl');

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

(This is of course assuming you have initialized Mongoose elsewhere with mongoose.connect().)

Altri suggerimenti

I happened to encounter this issues recently as well. And I tried many many solutions on stackoverflow but in vain. Finally I found the cause of the problem. Just want to share my experience upon resolving this issue. Typically people separate db config and acl config, thus cause this problem.

The source of the problem is the native feature of node.js--async. If you tried to log the connection status using:

console.log(mongoose.connection.readyState);

You will find in your db.js, it is 1(connected); while in your acl.js, it would be 2(connecting) if you do not make acl in the proper block that ensures mongodb is already connected.

If you follow the most voted and latest answer, your code may 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 may want to set your permissions and roles. But remember to make these in the block where the connection to mongodb is already built. So finally 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();

});

OK, so answering bncc's comment about not working.

In my setup script to create the initial acl database I had to ensure that mongoose had opened the connection before I tried to write to it. Normally this isn't and issue for the app, but just in this case, wrap all acl commands in db.connection.success

// 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);
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top