From e76a428a3696ef0d5a5e6ba0fa39fe83b2612120 Mon Sep 17 00:00:00 2001 From: roshan-cinq Date: Fri, 20 Oct 2017 12:59:46 +0530 Subject: [PATCH] whichUsers function to check which users have permission over given resource --- lib/acl.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/lib/acl.js b/lib/acl.js index 9851717..a6dfee3 100644 --- a/lib/acl.js +++ b/lib/acl.js @@ -607,6 +607,62 @@ Acl.prototype.permittedResources = function(roles, permissions, cb){ }).nodeify(cb); } + +/** + whichUsers(resource, function(err, {resourceName: [permissions]}) + + Returns which users have permission over given resource. + + whichUsers(role, permissions, function(err, resources) ) + + Returns which users have given permission over a resource. + + @param {String} Resource + @param {String} Permissions + @param {Function} Callback called wish the result. +*/ +Acl.prototype.whichUsers = function (resource, permissions, cb) { + contract(arguments) + .params('string') + .params('string', 'string|array') + .params('string', 'function') + .params('string', 'string|array', 'function') + .end(); + + if (_.isFunction(permissions)) { + cb = permissions; + permissions = undefined; + } else if (permissions) { + permissions = makeArray(permissions); + } + return this.permittedUsers(resource, permissions, cb); +}; + +Acl.prototype.permittedUsers = function (resource, permissions, cb) { + var _this = this; + var result = _.isUndefined(permissions) ? {} : []; + + return this.backend.getAsync(this.options.buckets.meta, 'roles').then(function (roles) { + return bluebird.all(roles.map(function (role) { + return _this._resourcePermissions([role], resource).then(function (p) { + if (permissions) { + var commonPermissions = _.intersection(permissions, p); + if (commonPermissions.length > 0) { + result.push(role); + } + } else { + result[role] = p; + } + }); + })).then(function () { + return result; + }); + }).nodeify(cb); + +} + + + /** clean ()