我正在尝试设置ACL middleware用于基本路由认证。在此示例中,我只是想确保用户(在本例中是我自己)具有“管理员”权限。
我的路线如下:
// 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'
}
最佳答案
问题与连接 Mongoose 的异步性质有关。在路由中,mongoose/acl 连接已经建立,但在此之前还没有建立(例如,中间件函数调用)。
感谢this example,我最终找到了正确的方法。通过 Alex Mueller
本质上,您需要在以下回调中初始化 acl 对象:
mongoose.connection.on('connected', function(test) {
require('./authorization').init();
});
然后在路由文件中访问完全初始化的 acl 对象。请引用示例。很容易遵循。
关于node.js - “没有足够的权限访问资源”: ACL Middleware with Mongoose + Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44813506/