2014-10-09 15:32:03 +00:00
|
|
|
var loopback = require('../../lib/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
|
|
|
|
2014-10-09 15:32:03 +00:00
|
|
|
var AccessContext = require('../../lib/access-context').AccessContext;
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
var RoleMapping = loopback.RoleMapping;
|
|
|
|
assert(RoleMapping, 'RoleMapping model must be defined before Role model');
|
2013-11-04 21:19:02 +00:00
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
/**
|
2013-12-20 01:49:47 +00:00
|
|
|
* The Role Model
|
2014-10-13 09:31:27 +00:00
|
|
|
* @class Role
|
2013-11-12 18:10:32 +00:00
|
|
|
*/
|
2014-10-13 09:31:27 +00:00
|
|
|
module.exports = function(Role) {
|
2013-11-04 21:19:02 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
// Workaround for https://github.com/strongloop/loopback/issues/292
|
|
|
|
Role.definition.rawProperties.created.default =
|
|
|
|
Role.definition.properties.created.default = function() {
|
|
|
|
return new Date();
|
|
|
|
};
|
2014-10-13 09:20:04 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
// Workaround for https://github.com/strongloop/loopback/issues/292
|
|
|
|
Role.definition.rawProperties.modified.default =
|
|
|
|
Role.definition.properties.modified.default = function() {
|
|
|
|
return new Date();
|
2013-11-12 06:16:51 +00:00
|
|
|
};
|
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
// Set up the connection to users/applications/roles once the model
|
|
|
|
Role.once('dataSourceAttached', function() {
|
|
|
|
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
|
|
|
|
Role.prototype.users = function(callback) {
|
|
|
|
roleMappingModel.find({where: {roleId: this.id,
|
|
|
|
principalType: RoleMapping.USER}}, function(err, mappings) {
|
|
|
|
if (err) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(err);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return mappings.map(function(m) {
|
|
|
|
return m.principalId;
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2014-10-13 09:31:27 +00:00
|
|
|
};
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
Role.prototype.applications = function(callback) {
|
|
|
|
roleMappingModel.find({where: {roleId: this.id,
|
|
|
|
principalType: RoleMapping.APPLICATION}}, function(err, mappings) {
|
|
|
|
if (err) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(err);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return mappings.map(function(m) {
|
|
|
|
return m.principalId;
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2014-10-13 09:31:27 +00:00
|
|
|
};
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
Role.prototype.roles = function(callback) {
|
|
|
|
roleMappingModel.find({where: {roleId: this.id,
|
|
|
|
principalType: RoleMapping.ROLE}}, function(err, mappings) {
|
|
|
|
if (err) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(err);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return mappings.map(function(m) {
|
|
|
|
return m.principalId;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
// Special roles
|
|
|
|
Role.OWNER = '$owner'; // owner of the object
|
2014-11-04 12:52:49 +00:00
|
|
|
Role.RELATED = '$related'; // any User with a relationship to the object
|
|
|
|
Role.AUTHENTICATED = '$authenticated'; // authenticated user
|
|
|
|
Role.UNAUTHENTICATED = '$unauthenticated'; // authenticated user
|
|
|
|
Role.EVERYONE = '$everyone'; // everyone
|
2014-10-13 09:31:27 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-20 21:45:40 +00:00
|
|
|
* Add custom handler for roles.
|
|
|
|
* @param {String} role Name of role.
|
|
|
|
* @param {Function} resolver Function that determines if a principal is in the specified role.
|
|
|
|
* Signature must be `function(role, context, callback)`
|
2014-10-13 09:31:27 +00:00
|
|
|
*/
|
|
|
|
Role.registerResolver = function(role, resolver) {
|
|
|
|
if (!Role.resolvers) {
|
|
|
|
Role.resolvers = {};
|
|
|
|
}
|
|
|
|
Role.resolvers[role] = resolver;
|
|
|
|
};
|
2013-11-13 18:29:33 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
Role.registerResolver(Role.OWNER, function(role, context, callback) {
|
|
|
|
if (!context || !context.model || !context.modelId) {
|
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, false);
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var modelClass = context.model;
|
|
|
|
var modelId = context.modelId;
|
|
|
|
var userId = context.getUserId();
|
|
|
|
Role.isOwner(modelClass, modelId, userId, callback);
|
|
|
|
});
|
2014-03-19 22:09:20 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
function isUserClass(modelClass) {
|
2015-02-18 15:05:53 +00:00
|
|
|
if (modelClass) {
|
|
|
|
return modelClass === loopback.User ||
|
|
|
|
modelClass.prototype instanceof loopback.User;
|
2015-02-18 23:25:46 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
2015-02-18 15:05:53 +00:00
|
|
|
}
|
2013-12-11 05:49:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 09:31:27 +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 05:49:18 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
/**
|
2014-10-15 07:07:30 +00:00
|
|
|
* Check if a given user ID is the owner the model instance.
|
2014-10-13 09:31:27 +00:00
|
|
|
* @param {Function} modelClass The model class
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {*} modelId The model ID
|
|
|
|
* @param {*} userId The user ID
|
|
|
|
* @param {Function} callback Callback function
|
2014-10-13 09:31:27 +00:00
|
|
|
*/
|
|
|
|
Role.isOwner = function isOwner(modelClass, modelId, userId, callback) {
|
|
|
|
assert(modelClass, 'Model class is required');
|
|
|
|
debug('isOwner(): %s %s userId: %s', modelClass && modelClass.modelName, modelId, userId);
|
|
|
|
// No userId is present
|
|
|
|
if (!userId) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
callback(null, false);
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
|
|
|
|
// Is the modelClass User or a subclass of User?
|
|
|
|
if (isUserClass(modelClass)) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
callback(null, matches(modelId, userId));
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
return;
|
2014-10-13 09:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
modelClass.findById(modelId, function(err, inst) {
|
|
|
|
if (err || !inst) {
|
|
|
|
debug('Model not found for id %j', modelId);
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(err, false);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug('Model found: %j', inst);
|
|
|
|
var ownerId = inst.userId || inst.owner;
|
|
|
|
if (ownerId) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, matches(ownerId, userId));
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Try to follow belongsTo
|
|
|
|
for (var r in modelClass.relations) {
|
|
|
|
var rel = modelClass.relations[r];
|
|
|
|
if (rel.type === 'belongsTo' && isUserClass(rel.modelTo)) {
|
|
|
|
debug('Checking relation %s to %s: %j', r, rel.modelTo.modelName, rel);
|
2014-11-04 12:52:49 +00:00
|
|
|
inst[r](processRelatedUser);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-19 19:58:30 +00:00
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId);
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function processRelatedUser(err, user) {
|
|
|
|
if (!err && user) {
|
|
|
|
debug('User found: %j', user.id);
|
|
|
|
if (callback) callback(null, matches(user.id, userId));
|
|
|
|
} else {
|
|
|
|
if (callback) callback(err, false);
|
|
|
|
}
|
2013-11-19 19:58:30 +00:00
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Role.registerResolver(Role.AUTHENTICATED, function(role, context, callback) {
|
|
|
|
if (!context) {
|
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, false);
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
|
|
|
return;
|
2013-11-19 19:58:30 +00:00
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
Role.isAuthenticated(context, callback);
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
/**
|
|
|
|
* Check if the user id is authenticated
|
|
|
|
* @param {Object} context The security context
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Boolean} isAuthenticated
|
|
|
|
*/
|
|
|
|
Role.isAuthenticated = function isAuthenticated(context, callback) {
|
2013-11-19 19:58:30 +00:00
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, context.isAuthenticated());
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
2014-10-13 09:31:27 +00:00
|
|
|
};
|
2013-11-20 21:38:14 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
Role.registerResolver(Role.UNAUTHENTICATED, function(role, context, callback) {
|
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, !context || !context.isAuthenticated());
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
});
|
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
Role.registerResolver(Role.EVERYONE, function(role, context, callback) {
|
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, true); // Always true
|
2013-12-12 00:03:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
/**
|
|
|
|
* Check if a given principal is in the role
|
|
|
|
*
|
|
|
|
* @param {String} role The role name
|
|
|
|
* @param {Object} context The context object
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Boolean} isInRole
|
|
|
|
*/
|
|
|
|
Role.isInRole = function(role, context, callback) {
|
|
|
|
if (!(context instanceof AccessContext)) {
|
|
|
|
context = new AccessContext(context);
|
|
|
|
}
|
2013-11-20 21:31:30 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
debug('isInRole(): %s', role);
|
|
|
|
context.debug();
|
|
|
|
|
|
|
|
var resolver = Role.resolvers[role];
|
|
|
|
if (resolver) {
|
|
|
|
debug('Custom resolver found for role %s', role);
|
|
|
|
resolver(role, context, callback);
|
2013-11-12 06:16:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
|
|
|
|
if (context.principals.length === 0) {
|
|
|
|
debug('isInRole() returns: false');
|
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, false);
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
2013-11-12 18:47:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
var inRole = context.principals.some(function(p) {
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var principalType = p.type || undefined;
|
|
|
|
var principalId = p.id || undefined;
|
2014-05-03 03:43:03 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
// Check if it's the same role
|
|
|
|
return principalType === RoleMapping.ROLE && principalId === role;
|
2013-12-12 00:03:48 +00:00
|
|
|
});
|
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
if (inRole) {
|
|
|
|
debug('isInRole() returns: %j', inRole);
|
|
|
|
process.nextTick(function() {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, true);
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
|
|
|
|
this.findOne({where: {name: role}}, function(err, result) {
|
|
|
|
if (err) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(err);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!result) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, false);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug('Role found: %j', result);
|
2013-12-11 17:06:21 +00:00
|
|
|
|
2014-10-13 09:31:27 +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;
|
|
|
|
var roleId = result.id.toString();
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
if (principalId !== null && principalId !== undefined && (typeof principalId !== 'string')) {
|
|
|
|
principalId = principalId.toString();
|
2014-03-19 22:09:20 +00:00
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
|
|
|
|
if (principalType && principalId) {
|
|
|
|
roleMappingModel.findOne({where: {roleId: roleId,
|
|
|
|
principalType: principalType, principalId: principalId}},
|
|
|
|
function(err, result) {
|
|
|
|
debug('Role mapping found: %j', result);
|
|
|
|
done(!err && result); // The only arg is the result
|
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
} else {
|
2014-10-13 09:31:27 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
done(false);
|
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
}, function(inRole) {
|
|
|
|
debug('isInRole() returns: %j', inRole);
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(null, inRole);
|
2013-12-11 17:06:21 +00:00
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2013-12-11 17:06:21 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
};
|
2013-12-11 17:06:21 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
/**
|
|
|
|
* List roles for a given principal
|
|
|
|
* @param {Object} context The security context
|
|
|
|
* @param {Function} callback
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
2014-11-04 12:52:49 +00:00
|
|
|
* @param {Error=} err
|
|
|
|
* @param {String[]} roles An array of role ids
|
2014-10-13 09:31:27 +00:00
|
|
|
*/
|
|
|
|
Role.getRoles = function(context, callback) {
|
|
|
|
if (!(context instanceof AccessContext)) {
|
|
|
|
context = new AccessContext(context);
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
2014-10-13 09:31:27 +00:00
|
|
|
var roles = [];
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
var addRole = function(role) {
|
|
|
|
if (role && roles.indexOf(role) === -1) {
|
|
|
|
roles.push(role);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
// Check against the smart roles
|
|
|
|
var inRoleTasks = [];
|
|
|
|
Object.keys(Role.resolvers).forEach(function(role) {
|
|
|
|
inRoleTasks.push(function(done) {
|
|
|
|
self.isInRole(role, context, function(err, inRole) {
|
|
|
|
if (debug.enabled) {
|
|
|
|
debug('In role %j: %j', role, inRole);
|
|
|
|
}
|
|
|
|
if (!err && inRole) {
|
|
|
|
addRole(role);
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
done(err, null);
|
2013-12-11 17:06:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
|
|
|
|
context.principals.forEach(function(p) {
|
|
|
|
// Check against the role mappings
|
|
|
|
var principalType = p.type || undefined;
|
2014-10-22 16:35:25 +00:00
|
|
|
var principalId = p.id == null ? undefined : p.id;
|
2014-11-04 12:52:49 +00:00
|
|
|
|
|
|
|
if (typeof principalId !== 'string' && principalId != null) {
|
2014-10-22 15:05:29 +00:00
|
|
|
principalId = principalId.toString();
|
|
|
|
}
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
// Add the role itself
|
|
|
|
if (principalType === RoleMapping.ROLE && principalId) {
|
|
|
|
addRole(principalId);
|
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
if (principalType && principalId) {
|
|
|
|
// Please find() treat undefined matches all values
|
|
|
|
inRoleTasks.push(function(done) {
|
|
|
|
roleMappingModel.find({where: {principalType: principalType,
|
|
|
|
principalId: principalId}}, function(err, mappings) {
|
|
|
|
debug('Role mappings found: %s %j', err, mappings);
|
|
|
|
if (err) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (done) done(err);
|
2014-10-13 09:31:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mappings.forEach(function(m) {
|
|
|
|
addRole(m.roleId);
|
|
|
|
});
|
2014-11-04 12:52:49 +00:00
|
|
|
if (done) done();
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2014-10-13 09:31:27 +00:00
|
|
|
async.parallel(inRoleTasks, function(err, results) {
|
|
|
|
debug('getRoles() returns: %j %j', err, roles);
|
2014-11-04 12:52:49 +00:00
|
|
|
if (callback) callback(err, roles);
|
2014-10-13 09:31:27 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|