简体   繁体   中英

Integrating node_acl module with SailsJs

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM