2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2015. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2014-10-15 14:42:46 +00:00
|
|
|
var loopback = require('../../lib/loopback');
|
2017-01-31 22:07:24 +00:00
|
|
|
var utils = require('../../lib/utils');
|
2014-10-15 14:42:46 +00:00
|
|
|
|
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;
|
2017-01-31 22:07:24 +00:00
|
|
|
this.roleModel = reg.getModelByType('Role');
|
|
|
|
this.userModel = reg.getModelByType('User');
|
|
|
|
this.applicationModel = reg.getModelByType('Application');
|
2015-08-13 15:58:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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) {
|
2017-01-31 22:07:24 +00:00
|
|
|
callback = callback || utils.createPromiseCallback();
|
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() {
|
2017-01-31 22:07:24 +00:00
|
|
|
callback(null, null);
|
2014-10-13 09:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
2017-01-31 22:07:24 +00:00
|
|
|
return callback.promise;
|
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) {
|
2017-01-31 22:07:24 +00:00
|
|
|
callback = callback || utils.createPromiseCallback();
|
2015-08-13 15:58:41 +00:00
|
|
|
this.constructor.resolveRelatedModels();
|
2017-01-31 22:07:24 +00:00
|
|
|
|
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() {
|
2017-01-31 22:07:24 +00:00
|
|
|
callback(null, null);
|
2014-10-13 09:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
2017-01-31 22:07:24 +00:00
|
|
|
return callback.promise;
|
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) {
|
2017-01-31 22:07:24 +00:00
|
|
|
callback = callback || utils.createPromiseCallback();
|
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() {
|
2017-01-31 22:07:24 +00:00
|
|
|
callback(null, null);
|
2014-10-13 09:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
2017-01-31 22:07:24 +00:00
|
|
|
return callback.promise;
|
2014-10-13 09:20:04 +00:00
|
|
|
};
|
|
|
|
};
|