1

Iam trying to implement role based authentication with sails js and use node_acl middleware. Has anybody tried it before? What i see from acl documentation is

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

How do I get the dbInstance object from SailsJs

jsphdnl
  • 415
  • 6
  • 21

1 Answers1

0

I was just trying the same thing (not sure about the merits in that), and I managed to connect using the MongoDB Driver.

You have to

npm install mongodb --save

and then, assuming you have in your config/connections.js the following adapter information:

  MyMongo: {
    adapter: 'sails-mongo',
    host: 'localhost', // defaults to `localhost` if omitted
    port: 27017, // defaults to 27017 if omitted
    user: '', // or omit if not relevant
    password: '', // or omit if not relevant
    database: 'someDB' // or omit if not relevant
  },

You can now do the following:

var node_acl = require('acl');
var MongoClient = require('mongodb').MongoClient;

var dbInstance = "mongodb://"+sails.config.connections.MyMongo.host+":"+
      sails.config.connections.MyMongo.port+"/"+sails.config.connections.MyMongo.database;

MongoClient.connect(dbInstance, function(error, db) {
   //check for errors...

   var mongoBackend = new node_acl.mongodbBackend(db, 'acl_');
   var acl = new node_acl( mongoBackend );

   acl.allow('role', 'model', 'action'); // Now you can do this...
}

I hope this helps. Notice I added the acl_ prefix so all the collections generated by ACL are discernible from other collections used by your models with sails.

Ido
  • 81
  • 6