【问题标题】:Integrating node_acl module with SailsJs将 node_acl 模块与 SailsJs 集成
【发布时间】:2016-01-25 10:23:39
【问题描述】:

我正在尝试使用sails js 实现基于角色的身份验证并使用node_acl 中间件。以前有人试过吗?我从 acl 文档中看到的是

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

如何从 SailsJs 中获取 dbInstance 对象

【问题讨论】:

    标签: node.js express sails.js middleware sails-mongo


    【解决方案1】:

    我只是在尝试同样的事情(不确定其中的优点),并且我设法使用 MongoDB 驱动程序进行连接。

    你必须

    npm install mongodb --save
    

    然后,假设您的 config/connections.js 中有以下适配器信息:

      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
      },
    

    您现在可以执行以下操作:

    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...
    }
    

    我希望这会有所帮助。请注意,我添加了 acl_ 前缀,因此 ACL 生成的所有集合都可以与您的带有风帆的模型使用的其他集合区分开来。

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多