Merge branch 'esco-master'
# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
This commit is contained in:
commit
553a3b2083
|
@ -6,6 +6,10 @@ var async = require('async');
|
||||||
var AccessContext = require('../../lib/access-context').AccessContext;
|
var AccessContext = require('../../lib/access-context').AccessContext;
|
||||||
|
|
||||||
var RoleMapping = loopback.RoleMapping;
|
var RoleMapping = loopback.RoleMapping;
|
||||||
|
var Role = loopback.Role;
|
||||||
|
var User = loopback.User;
|
||||||
|
var Application = loopback.Application;
|
||||||
|
|
||||||
assert(RoleMapping, 'RoleMapping model must be defined before Role model');
|
assert(RoleMapping, 'RoleMapping model must be defined before Role model');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,44 +35,69 @@ module.exports = function(Role) {
|
||||||
Role.once('dataSourceAttached', function() {
|
Role.once('dataSourceAttached', function() {
|
||||||
var registry = Role.registry;
|
var registry = Role.registry;
|
||||||
var roleMappingModel = this.RoleMapping || registry.getModelByType(RoleMapping);
|
var roleMappingModel = this.RoleMapping || registry.getModelByType(RoleMapping);
|
||||||
Role.prototype.users = function(callback) {
|
var principalTypesToModels = {};
|
||||||
roleMappingModel.find({where: {roleId: this.id,
|
|
||||||
principalType: RoleMapping.USER}}, function(err, mappings) {
|
|
||||||
if (err) {
|
|
||||||
if (callback) callback(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return mappings.map(function(m) {
|
|
||||||
return m.principalId;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Role.prototype.applications = function(callback) {
|
principalTypesToModels[RoleMapping.USER] = User;
|
||||||
roleMappingModel.find({where: {roleId: this.id,
|
principalTypesToModels[RoleMapping.APPLICATION] = Application;
|
||||||
principalType: RoleMapping.APPLICATION}}, function(err, mappings) {
|
principalTypesToModels[RoleMapping.ROLE] = Role;
|
||||||
if (err) {
|
|
||||||
if (callback) callback(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return mappings.map(function(m) {
|
|
||||||
return m.principalId;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Role.prototype.roles = function(callback) {
|
Object.keys(principalTypesToModels).forEach(function(principalType) {
|
||||||
roleMappingModel.find({where: {roleId: this.id,
|
var model = principalTypesToModels[principalType];
|
||||||
principalType: RoleMapping.ROLE}}, function(err, mappings) {
|
var pluralName = model.pluralModelName.toLowerCase();
|
||||||
|
/**
|
||||||
|
* Fetch all users assigned to this role
|
||||||
|
* @function Role.prototype#users
|
||||||
|
* @param {object} [query] query object passed to model find call
|
||||||
|
* @param {Function} [callback]
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Fetch all applications assigned to this role
|
||||||
|
* @function Role.prototype#applications
|
||||||
|
* @param {object} [query] query object passed to model find call
|
||||||
|
* @param {Function} [callback]
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Fetch all roles assigned to this role
|
||||||
|
* @function Role.prototype#roles
|
||||||
|
* @param {object} [query] query object passed to model find call
|
||||||
|
* @param {Function} [callback]
|
||||||
|
*/
|
||||||
|
Role.prototype[pluralName] = function(query, callback) {
|
||||||
|
listByPrincipalType(model, principalType, query, callback);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all models assigned to this role
|
||||||
|
* @private
|
||||||
|
* @param {*} model model type to fetch
|
||||||
|
* @param {String} [principalType] principalType used in the rolemapping for model
|
||||||
|
* @param {object} [query] query object passed to model find call
|
||||||
|
* @param {Function} [callback] callback function called with `(err, models)` arguments.
|
||||||
|
*/
|
||||||
|
function listByPrincipalType(model, principalType, query, callback) {
|
||||||
|
if (callback === undefined) {
|
||||||
|
callback = query;
|
||||||
|
query = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
roleMappingModel.find({
|
||||||
|
where: {roleId: this.id, principalType: principalType}
|
||||||
|
}, function(err, mappings) {
|
||||||
|
var ids;
|
||||||
if (err) {
|
if (err) {
|
||||||
if (callback) callback(err);
|
return callback(err);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
return mappings.map(function(m) {
|
ids = mappings.map(function(m) {
|
||||||
return m.principalId;
|
return m.principalId;
|
||||||
});
|
});
|
||||||
|
query.where = query.where || {};
|
||||||
|
query.where.id = {inq: ids};
|
||||||
|
model.find(query, function(err, models) {
|
||||||
|
callback(err, models);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,10 @@ module.exports = function(registry) {
|
||||||
require('../common/models/access-token.json'),
|
require('../common/models/access-token.json'),
|
||||||
require('../common/models/access-token.js'));
|
require('../common/models/access-token.js'));
|
||||||
|
|
||||||
|
registry.User = createModel(
|
||||||
|
require('../common/models/user.json'),
|
||||||
|
require('../common/models/user.js'));
|
||||||
|
|
||||||
registry.RoleMapping = createModel(
|
registry.RoleMapping = createModel(
|
||||||
require('../common/models/role-mapping.json'),
|
require('../common/models/role-mapping.json'),
|
||||||
require('../common/models/role-mapping.js'));
|
require('../common/models/role-mapping.js'));
|
||||||
|
@ -29,10 +33,6 @@ module.exports = function(registry) {
|
||||||
require('../common/models/scope.json'),
|
require('../common/models/scope.json'),
|
||||||
require('../common/models/scope.js'));
|
require('../common/models/scope.js'));
|
||||||
|
|
||||||
registry.User = createModel(
|
|
||||||
require('../common/models/user.json'),
|
|
||||||
require('../common/models/user.js'));
|
|
||||||
|
|
||||||
registry.Change = createModel(
|
registry.Change = createModel(
|
||||||
require('../common/models/change.json'),
|
require('../common/models/change.json'),
|
||||||
require('../common/models/change.js'));
|
require('../common/models/change.js'));
|
||||||
|
|
|
@ -83,6 +83,7 @@
|
||||||
"loopback-datasource-juggler": "^2.19.1",
|
"loopback-datasource-juggler": "^2.19.1",
|
||||||
"loopback-testing": "^1.1.0",
|
"loopback-testing": "^1.1.0",
|
||||||
"mocha": "^2.1.0",
|
"mocha": "^2.1.0",
|
||||||
|
"sinon": "^1.13.0",
|
||||||
"strong-task-emitter": "^0.0.6",
|
"strong-task-emitter": "^0.0.6",
|
||||||
"supertest": "^0.15.0"
|
"supertest": "^0.15.0"
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
var sinon = require('sinon');
|
||||||
var loopback = require('../index');
|
var loopback = require('../index');
|
||||||
var Role = loopback.Role;
|
var Role = loopback.Role;
|
||||||
var RoleMapping = loopback.RoleMapping;
|
var RoleMapping = loopback.RoleMapping;
|
||||||
var User = loopback.User;
|
var User = loopback.User;
|
||||||
|
var Application = loopback.Application;
|
||||||
var ACL = loopback.ACL;
|
var ACL = loopback.ACL;
|
||||||
|
|
||||||
function checkResult(err, result) {
|
function checkResult(err, result) {
|
||||||
|
@ -67,8 +69,7 @@ describe('role model', function() {
|
||||||
role.users(function(err, users) {
|
role.users(function(err, users) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
assert.equal(users.length, 1);
|
assert.equal(users.length, 1);
|
||||||
assert.equal(users[0].principalType, RoleMapping.USER);
|
assert.equal(users[0].id, user.id);
|
||||||
assert.equal(users[0].principalId, user.id);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -100,8 +101,7 @@ describe('role model', function() {
|
||||||
role.users(function(err, users) {
|
role.users(function(err, users) {
|
||||||
assert(!err);
|
assert(!err);
|
||||||
assert.equal(users.length, 1);
|
assert.equal(users.length, 1);
|
||||||
assert.equal(users[0].principalType, RoleMapping.USER);
|
assert.equal(users[0].id, user.id);
|
||||||
assert.equal(users[0].principalId, user.id);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -208,4 +208,64 @@ describe('role model', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('listByPrincipalType', function() {
|
||||||
|
var sandbox;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
sandbox = sinon.sandbox.create();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
sandbox.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch all models assigned to the role', function(done) {
|
||||||
|
var principalTypesToModels = {};
|
||||||
|
var runs = 0;
|
||||||
|
var mappings;
|
||||||
|
|
||||||
|
principalTypesToModels[RoleMapping.USER] = User;
|
||||||
|
principalTypesToModels[RoleMapping.APPLICATION] = Application;
|
||||||
|
principalTypesToModels[RoleMapping.ROLE] = Role;
|
||||||
|
|
||||||
|
mappings = Object.keys(principalTypesToModels);
|
||||||
|
|
||||||
|
mappings.forEach(function(principalType) {
|
||||||
|
var Model = principalTypesToModels[principalType];
|
||||||
|
Model.create({name:'test', email:'x@y.com', password: 'foobar'}, function(err, model) {
|
||||||
|
Role.create({name:'testRole'}, function(err, role) {
|
||||||
|
role.principals.create({principalType: principalType, principalId: model.id}, function(err, p) {
|
||||||
|
var pluralName = Model.pluralModelName.toLowerCase();
|
||||||
|
role[pluralName](function(err, models) {
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(models.length, 1);
|
||||||
|
if (++runs === mappings.length) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should apply query', 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) {
|
||||||
|
var query = {fields:['id', 'name']};
|
||||||
|
sandbox.spy(User, 'find');
|
||||||
|
role.users(query, function(err, users) {
|
||||||
|
assert(!err);
|
||||||
|
assert.equal(users.length, 1);
|
||||||
|
assert.equal(users[0].id, user.id);
|
||||||
|
assert(User.find.calledWith(query));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue