Skip to content

Instantly share code, notes, and snippets.

@edy
Forked from tarlepp/allowedProjects.js
Created August 22, 2014 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edy/b1f0724ce7513712771e to your computer and use it in GitHub Desktop.
Save edy/b1f0724ce7513712771e to your computer and use it in GitHub Desktop.
'use strict';
var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');
/**
* todo
*
* @param {Request} request Request object
* @param {Response} response Response object
* @param {Function} next Callback function
*
* @returns {*}
*/
module.exports = function(request, response, next) {
sails.log.verbose(' POLICY - ' + __filename);
// Parse where criteria
var where = actionUtil.parseCriteria(request);
// We have id condition set so we need to check if that / those are allowed
if (where.id) {
console.log('ID found');
where.id = 2;
} else { // Otherwise just add id collection to where query
console.log('ID not found');
where.id = 1;
}
request.query = where;
return next();
};
'use strict';
var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');
var _ = require('lodash');
/**
* todo
*
* @param {Request} request Request object
* @param {Response} response Response object
* @param {Function} next Callback function
*
* @returns {*}
*/
module.exports = function(request, response, next) {
sails.log.verbose(' POLICY - ' + __filename);
// Parse where criteria
var where = actionUtil.parseCriteria(request);
sails.models['projectuser']
.find()
.where({user: request.token})
.populate('project')
.then(
function(projectUsers) {
var validIds = _.map(projectUsers, function(projectUser) {
return parseInt(projectUser.project.id, 10);
});
// We have id condition set so we need to check if that / those are allowed
if (where.id) {
var currentIds = _.map((!_.isArray(where.id)) ? [where.id] : where.id, function(id) {
return parseInt(id, 10);
});
where.id = _.intersection(currentIds, validIds);
} else { // Otherwise just add id collection to where query
where.id = validIds;
}
request.query = where;
return next();
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment