Built motion from commit 932cf0d0.|2.5.42
[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 _0x5632=['base64','pbkdf2Sync','sha1','pbkdf2','indexOf','provider','Invalid\x20password','makeSalt','md5secret',':asterisk:','bluebird','crypto','util','md5','lodash','./user.attributes','google','context','from-sip','format','%s%s','prefix','push','merge','Set','record','MixMonitor','recordingFormat','auth','Authenticate','password','ChanSpy','SIP/%s,%s','name','options','StopMixMonitor','wav','gsm','system','CDR(type)=internal','includes','extensionMonitor','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=%s','Dial','SIP/%s,,tTxXU(xcally-mixmonitor-context)','SIP/%s,,tTxX','Hangup','priority','defaults','internal','concat','catch','models','define','users','utf8','UserProfile','find','then','privacyEnabled','updateMd5Password','updatePassword','resolve','changed','userProfileId','VoiceExtension','bulkCreate','chanspy','length','destroy','function','randomBytes','toString','salt'];(function(_0x43b06b,_0x1929c7){var _0x2c555b=function(_0x30fa8c){while(--_0x30fa8c){_0x43b06b['push'](_0x43b06b['shift']());}};_0x2c555b(++_0x1929c7);}(_0x5632,0xeb));var _0x2563=function(_0x19be1a,_0x3cef8f){_0x19be1a=_0x19be1a-0x0;var _0x3ade6d=_0x5632[_0x19be1a];return _0x3ade6d;};'use strict';var BPromise=require(_0x2563('0x0'));var crypto=require(_0x2563('0x1'));var util=require(_0x2563('0x2'));var md5=require(_0x2563('0x3'));var _=require(_0x2563('0x4'));var attributes=require(_0x2563('0x5'));var authTypes=[_0x2563('0x6')];var validatePresenceOf=function(_0xe771c2){return _0xe771c2&&_0xe771c2['length'];};var getChanSpyApplications=function(_0x5f5c1f,_0xb1d122,_0x13f8fb){var _0x55cc3a={'context':_0x5f5c1f[_0x2563('0x7')]||_0x2563('0x8'),'type':'system','isApp':![],'UserId':_0x5f5c1f['id']};var _0x44c6d6=[];for(var _0x4194e3=0x0,_0x3f7c27=0x1;_0x4194e3<_0x13f8fb['length'];_0x4194e3++,_0x3f7c27=0x1){var _0x4f727a={'exten':util[_0x2563('0x9')](_0x2563('0xa'),_0x13f8fb[_0x4194e3][_0x2563('0xb')],_0xb1d122),'ChanSpyId':_0x13f8fb[_0x4194e3]['id']};_0x44c6d6[_0x2563('0xc')](_[_0x2563('0xd')]({'app':_0x2563('0xe'),'appdata':util[_0x2563('0x9')]('CDR(type)=%s','chanspy'),'priority':_0x3f7c27++},_0x55cc3a,_0x4f727a));if(_0x13f8fb[_0x4194e3][_0x2563('0xf')]){_0x44c6d6[_0x2563('0xc')](_['merge']({'app':_0x2563('0x10'),'appdata':util[_0x2563('0x9')]('${UNIQUEID}.%s,a',_0x13f8fb[_0x4194e3][_0x2563('0x11')]),'priority':_0x3f7c27++},_0x55cc3a,_0x4f727a));}if(_0x13f8fb[_0x4194e3][_0x2563('0x12')]){_0x44c6d6[_0x2563('0xc')](_['merge']({'app':_0x2563('0x13'),'appdata':_0x13f8fb[_0x4194e3][_0x2563('0x14')],'priority':_0x3f7c27++},_0x55cc3a,_0x4f727a));}_0x44c6d6[_0x2563('0xc')](_[_0x2563('0xd')]({'app':_0x2563('0x15'),'appdata':util[_0x2563('0x9')](_0x2563('0x16'),_0x5f5c1f[_0x2563('0x17')],_0x13f8fb[_0x4194e3][_0x2563('0x18')]?_0x13f8fb[_0x4194e3][_0x2563('0x18')]['join'](''):''),'priority':_0x3f7c27++},_0x55cc3a,_0x4f727a));if(_0x13f8fb[_0x4194e3][_0x2563('0xf')]){_0x44c6d6[_0x2563('0xc')](_[_0x2563('0xd')]({'app':_0x2563('0x19'),'priority':_0x3f7c27++},_0x55cc3a,_0x4f727a));}}return _0x44c6d6;};var getUserApplications=function(_0x492ebb,_0x43224f){var _0x38068e=[_0x2563('0x1a'),'WAV',_0x2563('0x1b')];var _0x164a6a={'context':_0x492ebb['context']||_0x2563('0x8'),'exten':_0x43224f,'type':_0x2563('0x1c'),'isApp':![],'UserId':_0x492ebb['id']};var _0x240b2e=[{'app':_0x2563('0xe'),'appdata':_0x2563('0x1d')}];if(_0x492ebb['extensionMonitor']&&_[_0x2563('0x1e')](_0x38068e,_0x492ebb[_0x2563('0x1f')])){_0x240b2e['push']({'app':_0x2563('0xe'),'appdata':_0x2563('0x20')});_0x240b2e[_0x2563('0xc')]({'app':_0x2563('0xe'),'appdata':util['format'](_0x2563('0x21'),_0x492ebb[_0x2563('0x1f')])});_0x240b2e[_0x2563('0xc')]({'app':_0x2563('0x22'),'appdata':util[_0x2563('0x9')](_0x2563('0x23'),_0x492ebb[_0x2563('0x17')])});}else{_0x240b2e[_0x2563('0xc')]({'app':'Dial','appdata':util[_0x2563('0x9')](_0x2563('0x24'),_0x492ebb[_0x2563('0x17')])});}_0x240b2e[_0x2563('0xc')]({'app':_0x2563('0x25')});for(var _0x2850b3=0x0;_0x2850b3<_0x240b2e['length'];_0x2850b3++){_0x240b2e[_0x2850b3][_0x2563('0x26')]=_0x2850b3+0x1;_0x240b2e[_0x2850b3]=_[_0x2563('0x27')](_0x240b2e[_0x2850b3],_0x164a6a);}return _0x240b2e;};var getChanSpyPromise=function(_0x147d9d,_0x535e21){return new BPromise(function(_0x288ad5,_0x2a41ab){return _0x147d9d['VoiceChanSpy']['findAll']()['then'](function(_0x4a28a2){if(_0x4a28a2){var _0x4d522d=getChanSpyApplications(_0x535e21,_0x535e21[_0x2563('0x28')],_0x4a28a2);if(_0x535e21[_0x2563('0x28')]!=_0x535e21[_0x2563('0x17')]){_0x4d522d=_0x4d522d[_0x2563('0x29')](getChanSpyApplications(_0x535e21,_0x535e21[_0x2563('0x17')],_0x4a28a2));}return _0x288ad5(_0x4d522d);}return _0x288ad5([]);})[_0x2563('0x2a')](function(_0x107042){_0x2a41ab(_0x107042);});});};module['exports']=function(_0x2e561e,_0x358fa7){var _0x24db04=_0x2e561e[_0x2563('0x2b')];return _0x2e561e[_0x2563('0x2c')]('User',attributes,{'tableName':_0x2563('0x2d'),'charset':_0x2563('0x2e'),'collate':'utf8_bin','hooks':{'beforeCreate':function(_0x4b99c7,_0x18979d,_0x1880fb){var _0x17863c=typeof _0x4b99c7['userProfileId']==='undefined'?BPromise['resolve'](null):_0x24db04[_0x2563('0x2f')][_0x2563('0x30')]({'where':{'id':_0x4b99c7['userProfileId']}});_0x17863c[_0x2563('0x31')](function(_0x59d199){if(_0x59d199){_0x4b99c7['privacyEnabled']=_0x59d199[_0x2563('0x32')];}})[_0x2563('0x31')](function(){_0x4b99c7[_0x2563('0x33')]();_0x4b99c7[_0x2563('0x34')](_0x1880fb);})['catch'](function(_0x579a80){_0x1880fb(_0x579a80);});},'beforeUpdate':function(_0x140187,_0x36c4ac,_0xfbf4d2){Promise[_0x2563('0x35')]()[_0x2563('0x31')](function(){if(_0x140187[_0x2563('0x36')](_0x2563('0x37'))===![])return;return _0x24db04[_0x2563('0x2f')][_0x2563('0x30')]({'where':{'id':_0x140187['userProfileId']}})[_0x2563('0x31')](function(_0x2d6bce){if(_0x2d6bce)_0x140187[_0x2563('0x32')]=_0x2d6bce[_0x2563('0x32')];return;});})[_0x2563('0x31')](function(){if(_0x140187['changed']('password')){_0x140187[_0x2563('0x33')]();return _0x140187[_0x2563('0x34')](_0xfbf4d2);}_0xfbf4d2();})[_0x2563('0x2a')](function(_0x3c70b7){_0xfbf4d2(_0x3c70b7);});},'afterCreate':function(_0x70bf7b,_0x3a6229,_0x1025da){var _0x4956d0=getUserApplications(_0x70bf7b,_0x70bf7b[_0x2563('0x28')]);if(_0x70bf7b[_0x2563('0x28')]!=_0x70bf7b[_0x2563('0x17')]){_0x4956d0=_0x4956d0[_0x2563('0x29')](getUserApplications(_0x70bf7b,_0x70bf7b['name']));}return _0x24db04[_0x2563('0x38')][_0x2563('0x39')](_0x4956d0)[_0x2563('0x31')](function(){if(_0x70bf7b[_0x2563('0x3a')]){return getChanSpyPromise(_0x24db04,_0x70bf7b);}})[_0x2563('0x31')](function(_0x55f0ba){if(_0x55f0ba&&_0x55f0ba[_0x2563('0x3b')]){return _0x24db04[_0x2563('0x38')][_0x2563('0x39')](_0x55f0ba);}})['then'](function(){_0x1025da();})['catch'](function(_0x38c3c7){_0x1025da(_0x38c3c7);});},'afterUpdate':function(_0x36c8c4,_0x577654,_0x375749){return BPromise[_0x2563('0x35')]()[_0x2563('0x31')](function(){if(_0x36c8c4['changed'](_0x2563('0x3a'))||_0x36c8c4['changed'](_0x2563('0x7'))){return _0x24db04[_0x2563('0x38')][_0x2563('0x3c')]({'where':{'UserId':_0x36c8c4['id'],'type':'system','ChanSpyId':{'$ne':null}}})[_0x2563('0x31')](function(){if(_0x36c8c4[_0x2563('0x3a')]){return getChanSpyPromise(_0x24db04,_0x36c8c4)['then'](function(_0x3fa833){if(_0x3fa833&&_0x3fa833[_0x2563('0x3b')]){return _0x24db04['VoiceExtension']['bulkCreate'](_0x3fa833);}});}});}})['then'](function(){if(_0x36c8c4[_0x2563('0x36')]('extensionMonitor')||_0x36c8c4[_0x2563('0x36')]('context')){return _0x24db04[_0x2563('0x38')]['destroy']({'where':{'UserId':_0x36c8c4['id'],'type':_0x2563('0x1c'),'ChanSpyId':null}})[_0x2563('0x31')](function(){var _0x29df54=getUserApplications(_0x36c8c4,_0x36c8c4[_0x2563('0x28')]);if(_0x36c8c4[_0x2563('0x28')]!=_0x36c8c4[_0x2563('0x17')]){_0x29df54=_0x29df54[_0x2563('0x29')](getUserApplications(_0x36c8c4,_0x36c8c4[_0x2563('0x17')]));}return _0x24db04[_0x2563('0x38')][_0x2563('0x39')](_0x29df54);});}})[_0x2563('0x31')](function(){_0x375749();})['catch'](function(_0x317e95){_0x375749(_0x317e95);});},'afterDestroy':function(_0x24daf6,_0x26d875){return _0x24db04['VoiceExtension']['destroy']({'where':{'UserId':_0x24daf6['id'],'type':_0x2563('0x1c')}});}},'instanceMethods':{'authenticate':function(_0x74185c,_0x22765f){if(!_0x22765f){return this['password']===this['encryptPassword'](_0x74185c);}var _0x49d81c=this;this['encryptPassword'](_0x74185c,function(_0x2a6a02,_0x4e3b40){if(_0x2a6a02){_0x22765f(_0x2a6a02);}if(_0x49d81c[_0x2563('0x14')]===_0x4e3b40){_0x22765f(null,!![]);}else{_0x22765f(null,![]);}});},'makeSalt':function(_0x561e85,_0x2d1ce3){var _0x4eee66=0x10;if(typeof arguments[0x0]===_0x2563('0x3d')){_0x2d1ce3=arguments[0x0];_0x561e85=_0x4eee66;}else if(typeof arguments[0x1]===_0x2563('0x3d')){_0x2d1ce3=arguments[0x1];}else{throw new Error('Missing\x20Callback');}if(!_0x561e85){_0x561e85=_0x4eee66;}return crypto[_0x2563('0x3e')](_0x561e85,function(_0x2f7207,_0x7f48f9){if(_0x2f7207){_0x2d1ce3(_0x2f7207);}return _0x2d1ce3(null,_0x7f48f9[_0x2563('0x3f')]('base64'));});},'encryptPassword':function(_0x177091,_0x5f486d){if(!_0x177091||!this[_0x2563('0x40')]){return _0x5f486d?_0x5f486d(null):null;}var _0x38f1d4=0x2710;var _0x1f974b=0x40;var _0x4d2991=new Buffer(this[_0x2563('0x40')],_0x2563('0x41'));if(!_0x5f486d){return crypto[_0x2563('0x42')](_0x177091,_0x4d2991,_0x38f1d4,_0x1f974b,_0x2563('0x43'))[_0x2563('0x3f')](_0x2563('0x41'));}return crypto[_0x2563('0x44')](_0x177091,_0x4d2991,_0x38f1d4,_0x1f974b,'sha1',function(_0x39f6da,_0xc112f4){if(_0x39f6da){_0x5f486d(_0x39f6da);}return _0x5f486d(null,_0xc112f4[_0x2563('0x3f')](_0x2563('0x41')));});},'updatePassword':function(_0x331ee7){if(!this[_0x2563('0x14')]){return _0x331ee7(null);}if(!validatePresenceOf(this[_0x2563('0x14')])&&authTypes[_0x2563('0x45')](this[_0x2563('0x46')])===-0x1){_0x331ee7(new Error(_0x2563('0x47')));}var _0x2c59d5=this;this[_0x2563('0x48')](function(_0x376c3a,_0x402eac){if(_0x376c3a){return _0x331ee7(_0x376c3a);}_0x2c59d5[_0x2563('0x40')]=_0x402eac;_0x2c59d5['encryptPassword'](_0x2c59d5['password'],function(_0x253470,_0x41ab7c){if(_0x253470){_0x331ee7(_0x253470);}_0x2c59d5[_0x2563('0x14')]=_0x41ab7c;_0x331ee7(null);});});},'updateMd5Password':function(){this[_0x2563('0x49')]=md5(this[_0x2563('0x17')]+_0x2563('0x4a')+this[_0x2563('0x14')]);return this['md5secret'];}}});};