Built motion from commit 88aa8522.|2.5.37
[motion2.git] / server / api / user / user.model.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xf88b=['name','SIP/%s,,tTxX','priority','defaults','findAll','then','internal','catch','exports','models','define','users','utf8','undefined','resolve','UserProfile','find','userProfileId','privacyEnabled','updateMd5Password','updatePassword','changed','concat','userSetting','createUserSetting','error','VoiceExtension','bulkCreate','destroy','encryptPassword','function','Missing\x20Callback','randomBytes','base64','salt','pbkdf2Sync','pbkdf2','sha1','toString','indexOf','provider','Invalid\x20password','md5secret',':asterisk:','bluebird','util','md5','lodash','./user.attributes','google','length','context','from-sip','system','%s%s','prefix','push','merge','Set','format','CDR(type)=%s','chanspy','record','MixMonitor','${UNIQUEID}.%s,a','recordingFormat','password','ChanSpy','SIP/%s,%s','options','join','StopMixMonitor','wav','WAV','gsm','CDR(type)=internal','extensionMonitor','includes','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','Dial'];(function(_0x43d5a1,_0x5a0209){var _0x469ab2=function(_0xf66bbe){while(--_0xf66bbe){_0x43d5a1['push'](_0x43d5a1['shift']());}};_0x469ab2(++_0x5a0209);}(_0xf88b,0xcc));var _0xbf88=function(_0x1747b7,_0x3d47ca){_0x1747b7=_0x1747b7-0x0;var _0xc7851e=_0xf88b[_0x1747b7];return _0xc7851e;};'use strict';var BPromise=require(_0xbf88('0x0'));var crypto=require('crypto');var util=require(_0xbf88('0x1'));var md5=require(_0xbf88('0x2'));var _=require(_0xbf88('0x3'));var attributes=require(_0xbf88('0x4'));var authTypes=[_0xbf88('0x5')];var validatePresenceOf=function(_0x4635f0){return _0x4635f0&&_0x4635f0[_0xbf88('0x6')];};var getChanSpyApplications=function(_0x49393f,_0x14f663,_0x40f50e){var _0x54430c={'context':_0x49393f[_0xbf88('0x7')]||_0xbf88('0x8'),'type':_0xbf88('0x9'),'isApp':![],'UserId':_0x49393f['id']};var _0x271440=[];for(var _0x514103=0x0,_0x5ce2d2=0x1;_0x514103<_0x40f50e[_0xbf88('0x6')];_0x514103++,_0x5ce2d2=0x1){var _0x108597={'exten':util['format'](_0xbf88('0xa'),_0x40f50e[_0x514103][_0xbf88('0xb')],_0x14f663),'ChanSpyId':_0x40f50e[_0x514103]['id']};_0x271440[_0xbf88('0xc')](_[_0xbf88('0xd')]({'app':_0xbf88('0xe'),'appdata':util[_0xbf88('0xf')](_0xbf88('0x10'),_0xbf88('0x11')),'priority':_0x5ce2d2++},_0x54430c,_0x108597));if(_0x40f50e[_0x514103][_0xbf88('0x12')]){_0x271440[_0xbf88('0xc')](_[_0xbf88('0xd')]({'app':_0xbf88('0x13'),'appdata':util[_0xbf88('0xf')](_0xbf88('0x14'),_0x40f50e[_0x514103][_0xbf88('0x15')]),'priority':_0x5ce2d2++},_0x54430c,_0x108597));}if(_0x40f50e[_0x514103]['auth']){_0x271440[_0xbf88('0xc')](_[_0xbf88('0xd')]({'app':'Authenticate','appdata':_0x40f50e[_0x514103][_0xbf88('0x16')],'priority':_0x5ce2d2++},_0x54430c,_0x108597));}_0x271440['push'](_['merge']({'app':_0xbf88('0x17'),'appdata':util['format'](_0xbf88('0x18'),_0x49393f['name'],_0x40f50e[_0x514103][_0xbf88('0x19')]?_0x40f50e[_0x514103]['options'][_0xbf88('0x1a')](''):''),'priority':_0x5ce2d2++},_0x54430c,_0x108597));if(_0x40f50e[_0x514103][_0xbf88('0x12')]){_0x271440[_0xbf88('0xc')](_['merge']({'app':_0xbf88('0x1b'),'priority':_0x5ce2d2++},_0x54430c,_0x108597));}}return _0x271440;};var getUserApplications=function(_0x290612,_0x1af02b){var _0x18c168=[_0xbf88('0x1c'),_0xbf88('0x1d'),_0xbf88('0x1e')];var _0xa2a966={'context':_0x290612[_0xbf88('0x7')]||_0xbf88('0x8'),'exten':_0x1af02b,'type':'system','isApp':![],'UserId':_0x290612['id']};var _0x2ceeaa=[{'app':'Set','appdata':_0xbf88('0x1f')}];if(_0x290612[_0xbf88('0x20')]&&_[_0xbf88('0x21')](_0x18c168,_0x290612[_0xbf88('0x20')])){_0x2ceeaa['push']({'app':'Set','appdata':_0xbf88('0x22')});_0x2ceeaa[_0xbf88('0xc')]({'app':_0xbf88('0xe'),'appdata':util[_0xbf88('0xf')]('_XCALLY_MIXMONITOR_FORMAT=%s',_0x290612['extensionMonitor'])});_0x2ceeaa[_0xbf88('0xc')]({'app':_0xbf88('0x23'),'appdata':util['format']('SIP/%s,,tTxXU(xcally-mixmonitor-context)',_0x290612[_0xbf88('0x24')])});}else{_0x2ceeaa[_0xbf88('0xc')]({'app':_0xbf88('0x23'),'appdata':util[_0xbf88('0xf')](_0xbf88('0x25'),_0x290612['name'])});}_0x2ceeaa[_0xbf88('0xc')]({'app':'Hangup'});for(var _0x3190a3=0x0;_0x3190a3<_0x2ceeaa['length'];_0x3190a3++){_0x2ceeaa[_0x3190a3][_0xbf88('0x26')]=_0x3190a3+0x1;_0x2ceeaa[_0x3190a3]=_[_0xbf88('0x27')](_0x2ceeaa[_0x3190a3],_0xa2a966);}return _0x2ceeaa;};var getChanSpyPromise=function(_0x23a7e3,_0x3fcbe2){return new BPromise(function(_0x2f30bd,_0x49b615){return _0x23a7e3['VoiceChanSpy'][_0xbf88('0x28')]()[_0xbf88('0x29')](function(_0x54878e){if(_0x54878e){var _0x58d296=getChanSpyApplications(_0x3fcbe2,_0x3fcbe2[_0xbf88('0x2a')],_0x54878e);if(_0x3fcbe2[_0xbf88('0x2a')]!=_0x3fcbe2[_0xbf88('0x24')]){_0x58d296=_0x58d296['concat'](getChanSpyApplications(_0x3fcbe2,_0x3fcbe2[_0xbf88('0x24')],_0x54878e));}return _0x2f30bd(_0x58d296);}return _0x2f30bd([]);})[_0xbf88('0x2b')](function(_0x531b65){_0x49b615(_0x531b65);});});};module[_0xbf88('0x2c')]=function(_0x4d2dde){var _0x5efea9=_0x4d2dde[_0xbf88('0x2d')];return _0x4d2dde[_0xbf88('0x2e')]('User',attributes,{'tableName':_0xbf88('0x2f'),'charset':_0xbf88('0x30'),'collate':'utf8_bin','hooks':{'beforeCreate':function(_0x3a118f,_0x318380,_0x2d33b8){var _0x3fc2e1=typeof _0x3a118f['userProfileId']===_0xbf88('0x31')?BPromise[_0xbf88('0x32')](null):_0x5efea9[_0xbf88('0x33')][_0xbf88('0x34')]({'where':{'id':_0x3a118f[_0xbf88('0x35')]}});_0x3fc2e1['then'](function(_0x19539a){if(_0x19539a){_0x3a118f[_0xbf88('0x36')]=_0x19539a[_0xbf88('0x36')];}})['then'](function(){_0x3a118f[_0xbf88('0x37')]();_0x3a118f[_0xbf88('0x38')](_0x2d33b8);})[_0xbf88('0x2b')](function(_0x2e33d0){_0x2d33b8(_0x2e33d0);});},'beforeUpdate':function(_0x1c47b7,_0x3485d8,_0x24d2e0){Promise[_0xbf88('0x32')]()[_0xbf88('0x29')](function(){if(_0x1c47b7[_0xbf88('0x39')](_0xbf88('0x35'))===![])return;return _0x5efea9[_0xbf88('0x33')][_0xbf88('0x34')]({'where':{'id':_0x1c47b7['userProfileId']}})[_0xbf88('0x29')](function(_0x4dffef){if(_0x4dffef)_0x1c47b7[_0xbf88('0x36')]=_0x4dffef[_0xbf88('0x36')];return;});})[_0xbf88('0x29')](function(){if(_0x1c47b7[_0xbf88('0x39')]('password')){_0x1c47b7[_0xbf88('0x37')]();return _0x1c47b7['updatePassword'](_0x24d2e0);}_0x24d2e0();})[_0xbf88('0x2b')](function(_0x42c1aa){_0x24d2e0(_0x42c1aa);});},'afterCreate':function(_0x5d22cf,_0x3747e8,_0x120926){var _0x29257e=getUserApplications(_0x5d22cf,_0x5d22cf[_0xbf88('0x2a')]);if(_0x5d22cf[_0xbf88('0x2a')]!=_0x5d22cf[_0xbf88('0x24')]){_0x29257e=_0x29257e[_0xbf88('0x3a')](getUserApplications(_0x5d22cf,_0x5d22cf[_0xbf88('0x24')]));}return Promise[_0xbf88('0x32')]()[_0xbf88('0x29')](function(){if(!_0x5d22cf[_0xbf88('0x3b')]){return _0x5d22cf[_0xbf88('0x3c')]({'clientSideLoggingLevel':_0xbf88('0x3d'),'clientSideLoggingEnabled':![]});}})[_0xbf88('0x29')](function(){return _0x5efea9[_0xbf88('0x3e')][_0xbf88('0x3f')](_0x29257e);})['then'](function(){if(_0x5d22cf[_0xbf88('0x11')]){return getChanSpyPromise(_0x5efea9,_0x5d22cf);}})['then'](function(_0x4e527a){if(_0x4e527a&&_0x4e527a['length']){return _0x5efea9[_0xbf88('0x3e')][_0xbf88('0x3f')](_0x4e527a);}})[_0xbf88('0x29')](function(){_0x120926();})[_0xbf88('0x2b')](function(_0x351646){_0x120926(_0x351646);});},'afterUpdate':function(_0x1bee45,_0x463e75,_0x26e6b0){return BPromise['resolve']()[_0xbf88('0x29')](function(){if(_0x1bee45[_0xbf88('0x39')](_0xbf88('0x11'))||_0x1bee45['changed'](_0xbf88('0x7'))){return _0x5efea9[_0xbf88('0x3e')][_0xbf88('0x40')]({'where':{'UserId':_0x1bee45['id'],'type':_0xbf88('0x9'),'ChanSpyId':{'$ne':null}}})['then'](function(){if(_0x1bee45[_0xbf88('0x11')]){return getChanSpyPromise(_0x5efea9,_0x1bee45)[_0xbf88('0x29')](function(_0x2f5f23){if(_0x2f5f23&&_0x2f5f23[_0xbf88('0x6')]){return _0x5efea9['VoiceExtension'][_0xbf88('0x3f')](_0x2f5f23);}});}});}})[_0xbf88('0x29')](function(){if(_0x1bee45[_0xbf88('0x39')](_0xbf88('0x20'))||_0x1bee45[_0xbf88('0x39')](_0xbf88('0x7'))){return _0x5efea9[_0xbf88('0x3e')][_0xbf88('0x40')]({'where':{'UserId':_0x1bee45['id'],'type':_0xbf88('0x9'),'ChanSpyId':null}})['then'](function(){var _0x3b0a61=getUserApplications(_0x1bee45,_0x1bee45[_0xbf88('0x2a')]);if(_0x1bee45[_0xbf88('0x2a')]!=_0x1bee45['name']){_0x3b0a61=_0x3b0a61[_0xbf88('0x3a')](getUserApplications(_0x1bee45,_0x1bee45[_0xbf88('0x24')]));}return _0x5efea9[_0xbf88('0x3e')][_0xbf88('0x3f')](_0x3b0a61);});}})[_0xbf88('0x29')](function(){_0x26e6b0();})['catch'](function(_0x4615de){_0x26e6b0(_0x4615de);});},'afterDestroy':function(_0x56e1c3){return _0x5efea9[_0xbf88('0x3e')][_0xbf88('0x40')]({'where':{'UserId':_0x56e1c3['id'],'type':_0xbf88('0x9')}});}},'instanceMethods':{'authenticate':function(_0x2c88ea,_0x2dc885){if(!_0x2dc885){return this[_0xbf88('0x16')]===this[_0xbf88('0x41')](_0x2c88ea);}var _0x1bf849=this;this['encryptPassword'](_0x2c88ea,function(_0x4974a4,_0x15bd2a){if(_0x4974a4){_0x2dc885(_0x4974a4);}if(_0x1bf849[_0xbf88('0x16')]===_0x15bd2a){_0x2dc885(null,!![]);}else{_0x2dc885(null,![]);}});},'makeSalt':function(_0x306f29,_0x3788d2){var _0x3743fd=0x10;if(typeof arguments[0x0]==='function'){_0x3788d2=arguments[0x0];_0x306f29=_0x3743fd;}else if(typeof arguments[0x1]===_0xbf88('0x42')){_0x3788d2=arguments[0x1];}else{throw new Error(_0xbf88('0x43'));}if(!_0x306f29){_0x306f29=_0x3743fd;}return crypto[_0xbf88('0x44')](_0x306f29,function(_0x27e506,_0x3ab73a){if(_0x27e506){_0x3788d2(_0x27e506);}return _0x3788d2(null,_0x3ab73a['toString'](_0xbf88('0x45')));});},'encryptPassword':function(_0xd6b5e3,_0xdd479){if(!_0xd6b5e3||!this[_0xbf88('0x46')]){return _0xdd479?_0xdd479(null):null;}var _0x4b5f30=0x2710;var _0x445627=0x40;var _0x1f3bad=new Buffer(this['salt'],_0xbf88('0x45'));if(!_0xdd479){return crypto[_0xbf88('0x47')](_0xd6b5e3,_0x1f3bad,_0x4b5f30,_0x445627,'sha1')['toString'](_0xbf88('0x45'));}return crypto[_0xbf88('0x48')](_0xd6b5e3,_0x1f3bad,_0x4b5f30,_0x445627,_0xbf88('0x49'),function(_0x3710d2,_0x158563){if(_0x3710d2){_0xdd479(_0x3710d2);}return _0xdd479(null,_0x158563[_0xbf88('0x4a')](_0xbf88('0x45')));});},'updatePassword':function(_0x361c74){if(!this[_0xbf88('0x16')]){return _0x361c74(null);}if(!validatePresenceOf(this[_0xbf88('0x16')])&&authTypes[_0xbf88('0x4b')](this[_0xbf88('0x4c')])===-0x1){_0x361c74(new Error(_0xbf88('0x4d')));}var _0x44a989=this;this['makeSalt'](function(_0x764725,_0x2c7c83){if(_0x764725){return _0x361c74(_0x764725);}_0x44a989['salt']=_0x2c7c83;_0x44a989['encryptPassword'](_0x44a989[_0xbf88('0x16')],function(_0x5adb27,_0x2387cb){if(_0x5adb27){_0x361c74(_0x5adb27);}_0x44a989['password']=_0x2387cb;_0x361c74(null);});});},'updateMd5Password':function(){this[_0xbf88('0x4e')]=md5(this[_0xbf88('0x24')]+_0xbf88('0x4f')+this[_0xbf88('0x16')]);return this[_0xbf88('0x4e')];}}});};