Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
60ae0d8
use populate function if the actioin is add
kmcgrath Jul 29, 2015
699caa2
also check for remove
kmcgrath Jul 29, 2015
83c6e66
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Jul 29, 2015
38d3886
here until PR 101 is merged
kmcgrath Jul 29, 2015
8236611
also look for populate action
kmcgrath Jul 30, 2015
2f2906c
also need to check for populate action
kmcgrath Jul 30, 2015
7d14a9e
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Jul 30, 2015
ffa49e4
need to return the parent record
kmcgrath Jul 30, 2015
7c7b54e
check that populate works, then load the parent object for the target
kmcgrath Jul 30, 2015
8e4b5ea
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Jul 30, 2015
482dea9
when working with relations, check the primary object and proceed. T…
kmcgrath Jul 30, 2015
3986b8c
typo
kmcgrath Jul 30, 2015
09dd70b
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Jul 30, 2015
3d43279
Merge branch 'feature/permission-without-criteria'
kmcgrath Jul 30, 2015
902d12b
if the action is populate, load the permissions for the relation and …
kmcgrath Jul 31, 2015
452ffb4
use the error message string
kmcgrath Jul 31, 2015
9e57dc0
merge
kmcgrath Jul 31, 2015
5389c15
Merge branch 'feature/permission-without-criteria'
kmcgrath Jul 31, 2015
667a826
fix from merge
kmcgrath Jul 31, 2015
07eee0c
add populate check
kmcgrath Jul 31, 2015
7f9ac86
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Jul 31, 2015
29a3889
Merge branch 'feature/permission-without-criteria'
kmcgrath Aug 1, 2015
fab2d9e
Merge branch 'feature/permission-without-criteria'
kmcgrath Aug 1, 2015
5909a69
Merge branch 'feature/permission-without-criteria'
kmcgrath Aug 3, 2015
f0c2f26
Merge branch 'master' of https://github.com/tjwebb/sails-permissions …
kmcgrath Aug 5, 2015
eb3b32c
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Aug 5, 2015
1f5d9c7
Merge branch 'feature/permission-without-criteria'
kmcgrath Aug 5, 2015
fd08636
fix merge
kmcgrath Aug 5, 2015
04b0fde
Merge branch 'feature/findTargetObjects-populate'
kmcgrath Aug 5, 2015
362d935
merge with upstream master
kmcgrath Aug 31, 2015
d49a889
use updated function from commit 872fb94
kmcgrath Sep 1, 2015
a8f2509
merge with upstream master
kmcgrath Sep 9, 2015
50f3e3f
Merge branch 'master' of https://github.com/tjwebb/sails-permissions …
kmcgrath Sep 10, 2015
bc09712
merge with upstream master
kmcgrath Nov 11, 2015
cb373d2
Merge branch 'master' into feature/findTargetObjects-populate
kmcgrath Oct 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/policies/CriteriaPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ module.exports = function(req, res, next) {
// set up response filters if we are not mutating an existing object
if (!_.contains(['update', 'delete'], action)) {

var checkPermissions = req.options.action === 'populate' ? req.populatePermissions : req.permissions;

// get all of the where clauses and blacklists into one flat array
// if a permission has no criteria then it is always true
var criteria = _.compact(_.flatten(
_.map(
_.pluck(permissions, 'criteria'),
_.pluck(checkPermissions, 'criteria'),
function(c) {
if (c.length == 0) {
return [{where: {}}];
Expand Down
67 changes: 51 additions & 16 deletions api/policies/ModelPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,69 @@ function parseModel (req) {
/**
* Query the Model that is being acted upon, and set it on the req object.
*/

var Promise = require('bluebird');

module.exports = function ModelPolicy (req, res, next) {
var modelCache = sails.hooks.permissions._modelCache;
req.options.modelIdentity = parseModel(req);

if (_.isEmpty(req.options.modelIdentity)) {
return next();
var promiseAry = [];

var modelPromise = genModelPromise(req.options.model)
.then(function(model) {
if (!model) {
req.options.unknownModel = true;
}
req.options.modelDefinition = sails.models[model.identity];
req.model = model;
});
promiseAry.push(modelPromise);

if (req.options.action === 'populate') {
var assoc = _.find(req.options.associations,{alias: req.options.alias})
var modelName = assoc[assoc.type];
var model = sails.models[assoc[assoc.type]];

var populateModelPromise = genModelPromise(modelName)
.then(function(model) {
if (!model) {
req.options.unknownPopulateModel = true;
}
req.options.populateModelDefinition = sails.models[model.identity];
req.populateModel = model;
});
promiseAry.push(populateModelPromise);

}

req.options.modelDefinition = sails.models[req.options.modelIdentity];
req.model = modelCache[req.options.modelIdentity];
Promise.all(promiseAry)
.nodeify(next);

if (_.isObject(req.model) && !_.isNull(req.model.id)) {
return next();
};


var genModelPromise = function(reqModel) {

var modelCache = sails.hooks['sails-permissions']._modelCache;
var model = modelCache[reqModel];

if (_.isObject(model) && !_.isUndefined(model.id)) {
return Promise.resolve(model);
}

sails.log.warn('Model [', req.options.modelIdentity, '] not found in model cache');
sails.log.warn('Model [', model, '] not found in model cache');

// if the model is not found in the cache for some reason, get it from the database
Model.findOne({ identity: req.options.modelIdentity })
return Model.findOne({ identity: reqModel})
.then(function (model) {
if (!_.isObject(model)) {
req.options.unknownModel = true;

model = sails.models[req.options.modelIdentity];
if (!sails.config.permissions.allowUnknownModelDefinition) {
throw new Error('Model definition not found: '+ reqModel);
}
else {
model = sails.models[reqModel];
}
}

req.model = model;
next();
return model;
})
.catch(next);
};
39 changes: 37 additions & 2 deletions api/policies/PermissionPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ module.exports = function (req, res, next) {
return next();
}

PermissionService
var promiseAry = [];

var modelPromise = PermissionService
.findModelPermissions(options)
.then(function (permissions) {
sails.log.silly('PermissionPolicy:', permissions.length, 'permissions grant',
Expand All @@ -41,6 +43,39 @@ module.exports = function (req, res, next) {

req.permissions = permissions;

next();
});

promiseAry.push(modelPromise);

// If the request action is populate also grab permissions for the model to be populated
if (req.options.action === 'populate') {

var populatePromise = PermissionService
.findModelPermissions(_.merge(req.options,{
model: req.populateModel,
user: options.user
}))
.then(function (permissions) {
sails.log.silly('PermissionPolicy:', permissions.length, 'permissions grant',
PermissionService.getAction(options), 'on', req.populateModel.name, 'for', req.user.username);

if (!permissions || permissions.length === 0) {
throw new Error(PermissionService.getErrorMessage(options));
}

req.populatePermissions = permissions;

});

promiseAry.push(populatePromise);

}

Promise.all(promiseAry)
.then(function() {
next();
})
.catch(function(e) {
return res.badRequest({error: e.message});
});
};
4 changes: 3 additions & 1 deletion api/services/ModelService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module.exports = {
getTargetModelName: function (req) {
// TODO there has to be a more sails-y way to do this without including
// external modules
if (_.isString(req.options.alias)) {
//
// TODO if action is 'add' or 'remove' check to see if the user has read access on the relation
if (_.isString(req.options.alias) && !_.contains(['add','remove','populate'],req.options.action) ) {
sails.log.silly('singularizing', req.options.alias, 'to use as target model');
return pluralize.singular(req.options.alias);
} else if (_.isString(req.options.model)) {
Expand Down