Create tables
First you need to create your own tables, which will contain associations between roles, permissions and resources:
- Create a role table ('Admin', 'User', 'Guest')
- Create a resource table ("Users", "Projects", "Programs")
- ( "", "", "", "", "" )
, . , "Deny", Read!= True.
, , role_id resource_id , true.
, . , , , :
users.post('/', getAuth, handleUserPost)
, - , , , , :
getAuth = function (req, res, next) {
if(req.user) {
db.getPerms({role_id: req.user.role_id, resource_id: req.resource.id})
.then(function(perms){
var allow = false;
perms.forEach(function(perm){
if (req.method == "POST" && perms.create) allow = true;
else if (req.method == "GET" && perms.read) allow = true;
else if (req.method == "PUT" && perms.write) allow = true;
else if (req.method == "DELETE" && perm.delete) allow = true;
})
if (allow) next();
else res.status(403).send({error: 'access denied'});
})
} else res.status(400).send({error: 'invalid token'})
}
, , .