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

whichUsers function #255

Open
wants to merge 1 commit 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 lib/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Expand Down