2013-11-10 06:22:16 +00:00
|
|
|
var loopback = require('../loopback');
|
2013-12-12 03:15:19 +00:00
|
|
|
var debug = require('debug')('loopback:security:role');
|
2013-12-11 17:06:21 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var async = require('async');
|
2013-11-04 21:19:02 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var AccessContext = require('./access-context').AccessContext;
|
|
|
|
|
2013-06-26 23:25:51 +00:00
|
|
|
// Role model
|
|
|
|
var RoleSchema = {
|
2013-11-10 06:22:16 +00:00
|
|
|
id: {type: String, id: true}, // Id
|
|
|
|
name: {type: String, required: true}, // The name of a role
|
|
|
|
description: String, // Description
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
// Timestamps
|
|
|
|
created: {type: Date, default: Date},
|
|
|
|
modified: {type: Date, default: Date}
|
2013-11-04 21:19:02 +00:00
|
|
|
};
|
|
|
|
|
2013-11-12 06:16:51 +00:00
|
|
|
/**
|
|
|
|
* Map principals to roles
|
|
|
|
*/
|
|
|
|
var RoleMappingSchema = {
|
|
|
|
id: {type: String, id: true}, // Id
|
|
|
|
roleId: String, // The role id
|
|
|
|
principalType: String, // The principal type, such as user, application, or role
|
|
|
|
principalId: String // The principal id
|
|
|
|
};
|
|
|
|
|
2013-12-20 01:49:47 +00:00
|
|
|
/**
|
|
|
|
* Map Roles to
|
|
|
|
*/
|
|
|
|
|
2013-11-12 06:16:51 +00:00
|
|
|
var RoleMapping = loopback.createModel('RoleMapping', RoleMappingSchema, {
|
|
|
|
relations: {
|
|
|
|
role: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'Role',
|
|
|
|
foreignKey: 'roleId'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
// Principal types
|
|
|
|
RoleMapping.USER = 'USER';
|
|
|
|
RoleMapping.APP = RoleMapping.APPLICATION = 'APP';
|
|
|
|
RoleMapping.ROLE = 'ROLE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the application principal
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Application} application
|
2013-11-12 18:10:32 +00:00
|
|
|
*/
|
|
|
|
RoleMapping.prototype.application = function (callback) {
|
|
|
|
if (this.principalType === RoleMapping.APPLICATION) {
|
2014-01-23 22:26:45 +00:00
|
|
|
var applicationModel = this.constructor.Application
|
|
|
|
|| loopback.getModelByType(loopback.Application);
|
2014-01-16 16:50:50 +00:00
|
|
|
applicationModel.findById(this.principalId, callback);
|
2013-11-12 06:16:51 +00:00
|
|
|
} else {
|
2013-11-12 18:10:32 +00:00
|
|
|
process.nextTick(function () {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(null, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
/**
|
|
|
|
* Get the user principal
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {User} user
|
2013-11-12 18:10:32 +00:00
|
|
|
*/
|
|
|
|
RoleMapping.prototype.user = function (callback) {
|
|
|
|
if (this.principalType === RoleMapping.USER) {
|
2014-01-23 22:26:45 +00:00
|
|
|
var userModel = this.constructor.User
|
|
|
|
|| loopback.getModelByType(loopback.User);
|
|
|
|
userModel.findById(this.principalId, callback);
|
2013-11-12 06:16:51 +00:00
|
|
|
} else {
|
2013-11-12 18:10:32 +00:00
|
|
|
process.nextTick(function () {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(null, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
/**
|
|
|
|
* Get the child role principal
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {User} childUser
|
2013-11-12 18:10:32 +00:00
|
|
|
*/
|
|
|
|
RoleMapping.prototype.childRole = function (callback) {
|
|
|
|
if (this.principalType === RoleMapping.ROLE) {
|
2014-01-23 22:26:45 +00:00
|
|
|
var roleModel = this.constructor.Role || loopback.getModelByType(Role);
|
2014-01-16 16:50:50 +00:00
|
|
|
roleModel.findById(this.principalId, callback);
|
2013-11-12 06:16:51 +00:00
|
|
|
} else {
|
2013-11-12 18:10:32 +00:00
|
|
|
process.nextTick(function () {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(null, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
/**
|
2013-12-20 01:49:47 +00:00
|
|
|
* The Role Model
|
|
|
|
* @class
|
2013-11-12 18:10:32 +00:00
|
|
|
*/
|
2013-11-10 06:22:16 +00:00
|
|
|
var Role = loopback.createModel('Role', RoleSchema, {
|
|
|
|
relations: {
|
2013-11-12 06:16:51 +00:00
|
|
|
principals: {
|
2013-11-10 06:22:16 +00:00
|
|
|
type: 'hasMany',
|
2013-11-12 06:16:51 +00:00
|
|
|
model: 'RoleMapping',
|
|
|
|
foreignKey: 'roleId'
|
2013-11-10 06:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-11-04 21:19:02 +00:00
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
// Set up the connection to users/applications/roles once the model
|
|
|
|
Role.once('dataSourceAttached', function () {
|
2014-01-23 22:26:45 +00:00
|
|
|
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
|
2013-11-12 18:10:32 +00:00
|
|
|
Role.prototype.users = function (callback) {
|
2014-01-16 16:50:50 +00:00
|
|
|
roleMappingModel.find({where: {roleId: this.id,
|
2013-12-11 07:33:57 +00:00
|
|
|
principalType: RoleMapping.USER}}, function (err, mappings) {
|
2013-11-12 18:10:32 +00:00
|
|
|
if (err) {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 18:10:32 +00:00
|
|
|
return mappings.map(function (m) {
|
2013-11-12 06:16:51 +00:00
|
|
|
return m.principalId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
Role.prototype.applications = function (callback) {
|
2014-01-16 16:50:50 +00:00
|
|
|
roleMappingModel.find({where: {roleId: this.id,
|
2013-12-11 07:33:57 +00:00
|
|
|
principalType: RoleMapping.APPLICATION}}, function (err, mappings) {
|
2013-11-12 18:10:32 +00:00
|
|
|
if (err) {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 18:10:32 +00:00
|
|
|
return mappings.map(function (m) {
|
2013-11-12 06:16:51 +00:00
|
|
|
return m.principalId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
Role.prototype.roles = function (callback) {
|
2014-01-16 16:50:50 +00:00
|
|
|
roleMappingModel.find({where: {roleId: this.id,
|
2013-12-11 07:33:57 +00:00
|
|
|
principalType: RoleMapping.ROLE}}, function (err, mappings) {
|
2013-11-12 18:10:32 +00:00
|
|
|
if (err) {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 18:10:32 +00:00
|
|
|
return mappings.map(function (m) {
|
2013-11-12 06:16:51 +00:00
|
|
|
return m.principalId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
// Special roles
|
|
|
|
Role.OWNER = '$owner'; // owner of the object
|
2013-11-04 21:19:02 +00:00
|
|
|
Role.RELATED = "$related"; // any User with a relationship to the object
|
|
|
|
Role.AUTHENTICATED = "$authenticated"; // authenticated user
|
2013-11-20 21:38:14 +00:00
|
|
|
Role.UNAUTHENTICATED = "$unauthenticated"; // authenticated user
|
2013-11-04 21:19:02 +00:00
|
|
|
Role.EVERYONE = "$everyone"; // everyone
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2013-11-13 18:29:33 +00:00
|
|
|
/**
|
|
|
|
* Add custom handler for roles
|
|
|
|
* @param role
|
2013-12-11 07:33:57 +00:00
|
|
|
* @param resolver The resolver function decides if a principal is in the role
|
|
|
|
* dynamically
|
2013-11-13 18:29:33 +00:00
|
|
|
*
|
2013-11-19 19:58:30 +00:00
|
|
|
* function(role, context, callback)
|
2013-11-13 18:29:33 +00:00
|
|
|
*/
|
|
|
|
Role.registerResolver = function(role, resolver) {
|
2013-11-19 19:58:30 +00:00
|
|
|
if(!Role.resolvers) {
|
|
|
|
Role.resolvers = {};
|
|
|
|
}
|
2013-11-13 18:29:33 +00:00
|
|
|
Role.resolvers[role] = resolver;
|
|
|
|
};
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.registerResolver(Role.OWNER, function(role, context, callback) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if(!context || !context.model || !context.modelId) {
|
2013-11-19 19:58:30 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
callback && callback(null, false);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var modelClass = context.model;
|
2013-12-12 00:03:48 +00:00
|
|
|
var modelId = context.modelId;
|
|
|
|
var userId = context.getUserId();
|
|
|
|
Role.isOwner(modelClass, modelId, userId, callback);
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
|
|
|
|
2013-12-11 07:33:57 +00:00
|
|
|
function isUserClass(modelClass) {
|
|
|
|
return modelClass === loopback.User ||
|
|
|
|
modelClass.prototype instanceof loopback.User;
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:09:20 +00:00
|
|
|
/*!
|
|
|
|
* Check if two user ids matches
|
|
|
|
* @param {*} id1
|
|
|
|
* @param {*} id2
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function matches(id1, id2) {
|
|
|
|
if (id1 === undefined || id1 === null || id1 ===''
|
|
|
|
|| id2 === undefined || id2 === null || id2 === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// The id can be a MongoDB ObjectID
|
|
|
|
return id1 === id2 || id1.toString() === id2.toString();
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:06:21 +00:00
|
|
|
/**
|
|
|
|
* Check if a given userId is the owner the model instance
|
|
|
|
* @param {Function} modelClass The model class
|
|
|
|
* @param {*} modelId The model id
|
|
|
|
* @param {*) userId The user id
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
Role.isOwner = function isOwner(modelClass, modelId, userId, callback) {
|
|
|
|
assert(modelClass, 'Model class is required');
|
2014-03-19 22:09:20 +00:00
|
|
|
debug('isOwner(): %s %s userId: %s', modelClass && modelClass.modelName, modelId, userId);
|
2013-12-11 07:33:57 +00:00
|
|
|
// No userId is present
|
2013-12-11 05:49:18 +00:00
|
|
|
if(!userId) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
callback(null, false);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 07:33:57 +00:00
|
|
|
// Is the modelClass User or a subclass of User?
|
|
|
|
if(isUserClass(modelClass)) {
|
2013-12-11 05:49:18 +00:00
|
|
|
process.nextTick(function() {
|
2014-03-19 22:09:20 +00:00
|
|
|
callback(null, matches(modelId, userId));
|
2013-12-11 05:49:18 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:06:21 +00:00
|
|
|
modelClass.findById(modelId, function(err, inst) {
|
2013-12-11 07:33:57 +00:00
|
|
|
if(err || !inst) {
|
2014-03-19 22:09:20 +00:00
|
|
|
debug('Model not found for id %j', modelId);
|
2013-12-11 07:33:57 +00:00
|
|
|
callback && callback(err, false);
|
2013-11-19 19:58:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-11 07:33:57 +00:00
|
|
|
debug('Model found: %j', inst);
|
2014-03-19 22:09:20 +00:00
|
|
|
var ownerId = inst.userId || inst.owner;
|
|
|
|
if(ownerId) {
|
|
|
|
callback && callback(null, matches(ownerId, userId));
|
2013-11-19 19:58:30 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2013-12-11 07:33:57 +00:00
|
|
|
// Try to follow belongsTo
|
2013-11-19 19:58:30 +00:00
|
|
|
for(var r in modelClass.relations) {
|
|
|
|
var rel = modelClass.relations[r];
|
2013-12-11 07:33:57 +00:00
|
|
|
if(rel.type === 'belongsTo' && isUserClass(rel.modelTo)) {
|
|
|
|
debug('Checking relation %s to %s: %j', r, rel.modelTo.modelName, rel);
|
|
|
|
inst[r](function(err, user) {
|
|
|
|
if(!err && user) {
|
|
|
|
debug('User found: %j', user.id);
|
2014-03-19 22:09:20 +00:00
|
|
|
callback && callback(null, matches(user.id, userId));
|
2013-12-11 07:33:57 +00:00
|
|
|
} else {
|
|
|
|
callback && callback(err, false);
|
|
|
|
}
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 22:09:20 +00:00
|
|
|
debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId);
|
2013-11-19 19:58:30 +00:00
|
|
|
callback && callback(null, false);
|
|
|
|
}
|
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
};
|
2013-11-19 19:58:30 +00:00
|
|
|
|
|
|
|
Role.registerResolver(Role.AUTHENTICATED, function(role, context, callback) {
|
|
|
|
if(!context) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
callback && callback(null, false);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-12-11 17:06:21 +00:00
|
|
|
Role.isAuthenticated(context, callback);
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
|
|
|
|
2013-12-11 17:06:21 +00:00
|
|
|
/**
|
|
|
|
* Check if the user id is authenticated
|
|
|
|
* @param {Object} context The security context
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Boolean} isAuthenticated
|
2013-12-11 17:06:21 +00:00
|
|
|
*/
|
|
|
|
Role.isAuthenticated = function isAuthenticated(context, callback) {
|
2013-11-19 19:58:30 +00:00
|
|
|
process.nextTick(function() {
|
2013-12-12 00:03:48 +00:00
|
|
|
callback && callback(null, context.isAuthenticated());
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
};
|
2013-11-19 19:58:30 +00:00
|
|
|
|
2013-11-20 21:38:14 +00:00
|
|
|
Role.registerResolver(Role.UNAUTHENTICATED, function(role, context, callback) {
|
|
|
|
process.nextTick(function() {
|
2013-12-12 00:03:48 +00:00
|
|
|
callback && callback(null, !context || !context.isAuthenticated());
|
2013-11-20 21:38:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.registerResolver(Role.EVERYONE, function (role, context, callback) {
|
|
|
|
process.nextTick(function () {
|
|
|
|
callback && callback(null, true); // Always true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-12 06:16:51 +00:00
|
|
|
/**
|
|
|
|
* Check if a given principal is in the role
|
|
|
|
*
|
2013-11-19 19:58:30 +00:00
|
|
|
* @param {String} role The role name
|
|
|
|
* @param {Object} context The context object
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Boolean} isInRole
|
2013-11-12 06:16:51 +00:00
|
|
|
*/
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.isInRole = function (role, context, callback) {
|
2013-12-11 07:33:57 +00:00
|
|
|
debug('isInRole(): %s %j', role, context);
|
2013-12-12 00:03:48 +00:00
|
|
|
|
|
|
|
if (!(context instanceof AccessContext)) {
|
|
|
|
context = new AccessContext(context);
|
|
|
|
}
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
var resolver = Role.resolvers[role];
|
2013-12-12 00:03:48 +00:00
|
|
|
if (resolver) {
|
2013-12-11 07:33:57 +00:00
|
|
|
debug('Custom resolver found for role %s', role);
|
2013-11-19 19:58:30 +00:00
|
|
|
resolver(role, context, callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
if (context.principals.length === 0) {
|
|
|
|
debug('isInRole() returns: false');
|
|
|
|
process.nextTick(function () {
|
|
|
|
callback && callback(null, false);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-11-19 19:58:30 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var inRole = context.principals.some(function (p) {
|
|
|
|
|
|
|
|
var principalType = p.type || undefined;
|
|
|
|
var principalId = p.id || undefined;
|
|
|
|
|
|
|
|
// Check if it's the same role
|
|
|
|
return principalType === RoleMapping.ROLE && principalId === role;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (inRole) {
|
|
|
|
debug('isInRole() returns: %j', inRole);
|
|
|
|
process.nextTick(function () {
|
2013-11-20 21:31:30 +00:00
|
|
|
callback && callback(null, true);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 23:04:48 +00:00
|
|
|
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
|
2014-01-16 16:50:50 +00:00
|
|
|
this.findOne({where: {name: role}}, function (err, result) {
|
2013-11-12 18:10:32 +00:00
|
|
|
if (err) {
|
2013-11-12 06:16:51 +00:00
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
if (!result) {
|
2013-11-12 18:47:59 +00:00
|
|
|
callback && callback(null, false);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-11 07:33:57 +00:00
|
|
|
debug('Role found: %j', result);
|
2013-12-12 00:03:48 +00:00
|
|
|
|
|
|
|
// Iterate through the list of principals
|
|
|
|
async.some(context.principals, function (p, done) {
|
|
|
|
var principalType = p.type || undefined;
|
|
|
|
var principalId = p.id || undefined;
|
|
|
|
if (principalType && principalId) {
|
2014-01-16 16:50:50 +00:00
|
|
|
roleMappingModel.findOne({where: {roleId: result.id,
|
2013-12-12 00:03:48 +00:00
|
|
|
principalType: principalType, principalId: principalId}},
|
|
|
|
function (err, result) {
|
|
|
|
debug('Role mapping found: %j', result);
|
|
|
|
done(!err && result); // The only arg is the result
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
process.nextTick(function () {
|
|
|
|
done(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, function (inRole) {
|
|
|
|
debug('isInRole() returns: %j', inRole);
|
|
|
|
callback && callback(null, inRole);
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2013-11-12 06:16:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List roles for a given principal
|
2013-12-11 17:06:21 +00:00
|
|
|
* @param {Object} context The security context
|
2013-11-12 18:10:32 +00:00
|
|
|
* @param {Function} callback
|
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
2013-11-12 18:10:32 +00:00
|
|
|
* @param err
|
|
|
|
* @param {String[]} An array of role ids
|
2013-11-12 06:16:51 +00:00
|
|
|
*/
|
2013-12-11 17:06:21 +00:00
|
|
|
Role.getRoles = function (context, callback) {
|
|
|
|
debug('getRoles(): %j', context);
|
2013-12-12 00:03:48 +00:00
|
|
|
|
|
|
|
if(!(context instanceof AccessContext)) {
|
|
|
|
context = new AccessContext(context);
|
|
|
|
}
|
2013-12-11 17:06:21 +00:00
|
|
|
var roles = [];
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var addRole = function (role) {
|
|
|
|
if (role && roles.indexOf(role) === -1) {
|
|
|
|
roles.push(role);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-16 16:50:50 +00:00
|
|
|
var self = this;
|
2013-12-11 17:06:21 +00:00
|
|
|
// Check against the smart roles
|
|
|
|
var inRoleTasks = [];
|
|
|
|
Object.keys(Role.resolvers).forEach(function (role) {
|
|
|
|
inRoleTasks.push(function (done) {
|
2014-01-16 16:50:50 +00:00
|
|
|
self.isInRole(role, context, function (err, inRole) {
|
2014-03-19 22:09:20 +00:00
|
|
|
if(debug.enabled) {
|
|
|
|
debug('In role %j: %j', role, inRole);
|
|
|
|
}
|
2013-12-11 17:06:21 +00:00
|
|
|
if (!err && inRole) {
|
2013-12-12 00:03:48 +00:00
|
|
|
addRole(role);
|
2013-12-11 17:06:21 +00:00
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done(err, null);
|
|
|
|
}
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
});
|
|
|
|
|
2014-01-23 22:26:45 +00:00
|
|
|
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
|
2013-12-12 00:03:48 +00:00
|
|
|
context.principals.forEach(function (p) {
|
|
|
|
// Check against the role mappings
|
|
|
|
var principalType = p.type || undefined;
|
|
|
|
var principalId = p.id || undefined;
|
2013-12-11 17:06:21 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
// Add the role itself
|
|
|
|
if (principalType === RoleMapping.ROLE && principalId) {
|
|
|
|
addRole(principalId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (principalType && principalId) {
|
|
|
|
// Please find() treat undefined matches all values
|
|
|
|
inRoleTasks.push(function (done) {
|
2014-01-16 16:50:50 +00:00
|
|
|
roleMappingModel.find({where: {principalType: principalType,
|
2013-12-12 00:03:48 +00:00
|
|
|
principalId: principalId}}, function (err, mappings) {
|
|
|
|
debug('Role mappings found: %s %j', err, mappings);
|
|
|
|
if (err) {
|
|
|
|
done && done(err);
|
|
|
|
return;
|
2013-12-11 17:06:21 +00:00
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
mappings.forEach(function (m) {
|
|
|
|
addRole(m.roleId);
|
|
|
|
});
|
|
|
|
done && done();
|
2013-12-11 17:06:21 +00:00
|
|
|
});
|
|
|
|
});
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
|
|
|
|
async.parallel(inRoleTasks, function (err, results) {
|
2013-12-12 00:03:48 +00:00
|
|
|
debug('getRoles() returns: %j %j', err, roles);
|
2013-12-11 17:06:21 +00:00
|
|
|
callback && callback(err, roles);
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Role: Role,
|
|
|
|
RoleMapping: RoleMapping
|
|
|
|
};
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
|
|
|
|
|