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';
|
|
|
|
|
2015-08-13 15:58:41 +00:00
|
|
|
RoleMapping.resolveRelatedModels = function() {
|
|
|
|
if (!this.userModel) {
|
|
|
|
var reg = this.registry;
|
|
|
|
this.roleModel = reg.getModelByType(loopback.Role);
|
|
|
|
this.userModel = reg.getModelByType(loopback.User);
|
|
|
|
this.applicationModel = reg.getModelByType(loopback.Application);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-13 09:20:04 +00:00
|
|
|
/**
|
|
|
|
* 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-08-13 15:58:41 +00:00
|
|
|
this.constructor.resolveRelatedModels();
|
2015-04-01 21:50:36 +00:00
|
|
|
|
2014-10-13 09:20:04 +00:00
|
|
|
if (this.principalType === RoleMapping.APPLICATION) {
|
2015-08-13 15:58:41 +00:00
|
|
|
var applicationModel = this.constructor.applicationModel;
|
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-08-13 15:58:41 +00:00
|
|
|
this.constructor.resolveRelatedModels();
|
2014-10-13 09:20:04 +00:00
|
|
|
if (this.principalType === RoleMapping.USER) {
|
2015-08-13 15:58:41 +00:00
|
|
|
var userModel = this.constructor.userModel;
|
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-08-13 15:58:41 +00:00
|
|
|
this.constructor.resolveRelatedModels();
|
2015-04-01 21:50:36 +00:00
|
|
|
|
2014-10-13 09:20:04 +00:00
|
|
|
if (this.principalType === RoleMapping.ROLE) {
|
2015-08-13 15:58:41 +00:00
|
|
|
var roleModel = this.constructor.roleModel;
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|