Last active
August 29, 2015 14:21
-
-
Save fcingolani/92e0502ae77df82d795e to your computer and use it in GitHub Desktop.
Revisions
-
fcingolani revised this gist
May 21, 2015 . 1 changed file with 0 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,9 +18,6 @@ app.use(function (req, res, next){ function restrict (roles){ return function (req, res, next){ if(roles.indexOf(req.role) === -1 ){ res.sendStatus(403); }else{ -
fcingolani created this gist
May 21, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ var express = require('express'); var app = express(); // middleware que obtiene los roles del usuario actual app.use(function (req, res, next){ if(req.query.admin){ req.role = "admin"; }else{ req.role = "anon"; }; next(); }); function restrict (roles){ return function (req, res, next){ console.log(req.role); console.log(roles); if(roles.indexOf(req.role) === -1 ){ res.sendStatus(403); }else{ next(); } }; }; app.get('/', restrict(['anon', 'admin']), function (req, res) { res.send('Hola'); }); app.get('/secreto', restrict(['admin']), function (req, res) { res.send('Mundo'); }); app.listen(3000);