Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

req.params.id solution, help needed #205

Closed
cookie-ag opened this issue Sep 5, 2016 · 3 comments
Closed

req.params.id solution, help needed #205

cookie-ag opened this issue Sep 5, 2016 · 3 comments

Comments

@cookie-ag
Copy link

var acl = require("acl");
var node_acl = new acl(new acl.memoryBackend());

//01 - Bind roles to resources
node_acl.allow('role01', '/resource01/:param1', 'get');

//02 - Middleware to assign roles to user

- Some code

//03 - Normalizing routes to ignore params

exports.HandlingParamsonAuthorization = function(req, res, next) {

    var parts = req.path.split('/');
    var i = 0;
    var newparts;

    for (i = 0; i < parts.length; i++) {
        if (parts[i] == '57cc79487a6643572fe5813b') {
            parts[i] = ':param' + i;
        }
    }
    newparts = parts.join('/');
    console.log(newparts);

    //Checking if this route has the required permission
    node_acl.isAllowed(req.user, newparts, req.method, function(err, res) {
        if (res) {
            console.log(res);
            next();
        }
    })

    next();
}

//04 - Enforce this to all routes
app.use(node_acl.middleware());

It fails to work when i do GET /resource01/57cc79487a6643572fe5813b it shows /resource01/:param1 but shows the error "insufficient permissions". Please help.

@akaustel
Copy link

akaustel commented Sep 7, 2016

A proposed solution:

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

and also set 1 optional parameter (to check for permission for resource /resource01 and not /resource01/57cc79487a6643572fe5813b). Obviously you should register the middleware according to paths and number of parameters.

app.use(node_acl.middleware(1));

Solves your problem?

@cookie-ag
Copy link
Author

Thanks.

Solved by using custom middleware which uses acl.isallowed(). I used req.route.path instead of req.path to normalize params on urls.

@akaustel
Copy link

akaustel commented Sep 8, 2016

Ok. Good. Is this thread still active then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants