Add "returnOnlyRoleNames" option to Role.getRoles
Currently the return type of Role.getRoles() method is inconsistent: role names are returned for smart roles and role ids are returned for static roles (configured through user-role mapping). This commit adds a new option to Role.getRoles() allowing the caller to request role names to be returned for all types of roles.
This commit is contained in:
parent
3ecb5e1cfe
commit
b0d6c4a7d2
|
@ -368,7 +368,12 @@ module.exports = function(Role) {
|
||||||
* @param {Error} err Error object.
|
* @param {Error} err Error object.
|
||||||
* @param {String[]} roles An array of role IDs
|
* @param {String[]} roles An array of role IDs
|
||||||
*/
|
*/
|
||||||
Role.getRoles = function(context, callback) {
|
Role.getRoles = function(context, options, callback) {
|
||||||
|
if (!callback && typeof options === 'function') {
|
||||||
|
callback = options;
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
|
||||||
if (!(context instanceof AccessContext)) {
|
if (!(context instanceof AccessContext)) {
|
||||||
context = new AccessContext(context);
|
context = new AccessContext(context);
|
||||||
}
|
}
|
||||||
|
@ -418,15 +423,24 @@ module.exports = function(Role) {
|
||||||
if (principalType && principalId) {
|
if (principalType && principalId) {
|
||||||
// Please find() treat undefined matches all values
|
// Please find() treat undefined matches all values
|
||||||
inRoleTasks.push(function(done) {
|
inRoleTasks.push(function(done) {
|
||||||
roleMappingModel.find({where: {principalType: principalType,
|
var filter = {where: {principalType: principalType, principalId: principalId}};
|
||||||
principalId: principalId}}, function(err, mappings) {
|
if (options.returnOnlyRoleNames === true) {
|
||||||
|
filter.include = ['role'];
|
||||||
|
}
|
||||||
|
roleMappingModel.find(filter, function(err, mappings) {
|
||||||
debug('Role mappings found: %s %j', err, mappings);
|
debug('Role mappings found: %s %j', err, mappings);
|
||||||
if (err) {
|
if (err) {
|
||||||
if (done) done(err);
|
if (done) done(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mappings.forEach(function(m) {
|
mappings.forEach(function(m) {
|
||||||
addRole(m.roleId);
|
var role;
|
||||||
|
if (options.returnOnlyRoleNames === true) {
|
||||||
|
role = m.toJSON().role.name;
|
||||||
|
} else {
|
||||||
|
role = m.roleId;
|
||||||
|
}
|
||||||
|
addRole(role);
|
||||||
});
|
});
|
||||||
if (done) done();
|
if (done) done();
|
||||||
});
|
});
|
||||||
|
|
|
@ -252,6 +252,20 @@ describe('role model', function() {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
function(next) {
|
||||||
|
Role.getRoles(
|
||||||
|
{principalType: RoleMapping.USER, principalId: user.id},
|
||||||
|
{returnOnlyRoleNames: true},
|
||||||
|
function(err, roles) {
|
||||||
|
if (err) return next(err);
|
||||||
|
expect(roles).to.eql([
|
||||||
|
Role.AUTHENTICATED,
|
||||||
|
Role.EVERYONE,
|
||||||
|
role.name,
|
||||||
|
]);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
Role.getRoles(
|
Role.getRoles(
|
||||||
{principalType: RoleMapping.APP, principalId: user.id},
|
{principalType: RoleMapping.APP, principalId: user.id},
|
||||||
|
|
Loading…
Reference in New Issue