2014-10-15 14:42:46 +00:00
|
|
|
var loopback = require('../../lib/loopback');
|
|
|
|
|
2014-10-13 09:20:04 +00:00
|
|
|
/**
|
|
|
|
* The `RoleMapping` model extends from the built in `loopback.Model` type.
|
|
|
|
*
|
|
|
|
* @property {String} id Generated ID.
|
|
|
|
* @property {String} name Name of the role.
|
|
|
|
* @property {String} Description Text description.
|
|
|
|
*
|
|
|
|
* @class RoleMapping
|
|
|
|
* @inherits {PersistedModel}
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function(RoleMapping) {
|
2015-03-27 15:59:11 +00:00
|
|
|
// Principal types
|
2014-10-13 09:20:04 +00:00
|
|
|
RoleMapping.USER = 'USER';
|
|
|
|
RoleMapping.APP = RoleMapping.APPLICATION = 'APP';
|
|
|
|
RoleMapping.ROLE = 'ROLE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the application principal
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Application} application
|
|
|
|
*/
|
2014-11-04 12:52:49 +00:00
|
|
|
RoleMapping.prototype.application = function(callback) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var registry = this.constructor.registry;
|
|
|
|
|
2014-10-13 09:20:04 +00:00
|
|
|
if (this.principalType === RoleMapping.APPLICATION) {
|
2014-11-04 12:52:49 +00:00
|
|
|
var applicationModel = this.constructor.Application ||
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.getModelByType('Application');
|
2014-10-13 09:20:04 +00:00
|
|
|
applicationModel.findById(this.principalId, callback);
|
|
|
|
} else {
|
2014-11-04 12:52:49 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
if (callback) callback(null, null);
|
2014-10-13 09:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user principal
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {User} user
|
|
|
|
*/
|
2014-11-04 12:52:49 +00:00
|
|
|
RoleMapping.prototype.user = function(callback) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var RoleMapping = this.constructor;
|
2014-10-13 09:20:04 +00:00
|
|
|
if (this.principalType === RoleMapping.USER) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var userModel = RoleMapping.User ||
|
|
|
|
RoleMapping.registry.getModelByType('User');
|
2014-10-13 09:20:04 +00:00
|
|
|
userModel.findById(this.principalId, callback);
|
|
|
|
} else {
|
2014-11-04 12:52:49 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
if (callback) callback(null, null);
|
2014-10-13 09:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the child role principal
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {User} childUser
|
|
|
|
*/
|
2014-11-04 12:52:49 +00:00
|
|
|
RoleMapping.prototype.childRole = function(callback) {
|
2015-04-01 21:50:36 +00:00
|
|
|
var registry = this.constructor.registry;
|
|
|
|
|
2014-10-13 09:20:04 +00:00
|
|
|
if (this.principalType === RoleMapping.ROLE) {
|
2014-10-15 14:42:46 +00:00
|
|
|
var roleModel = this.constructor.Role ||
|
2015-04-01 21:50:36 +00:00
|
|
|
registry.getModelByType(loopback.Role);
|
2014-10-13 09:20:04 +00:00
|
|
|
roleModel.findById(this.principalId, callback);
|
|
|
|
} else {
|
2014-11-04 12:52:49 +00:00
|
|
|
process.nextTick(function() {
|
|
|
|
if (callback) callback(null, null);
|
2014-10-13 09:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|