Skip to content

Instantly share code, notes, and snippets.

@tauren
Created March 13, 2014 02:38
Show Gist options
  • Save tauren/9520991 to your computer and use it in GitHub Desktop.
Save tauren/9520991 to your computer and use it in GitHub Desktop.
Sails.js policy that enforces current user as creator of models
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();
};
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']
}
};
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