diff --git a/common/models/role.js b/common/models/role.js index 2c005cde..c3b996a1 100644 --- a/common/models/role.js +++ b/common/models/role.js @@ -29,42 +29,55 @@ module.exports = function(Role) { // Set up the connection to users/applications/roles once the model Role.once('dataSourceAttached', function() { var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping); + + /** + * Fetch the ids of all users assigned to this role + * @param {Function} callback + */ Role.prototype.users = function(callback) { - roleMappingModel.find({where: {roleId: this.id, - principalType: RoleMapping.USER}}, function(err, mappings) { + roleMappingModel.find({ + where: {roleId: this.id, principalType: RoleMapping.USER} + }, function(err, mappings) { if (err) { - if (callback) callback(err); - return; + return callback(err); } - return mappings.map(function(m) { + callback(null, mappings.map(function(m) { return m.principalId; - }); + })); }); }; + /** + * Fetch the ids of all applications assigned to this role + * @param {Function} callback + */ Role.prototype.applications = function(callback) { - roleMappingModel.find({where: {roleId: this.id, - principalType: RoleMapping.APPLICATION}}, function(err, mappings) { + roleMappingModel.find({ + where: {roleId: this.id, principalType: RoleMapping.APPLICATION} + }, function(err, mappings) { if (err) { - if (callback) callback(err); - return; + return callback(err); } - return mappings.map(function(m) { + callback(null, mappings.map(function(m) { return m.principalId; - }); + })); }); }; + /** + * Fetch the ids of all roles assigned to this role + * @param {Function} callback + */ Role.prototype.roles = function(callback) { - roleMappingModel.find({where: {roleId: this.id, - principalType: RoleMapping.ROLE}}, function(err, mappings) { + roleMappingModel.find({ + where: {roleId: this.id, principalType: RoleMapping.ROLE} + }, function(err, mappings) { if (err) { - if (callback) callback(err); - return; + return callback(err); } - return mappings.map(function(m) { + callback(null, mappings.map(function(m) { return m.principalId; - }); + })); }); }; diff --git a/test/role.test.js b/test/role.test.js index dfedc6cc..f10a1c33 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -3,6 +3,7 @@ var loopback = require('../index'); var Role = loopback.Role; var RoleMapping = loopback.RoleMapping; var User = loopback.User; +var Application = loopback.Application; var ACL = loopback.ACL; function checkResult(err, result) { @@ -208,4 +209,49 @@ describe('role model', function() { }); + it('should fetch all user ids assigned to the role', function(done) { + User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function(err, user) { + Role.create({name: 'userRole'}, function(err, role) { + role.principals.create({principalType: RoleMapping.USER, principalId: user.id}, function(err, p) { + role.users(function(err, users) { + assert(!err); + assert.equal(users.length, 1); + assert.equal(users[0], user.id); + done(); + }); + }); + }); + }); + }); + + it('should fetch all application ids assigned to the role', function(done) { + Application.create({name: 'New App'}, function(err, application) { + Role.create({name: 'applicationRole'}, function(err, role) { + role.principals.create({principalType: RoleMapping.APPLICATION, principalId: application.id}, function(err, p) { + role.applications(function(err, applications) { + assert(!err); + assert.equal(applications.length, 1); + assert.equal(applications[0], application.id); + done(); + }); + }); + }); + }); + }); + + it('should fetch all role ids assigned to the role', function(done) { + Role.create({name: 'New Role'}, function(err, newRole) { + Role.create({name: 'applicationRole'}, function(err, role) { + role.principals.create({principalType: RoleMapping.ROLE, principalId: newRole.id}, function(err, p) { + role.roles(function(err, roles) { + assert(!err); + assert.equal(roles.length, 1); + assert.equal(roles[0], newRole.id); + done(); + }); + }); + }); + }); + }); + });