UNPKG

enhancer-data-bridge

Version:

A bridge between Enhancer Clould and user business datasource

94 lines (93 loc) 2.84 kB
module.exports = function(id, pwd) { return { _id: id, _password: pwd, _roles: [], _variables: { INPUT_CODE: '', SESSION_CODE: '', USER_ID: id }, setId: function(id) { this._id = id; this._variables.USER_ID = id; }, getId: function() { return this._id; }, setName: function(name) { this._name = name; this._variables.USER_NAME = name; }, getName: function() { return this._name; }, getPassword: function() { return this._password; }, hasRole: function(roleId) { if (!roleId) { return this._roles.length > 0; } return this._roles.index(roleId) !== -1; }, setRoles: function(roles) { if (!roles) { return; } if (typeof roles === 'number') { roles = [roles + '']; } else if (typeof roles === 'string') { roles = roles.replace(/(,|;)$/, '') .split(/,|;/g) .map(function(r) { return r.replace(/\s/g, ''); }); } this._roles = roles; this._variables.ROLES = roles.join(','); this._variables.ROLE_DISPLAY_NAMES = this._variables.ROLES; // bodhi.getRolesDisplayName(roles); }, getRoles: function() { return this._roles; }, getRoleIds: function() { return this._roles; }, // getRolesDisplayName() { // return bodhi.getRolesDisplayName(this._roles); // } setData: function(data) { this.setVariables(data); }, setVariables: function(vars) { if (!vars || typeof vars !== 'object' || vars instanceof Array) { console.warn('Invalid variables setting: ' + JSON.stringify(vars)); return; } for (var key in vars) { this.setVariable(key, vars[key]); } }, getVariables: function() { return this._variables || {}; }, setVariable: function(key, value) { if (!key) return null; key = key.toUpperCase(); // ignore password if (/PWD|PASSWORD/.test(key)) { return; } this._variables[key] = value; }, getVariable: function(key) { if (!key) return null; key = (key + '').toUpperCase(); return this._variables[key]; }, isLoggedIn: function() { return !!this._id; } } };