“访问资源的权限不足”:使用Mongoose + Express的ACL中间件

时间:2017-06-28 22:27:22

标签: node.js express acl

我正在尝试为基本路由身份验证设置ACL middleware。在这个例子中,我只是想确保用户(在这种情况下我自己)拥有'admin'权限。

我的路线如下:

// define the home page route
router.get('/', acl.middleware(1, '594a984a9815beb8219dca22', 'admin'), function (req, res) {
    acl.userRoles('594a984a9815beb8219dca22', function(err, roles){
      res.send(roles)
    })
})

使用中间件,我收到消息:

HttpError: Insufficient permissions to access resource

如果我在没有中间件的情况下运行此路由,则usersRole()函数确实会返回

['admin']

作为回应。我无法弄清楚ACL还有什么需要将我识别为管理员。我显然有“管理员”角色。

更新

这是连接代码:

var dbConn = mongoose.connect(configDB.url); // connect to our database
acl = new acl(new acl.mongodbBackend(mongoose.connection.db))

我的config.url正在:

module.exports = {
  'url' : 'mongodb://127.0.0.1:27017/NodeMongo'
}

1 个答案:

答案 0 :(得分:1)

问题与连接到mongoose的异步性质有关。在路由中,已经建立了mongoose / acl连接,但在此之前没有(例如,中间件函数调用)。

由于this example

Alex Mueller,我最终找到了正确的方法

基本上,您需要在回调函数中初始化acl对象:

mongoose.connection.on('connected', function(test) {
  require('./authorization').init();
});

然后在路径文件中,访问完全初始化的acl对象。请参考示例。这很容易理解。