OptimalBits / node_acl

Access control lists for node applications
2.62k stars 369 forks source link

User not authenticated error when using Mongo backend #203

Closed cookie-ag closed 7 years ago

cookie-ag commented 8 years ago
var mongoose = require('mongoose');
var acl = require("acl");

mongoose.connect("mongodb://localhost:27017/TempDB");
var node_acl = new acl(new acl.mongodbBackend(mongoose.connection.db, "acl_"));

node_acl.allow([{
    roles: ['Researcher'],
    allows: [
        { resources: ['/Otasks'], permissions: ['get']}
    ]
}])

app.use(function(req, res, next) {
    if (req.session.isManager === true) {
        node_acl.addUserRoles(req.user, 'Researcher');
    }
    next();
})

app.use(node_acl.middleware());

screen shot 2016-09-05 at 1 29 17 am

cookie-ag commented 8 years ago
works using memorybackend()
var acl = require("acl");
var node_acl = new acl(new acl.memoryBackend());

node_acl.allow('role01', '/resource01/', 'get');

app.use(function(req, res, next) {
    if (req.session.isResearcher === true) {
        node_acl.addUserRoles(req.session.userId, 'role01');
    }
    //Listing the roles this user has
    node_acl.userRoles(req.session.userId, function(err, res) {
        console.log(req.session.userId + " has the following roles : " + res);
    })

    //Listing the permissions this user has
    node_acl.allowedPermissions(req.session.userId, '/resource01/', function(err, permissions) {
        console.log(permissions);
    })
    next();
})

app.use(node_acl.middleware());
puhazh commented 7 years ago

This should be working fine. Please you mongodb as your driver and not mongoose. I was using monk as my driver had similar issue and switching to mongodb for acl resolved the issue.

cookie-ag commented 7 years ago

@puhazh Can you give an example?

stephen-last commented 7 years ago

@serganus Try looking at mongoose.connection.db... Does it have a db property..?

Try using mongoose.connection.db.db.