8b17eb84832373ae9e05dcef2663e22e58a4c2f1
[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 _0xb557=['Dial','SIP/%s,,tTxX','priority','defaults','VoiceChanSpy','findAll','then','internal','concat','catch','exports','models','define','User','users','utf8','utf8_bin','userProfileId','undefined','resolve','UserProfile','find','privacyEnabled','updateMd5Password','changed','createUserSetting','error','VoiceExtension','bulkCreate','chanspy','context','destroy','encryptPassword','function','Missing\x20Callback','randomBytes','toString','base64','salt','pbkdf2Sync','sha1','pbkdf2','indexOf','provider','Invalid\x20password','md5secret',':asterisk:','bluebird','crypto','util','md5','lodash','./user.attributes','google','length','from-sip','system','format','%s%s','prefix','merge','Set','CDR(type)=%s','record','push','MixMonitor','${UNIQUEID}.%s,a','auth','Authenticate','password','ChanSpy','options','StopMixMonitor','WAV','gsm','CDR(type)=internal','extensionMonitor','includes','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=%s','SIP/%s,,tTxXU(xcally-mixmonitor-context)','name'];(function(_0x2b1446,_0x4fe1ee){var _0xb87460=function(_0x549cbe){while(--_0x549cbe){_0x2b1446['push'](_0x2b1446['shift']());}};_0xb87460(++_0x4fe1ee);}(_0xb557,0x125));var _0x7b55=function(_0x42dae9,_0x4deeb4){_0x42dae9=_0x42dae9-0x0;var _0x55869a=_0xb557[_0x42dae9];return _0x55869a;};'use strict';var BPromise=require(_0x7b55('0x0'));var crypto=require(_0x7b55('0x1'));var util=require(_0x7b55('0x2'));var md5=require(_0x7b55('0x3'));var _=require(_0x7b55('0x4'));var attributes=require(_0x7b55('0x5'));var authTypes=[_0x7b55('0x6')];var validatePresenceOf=function(_0x9e7146){return _0x9e7146&&_0x9e7146[_0x7b55('0x7')];};var getChanSpyApplications=function(_0x498511,_0x5a8499,_0x51a927){var _0x3516da={'context':_0x498511['context']||_0x7b55('0x8'),'type':_0x7b55('0x9'),'isApp':![],'UserId':_0x498511['id']};var _0x5c6a69=[];for(var _0x5e3133=0x0,_0x3301ce=0x1;_0x5e3133<_0x51a927[_0x7b55('0x7')];_0x5e3133++,_0x3301ce=0x1){var _0x1c1053={'exten':util[_0x7b55('0xa')](_0x7b55('0xb'),_0x51a927[_0x5e3133][_0x7b55('0xc')],_0x5a8499),'ChanSpyId':_0x51a927[_0x5e3133]['id']};_0x5c6a69['push'](_[_0x7b55('0xd')]({'app':_0x7b55('0xe'),'appdata':util['format'](_0x7b55('0xf'),'chanspy'),'priority':_0x3301ce++},_0x3516da,_0x1c1053));if(_0x51a927[_0x5e3133][_0x7b55('0x10')]){_0x5c6a69[_0x7b55('0x11')](_[_0x7b55('0xd')]({'app':_0x7b55('0x12'),'appdata':util[_0x7b55('0xa')](_0x7b55('0x13'),_0x51a927[_0x5e3133]['recordingFormat']),'priority':_0x3301ce++},_0x3516da,_0x1c1053));}if(_0x51a927[_0x5e3133][_0x7b55('0x14')]){_0x5c6a69[_0x7b55('0x11')](_[_0x7b55('0xd')]({'app':_0x7b55('0x15'),'appdata':_0x51a927[_0x5e3133][_0x7b55('0x16')],'priority':_0x3301ce++},_0x3516da,_0x1c1053));}_0x5c6a69[_0x7b55('0x11')](_[_0x7b55('0xd')]({'app':_0x7b55('0x17'),'appdata':util[_0x7b55('0xa')]('SIP/%s,%s',_0x498511['name'],_0x51a927[_0x5e3133][_0x7b55('0x18')]?_0x51a927[_0x5e3133]['options']['join'](''):''),'priority':_0x3301ce++},_0x3516da,_0x1c1053));if(_0x51a927[_0x5e3133][_0x7b55('0x10')]){_0x5c6a69[_0x7b55('0x11')](_['merge']({'app':_0x7b55('0x19'),'priority':_0x3301ce++},_0x3516da,_0x1c1053));}}return _0x5c6a69;};var getUserApplications=function(_0x4807e9,_0x212a8a){var _0x516aac=['wav',_0x7b55('0x1a'),_0x7b55('0x1b')];var _0x28247b={'context':_0x4807e9['context']||_0x7b55('0x8'),'exten':_0x212a8a,'type':_0x7b55('0x9'),'isApp':![],'UserId':_0x4807e9['id']};var _0x49fc45=[{'app':_0x7b55('0xe'),'appdata':_0x7b55('0x1c')}];if(_0x4807e9[_0x7b55('0x1d')]&&_[_0x7b55('0x1e')](_0x516aac,_0x4807e9['extensionMonitor'])){_0x49fc45[_0x7b55('0x11')]({'app':_0x7b55('0xe'),'appdata':_0x7b55('0x1f')});_0x49fc45['push']({'app':'Set','appdata':util['format'](_0x7b55('0x20'),_0x4807e9['extensionMonitor'])});_0x49fc45[_0x7b55('0x11')]({'app':'Dial','appdata':util[_0x7b55('0xa')](_0x7b55('0x21'),_0x4807e9[_0x7b55('0x22')])});}else{_0x49fc45[_0x7b55('0x11')]({'app':_0x7b55('0x23'),'appdata':util[_0x7b55('0xa')](_0x7b55('0x24'),_0x4807e9['name'])});}_0x49fc45[_0x7b55('0x11')]({'app':'Hangup'});for(var _0x56b910=0x0;_0x56b910<_0x49fc45[_0x7b55('0x7')];_0x56b910++){_0x49fc45[_0x56b910][_0x7b55('0x25')]=_0x56b910+0x1;_0x49fc45[_0x56b910]=_[_0x7b55('0x26')](_0x49fc45[_0x56b910],_0x28247b);}return _0x49fc45;};var getChanSpyPromise=function(_0x213a66,_0x5c916a){return new BPromise(function(_0x8cba46,_0x2b3572){return _0x213a66[_0x7b55('0x27')][_0x7b55('0x28')]()[_0x7b55('0x29')](function(_0x4b3603){if(_0x4b3603){var _0x4dda3b=getChanSpyApplications(_0x5c916a,_0x5c916a['internal'],_0x4b3603);if(_0x5c916a[_0x7b55('0x2a')]!=_0x5c916a[_0x7b55('0x22')]){_0x4dda3b=_0x4dda3b[_0x7b55('0x2b')](getChanSpyApplications(_0x5c916a,_0x5c916a[_0x7b55('0x22')],_0x4b3603));}return _0x8cba46(_0x4dda3b);}return _0x8cba46([]);})[_0x7b55('0x2c')](function(_0x57f907){_0x2b3572(_0x57f907);});});};module[_0x7b55('0x2d')]=function(_0x10af08){var _0x17f66e=_0x10af08[_0x7b55('0x2e')];return _0x10af08[_0x7b55('0x2f')](_0x7b55('0x30'),attributes,{'tableName':_0x7b55('0x31'),'charset':_0x7b55('0x32'),'collate':_0x7b55('0x33'),'hooks':{'beforeCreate':function(_0x185991,_0x55f5f0,_0x2a412a){var _0x48ddaa=typeof _0x185991[_0x7b55('0x34')]===_0x7b55('0x35')?BPromise[_0x7b55('0x36')](null):_0x17f66e[_0x7b55('0x37')][_0x7b55('0x38')]({'where':{'id':_0x185991[_0x7b55('0x34')]}});_0x48ddaa[_0x7b55('0x29')](function(_0x398c64){if(_0x398c64){_0x185991[_0x7b55('0x39')]=_0x398c64['privacyEnabled'];}})[_0x7b55('0x29')](function(){_0x185991[_0x7b55('0x3a')]();_0x185991['updatePassword'](_0x2a412a);})['catch'](function(_0x3c93e5){_0x2a412a(_0x3c93e5);});},'beforeUpdate':function(_0x4b9dd9,_0x162a50,_0x2f1ccc){Promise[_0x7b55('0x36')]()[_0x7b55('0x29')](function(){if(_0x4b9dd9[_0x7b55('0x3b')](_0x7b55('0x34'))===![])return;return _0x17f66e[_0x7b55('0x37')][_0x7b55('0x38')]({'where':{'id':_0x4b9dd9[_0x7b55('0x34')]}})[_0x7b55('0x29')](function(_0x1f3a91){if(_0x1f3a91)_0x4b9dd9[_0x7b55('0x39')]=_0x1f3a91[_0x7b55('0x39')];return;});})['then'](function(){if(_0x4b9dd9[_0x7b55('0x3b')](_0x7b55('0x16'))){_0x4b9dd9['updateMd5Password']();return _0x4b9dd9['updatePassword'](_0x2f1ccc);}_0x2f1ccc();})['catch'](function(_0x20c8a5){_0x2f1ccc(_0x20c8a5);});},'afterCreate':function(_0x332e1d,_0x110570,_0xfc7154){var _0x3f269f=getUserApplications(_0x332e1d,_0x332e1d[_0x7b55('0x2a')]);if(_0x332e1d['internal']!=_0x332e1d[_0x7b55('0x22')]){_0x3f269f=_0x3f269f[_0x7b55('0x2b')](getUserApplications(_0x332e1d,_0x332e1d[_0x7b55('0x22')]));}return Promise[_0x7b55('0x36')]()[_0x7b55('0x29')](function(){if(!_0x332e1d['userSetting']){return _0x332e1d[_0x7b55('0x3c')]({'clientSideLoggingLevel':_0x7b55('0x3d'),'clientSideLoggingEnabled':![]});}})[_0x7b55('0x29')](function(){return _0x17f66e[_0x7b55('0x3e')][_0x7b55('0x3f')](_0x3f269f);})['then'](function(){if(_0x332e1d[_0x7b55('0x40')]){return getChanSpyPromise(_0x17f66e,_0x332e1d);}})[_0x7b55('0x29')](function(_0x17bdb3){if(_0x17bdb3&&_0x17bdb3['length']){return _0x17f66e[_0x7b55('0x3e')]['bulkCreate'](_0x17bdb3);}})['then'](function(){_0xfc7154();})[_0x7b55('0x2c')](function(_0x5abec9){_0xfc7154(_0x5abec9);});},'afterUpdate':function(_0x5821c2,_0x592c71,_0x4d0ed1){return BPromise[_0x7b55('0x36')]()['then'](function(){if(_0x5821c2['changed'](_0x7b55('0x40'))||_0x5821c2[_0x7b55('0x3b')](_0x7b55('0x41'))){return _0x17f66e[_0x7b55('0x3e')]['destroy']({'where':{'UserId':_0x5821c2['id'],'type':_0x7b55('0x9'),'ChanSpyId':{'$ne':null}}})['then'](function(){if(_0x5821c2[_0x7b55('0x40')]){return getChanSpyPromise(_0x17f66e,_0x5821c2)[_0x7b55('0x29')](function(_0x48dfb6){if(_0x48dfb6&&_0x48dfb6[_0x7b55('0x7')]){return _0x17f66e['VoiceExtension'][_0x7b55('0x3f')](_0x48dfb6);}});}});}})[_0x7b55('0x29')](function(){if(_0x5821c2[_0x7b55('0x3b')](_0x7b55('0x1d'))||_0x5821c2[_0x7b55('0x3b')](_0x7b55('0x41'))){return _0x17f66e[_0x7b55('0x3e')][_0x7b55('0x42')]({'where':{'UserId':_0x5821c2['id'],'type':_0x7b55('0x9'),'ChanSpyId':null}})[_0x7b55('0x29')](function(){var _0x4384b4=getUserApplications(_0x5821c2,_0x5821c2[_0x7b55('0x2a')]);if(_0x5821c2[_0x7b55('0x2a')]!=_0x5821c2[_0x7b55('0x22')]){_0x4384b4=_0x4384b4[_0x7b55('0x2b')](getUserApplications(_0x5821c2,_0x5821c2[_0x7b55('0x22')]));}return _0x17f66e[_0x7b55('0x3e')][_0x7b55('0x3f')](_0x4384b4);});}})[_0x7b55('0x29')](function(){_0x4d0ed1();})[_0x7b55('0x2c')](function(_0x17aafd){_0x4d0ed1(_0x17aafd);});},'afterDestroy':function(_0x4f339e){return _0x17f66e[_0x7b55('0x3e')][_0x7b55('0x42')]({'where':{'UserId':_0x4f339e['id'],'type':_0x7b55('0x9')}});}},'instanceMethods':{'authenticate':function(_0x4a5c64,_0x2e3346){if(!_0x2e3346){return this[_0x7b55('0x16')]===this[_0x7b55('0x43')](_0x4a5c64);}var _0x310335=this;this[_0x7b55('0x43')](_0x4a5c64,function(_0x505387,_0x55bb57){if(_0x505387){_0x2e3346(_0x505387);}if(_0x310335[_0x7b55('0x16')]===_0x55bb57){_0x2e3346(null,!![]);}else{_0x2e3346(null,![]);}});},'makeSalt':function(_0x587621,_0x244163){var _0x1b27aa=0x10;if(typeof arguments[0x0]===_0x7b55('0x44')){_0x244163=arguments[0x0];_0x587621=_0x1b27aa;}else if(typeof arguments[0x1]===_0x7b55('0x44')){_0x244163=arguments[0x1];}else{throw new Error(_0x7b55('0x45'));}if(!_0x587621){_0x587621=_0x1b27aa;}return crypto[_0x7b55('0x46')](_0x587621,function(_0x539b41,_0x301f12){if(_0x539b41){_0x244163(_0x539b41);}return _0x244163(null,_0x301f12[_0x7b55('0x47')](_0x7b55('0x48')));});},'encryptPassword':function(_0x566ab,_0x519a48){if(!_0x566ab||!this[_0x7b55('0x49')]){return _0x519a48?_0x519a48(null):null;}var _0x142361=0x2710;var _0xad4a37=0x40;var _0x41c51c=new Buffer(this[_0x7b55('0x49')],'base64');if(!_0x519a48){return crypto[_0x7b55('0x4a')](_0x566ab,_0x41c51c,_0x142361,_0xad4a37,_0x7b55('0x4b'))[_0x7b55('0x47')](_0x7b55('0x48'));}return crypto[_0x7b55('0x4c')](_0x566ab,_0x41c51c,_0x142361,_0xad4a37,'sha1',function(_0x37a4d7,_0x2a1f23){if(_0x37a4d7){_0x519a48(_0x37a4d7);}return _0x519a48(null,_0x2a1f23[_0x7b55('0x47')]('base64'));});},'updatePassword':function(_0x3f1759){if(!this[_0x7b55('0x16')]){return _0x3f1759(null);}if(!validatePresenceOf(this[_0x7b55('0x16')])&&authTypes[_0x7b55('0x4d')](this[_0x7b55('0x4e')])===-0x1){_0x3f1759(new Error(_0x7b55('0x4f')));}var _0x430725=this;this['makeSalt'](function(_0x5da90c,_0x4d8e1b){if(_0x5da90c){return _0x3f1759(_0x5da90c);}_0x430725[_0x7b55('0x49')]=_0x4d8e1b;_0x430725[_0x7b55('0x43')](_0x430725[_0x7b55('0x16')],function(_0x518892,_0x4d1b90){if(_0x518892){_0x3f1759(_0x518892);}_0x430725['password']=_0x4d1b90;_0x3f1759(null);});});},'updateMd5Password':function(){this[_0x7b55('0x50')]=md5(this[_0x7b55('0x22')]+_0x7b55('0x51')+this[_0x7b55('0x16')]);return this['md5secret'];}}});};