Reputation: 75
I'm trying to check user access level to grant him some actions in the service.
exports.hasAdminAccess = function(req, res, next) {
if (hasAccessLevel(req.user, adminLevel)) {
return next()
}
res.redirect('/accessProblem');
}
function hasAccessLevel(user, level) {
UserRole = models.UserRole;
return UserRole.findOne({
where: {
id: user.userRoleId,
}
}).then(function(role){
if (role.accessLevel <= level) {
return true;
} else {
return false;
}
});
}
but hasAccessLevel()
is constantly returning Promise
object instead true
or false
.
I could write body of hasAccessLevel
in hasAdminAccessLevel
, but I have plenty of other methods for other roles.
Are there any other ways to perform this operation?
Upvotes: 1
Views: 1574
Reputation: 10862
You could resolve the promise in the hasAdminAccess
method like below:
exports.hasAdminAccess = function(req,res,next) {
hasAccessLevel(req.user,adminLevel)
.then((value) => {
if (value) {
return next();
}
})
.catch(() => res.redirect('/accessProblem'));
}
function hasAccessLevel(user, level) {
UserRole = models.UserRole;
return UserRole.findOne({
where: {
id: user.userRoleId,
}
}).then(function(role){
if (role.accessLevel <= level) {
return true;
} else {
return false;
}
});
}
Upvotes: 1