Created
March 13, 2014 02:38
-
-
Save tauren/9520991 to your computer and use it in GitHub Desktop.
Sails.js policy that enforces current user as creator of models
This file contains 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 characters
module.exports = function(req, res, next) { | |
// Make sure request is for a single entity, not for a collection of entities | |
if (!req.params.id) { | |
return res.forbidden('error.noPermission'); | |
} | |
return next(); | |
}; |
This file contains 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 characters
module.exports.policies = { | |
'*': true, | |
'user': { | |
// Prevent entire list of users from being loaded | |
'find': 'isNotCollection' | |
}, | |
'project': { | |
// Only authenticated users can create projects and those | |
// projects must be owned by the current user | |
'create': ['isAuthenticated','useCurrentUser'], | |
'destroy': ['isAuthenticated','isOwner'], | |
'update': ['isAuthenticated','isOwner'] | |
} | |
}; |
This file contains 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 characters
module.exports = function(req, res, next) { | |
// Make sure that the user specified is the current user | |
if(req.query) { | |
req.query.user = req.user.id; | |
} | |
return next(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment