Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Added role based middleware authorisation
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Apr 21, 2014
1 parent edd549d commit 5eed915
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion app/controllers/articles.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ exports.articleByID = function(req, res, next, id) {
*/
exports.hasAuthorization = function(req, res, next) {
if (req.article.user.id !== req.user.id) {
return res.send(403, 'User is not authorized');
return res.send(403, {
message: 'User is not authorized'
});
}
next();
};
24 changes: 17 additions & 7 deletions app/controllers/users.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ exports.userByID = function(req, res, next, id) {
*/
exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.send(401, 'User is not logged in');
return res.send(401, {
message: 'User is not logged in'
});
}

next();
Expand All @@ -242,12 +244,20 @@ exports.requiresLogin = function(req, res, next) {
/**
* User authorizations routing middleware
*/
exports.hasAuthorization = function(req, res, next) {
if (req.profile.id !== req.user.id) {
return res.send(403, 'User is not authorized');
}
exports.hasAuthorization = function(roles) {
var _this = this;

next();
return function(req, res, next) {
_this.requiresLogin(req, res, function() {
if (_.intersection(req.user.roles, roles).length) {
return next();
} else {
return res.send(403, {
message: 'User is not authorized'
});
}
});
};
};

/**
Expand Down Expand Up @@ -339,7 +349,7 @@ exports.removeOAuthProvider = function(req, res, next) {
// Delete the additional provider
if (user.additionalProvidersData[provider]) {
delete user.additionalProvidersData[provider];

// Then tell mongoose that we've updated the additionalProvidersData field
user.markModified('additionalProvidersData');
}
Expand Down

0 comments on commit 5eed915

Please sign in to comment.