Skip to content

Instantly share code, notes, and snippets.

@swist
Last active August 29, 2015 14:11
Show Gist options
  • Save swist/56429283fe3eec8a9dd1 to your computer and use it in GitHub Desktop.
Save swist/56429283fe3eec8a9dd1 to your computer and use it in GitHub Desktop.
sails resource policy
/**
* Graph.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
},
user: {
model: 'User',
via: 'graphs',
required: true
}
}
};
/**
* Gets the current user from session, or returns 403
*/
module.exports = function(req, res, next) {
// User is allowed, proceed to controller
if (req.session.user) {
if (!req.query) {
req.query = {};
}
sails.log.debug('setting req query');
req.query.user = req.session.user.id;
return next();
}
// User is not allowed
else {
return res.send('You are not permitted to perform this action.', 403);
}
};
/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
var User = {
// Enforce model schema in the case of schemaless databases
schema: true,
attributes: {
username : { type: 'string', unique: true },
email : { type: 'email', unique: true },
passports : { collection: 'Passport', via: 'user' },
graphs : { collection: 'Graph', via: 'user'}
}
};
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment