Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PermissionService.js #205

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
56 changes: 56 additions & 0 deletions api/services/PermissionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,62 @@ module.exports = {
return ok;
},

//Performs checks first so the DB doesn't fill with duplicates
grantRole: function(options) {
var action = options.action;
var model = options.model;
var role = options.role;
var where = undefined;
var blacklist = undefined;
var _this = this;
if (typeof options.criteria !== 'undefined' && options.criteria) {
where = options.critera.where;
blacklist = options.criteria.blacklist;
}

return this.findRolePermission(action, model, role, where, blacklist).then(function (result) {
if (typeof result === 'undefined' || !result) {
var criteria = {};
criteria.where = where;
criteria.blacklist = blacklist;
if ((typeof criteria.blacklist === 'undefined' || !criteria.blacklist) && (typeof criteria.where === 'undefined' || !criteria.where))
criteria = undefined
return _this.grant({action: action, model: model, role: role, criteria : criteria});
}
else
return result;
});
},

findRolePermission: function(action, model, role, where, blacklist) {
var relation = "role";
return Model.findOneByName(model).then(function (model) {
return Role.findOneByName(role).then(function (role) {
if (typeof model === 'undefined' || !model || typeof role === 'undefined' || !role)
return Promise.reject(new Error("Role/Model missing. Model: " + model + "Role: " + role));
else {
var promise = Permission.findOne({action: action, model: model.id, role : role.id, relation : relation});
var criteria = {};
var hasCriteria = false;
if (typeof blacklist !== 'undefined' && blacklist && blacklist.length > 0)
criteria.blacklist = blacklist;
if (typeof where !== 'undefined' && where && where.length > 0)
criteria.where = where;
if ((typeof criteria.blacklist !== 'undefined' && criteria.blacklist) || (typeof criteria.where !== 'undefined' && criteria.where)) {
promise = query.populate('criteria', criteria);
hasCriteria = true;
}
return promise.then(function (result) {
if (hasCriteria && result.criteria.length === 0)
return undefined;
return result;
});
}
});
});
},


/**
* add one or more users to a particular role
* TODO should this work with multiple roles?
Expand Down