Built motion from commit (unavailable).|2.0.83
[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 _0xa9e0=['findAll','internal','concat','catch','exports','define','User','users','utf8','utf8_bin','updateMd5Password','changed','updatePassword','bulkCreate','chanspy','then','VoiceExtension','destroy','encryptPassword','function','Missing\x20Callback','randomBytes','toString','salt','base64','sha1','pbkdf2','indexOf','Invalid\x20password','makeSalt','md5secret','bluebird','crypto','util','./user.attributes','length','context','from-sip','system','format','%s%s','prefix','record','push','merge','auth','Authenticate','password','ChanSpy','SIP/%s,%s','name','options','wav','gsm','Set','CDR(type)=internal','extensionMonitor','includes','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','Dial','SIP/%s,,tTxXU(xcally-mixmonitor-context)','SIP/%s,,tTxX','Hangup','priority','defaults'];(function(_0x1e1ded,_0x432491){var _0x5302e2=function(_0x30e064){while(--_0x30e064){_0x1e1ded['push'](_0x1e1ded['shift']());}};_0x5302e2(++_0x432491);}(_0xa9e0,0x123));var _0x0a9e=function(_0x43d11d,_0xccff91){_0x43d11d=_0x43d11d-0x0;var _0x38cd08=_0xa9e0[_0x43d11d];return _0x38cd08;};'use strict';var BPromise=require(_0x0a9e('0x0'));var crypto=require(_0x0a9e('0x1'));var util=require(_0x0a9e('0x2'));var md5=require('md5');var _=require('lodash');var attributes=require(_0x0a9e('0x3'));var authTypes=['google'];var validatePresenceOf=function(_0x333808){return _0x333808&&_0x333808[_0x0a9e('0x4')];};var getChanSpyApplications=function(_0x2ab90b,_0x991246,_0x981158){var _0x57b080={'context':_0x2ab90b[_0x0a9e('0x5')]||_0x0a9e('0x6'),'type':_0x0a9e('0x7'),'isApp':![],'UserId':_0x2ab90b['id']};var _0x5f41ea=[];for(var _0x503809=0x0,_0xe42b77=0x1;_0x503809<_0x981158['length'];_0x503809++,_0xe42b77=0x1){var _0x52cace={'exten':util[_0x0a9e('0x8')](_0x0a9e('0x9'),_0x981158[_0x503809][_0x0a9e('0xa')],_0x991246),'ChanSpyId':_0x981158[_0x503809]['id']};if(_0x981158[_0x503809][_0x0a9e('0xb')]){_0x5f41ea[_0x0a9e('0xc')](_[_0x0a9e('0xd')]({'app':'MixMonitor','appdata':util['format']('${UNIQUEID}.%s,ab',_0x981158[_0x503809]['recordingFormat']),'priority':_0xe42b77++},_0x57b080,_0x52cace));}if(_0x981158[_0x503809][_0x0a9e('0xe')]){_0x5f41ea[_0x0a9e('0xc')](_[_0x0a9e('0xd')]({'app':_0x0a9e('0xf'),'appdata':_0x981158[_0x503809][_0x0a9e('0x10')],'priority':_0xe42b77++},_0x57b080,_0x52cace));}_0x5f41ea[_0x0a9e('0xc')](_['merge']({'app':_0x0a9e('0x11'),'appdata':util[_0x0a9e('0x8')](_0x0a9e('0x12'),_0x2ab90b[_0x0a9e('0x13')],_0x981158[_0x503809][_0x0a9e('0x14')]?_0x981158[_0x503809][_0x0a9e('0x14')]['join'](''):''),'priority':_0xe42b77++},_0x57b080,_0x52cace));if(_0x981158[_0x503809][_0x0a9e('0xb')]){_0x5f41ea['push'](_[_0x0a9e('0xd')]({'app':'StopMixMonitor','priority':_0xe42b77++},_0x57b080,_0x52cace));}}return _0x5f41ea;};var getUserApplications=function(_0x16033c,_0x12066b){var _0x48f32d=[_0x0a9e('0x15'),'WAV',_0x0a9e('0x16')];var _0x478723={'context':_0x16033c[_0x0a9e('0x5')]||_0x0a9e('0x6'),'exten':_0x12066b,'type':_0x0a9e('0x7'),'isApp':![],'UserId':_0x16033c['id']};var _0x2eb11f=[{'app':_0x0a9e('0x17'),'appdata':_0x0a9e('0x18')}];if(_0x16033c[_0x0a9e('0x19')]&&_[_0x0a9e('0x1a')](_0x48f32d,_0x16033c[_0x0a9e('0x19')])){_0x2eb11f[_0x0a9e('0xc')]({'app':'Set','appdata':_0x0a9e('0x1b')});_0x2eb11f[_0x0a9e('0xc')]({'app':'Set','appdata':util[_0x0a9e('0x8')]('_XCALLY_MIXMONITOR_FORMAT=%s',_0x16033c['extensionMonitor'])});_0x2eb11f[_0x0a9e('0xc')]({'app':_0x0a9e('0x1c'),'appdata':util[_0x0a9e('0x8')](_0x0a9e('0x1d'),_0x16033c['name'])});}else{_0x2eb11f[_0x0a9e('0xc')]({'app':_0x0a9e('0x1c'),'appdata':util[_0x0a9e('0x8')](_0x0a9e('0x1e'),_0x16033c[_0x0a9e('0x13')])});}_0x2eb11f[_0x0a9e('0xc')]({'app':_0x0a9e('0x1f')});for(var _0x5ea472=0x0;_0x5ea472<_0x2eb11f['length'];_0x5ea472++){_0x2eb11f[_0x5ea472][_0x0a9e('0x20')]=_0x5ea472+0x1;_0x2eb11f[_0x5ea472]=_[_0x0a9e('0x21')](_0x2eb11f[_0x5ea472],_0x478723);}return _0x2eb11f;};var getChanSpyPromise=function(_0x40c424,_0x33161b){return new BPromise(function(_0x526cf3,_0x384319){return _0x40c424['VoiceChanSpy'][_0x0a9e('0x22')]()['then'](function(_0x1dc186){if(_0x1dc186){var _0x34dc65=getChanSpyApplications(_0x33161b,_0x33161b[_0x0a9e('0x23')],_0x1dc186);if(_0x33161b[_0x0a9e('0x23')]!=_0x33161b[_0x0a9e('0x13')]){_0x34dc65=_0x34dc65[_0x0a9e('0x24')](getChanSpyApplications(_0x33161b,_0x33161b[_0x0a9e('0x13')],_0x1dc186));}return _0x526cf3(_0x34dc65);}return _0x526cf3([]);})[_0x0a9e('0x25')](function(_0x5a101a){_0x384319(_0x5a101a);});});};module[_0x0a9e('0x26')]=function(_0x468813,_0x2b1f27){var _0x2ec755=_0x468813['models'];return _0x468813[_0x0a9e('0x27')](_0x0a9e('0x28'),attributes,{'tableName':_0x0a9e('0x29'),'charset':_0x0a9e('0x2a'),'collate':_0x0a9e('0x2b'),'hooks':{'beforeCreate':function(_0x563b45,_0x296f41,_0x2e8759){_0x563b45[_0x0a9e('0x2c')]();_0x563b45['updatePassword'](_0x2e8759);},'beforeUpdate':function(_0x426950,_0x5d1e0e,_0x2407f9){if(_0x426950[_0x0a9e('0x2d')](_0x0a9e('0x10'))){_0x426950[_0x0a9e('0x2c')]();return _0x426950[_0x0a9e('0x2e')](_0x2407f9);}_0x2407f9();},'afterCreate':function(_0x5180f1,_0x66c1d2,_0x46f0a5){var _0x40cb85=getUserApplications(_0x5180f1,_0x5180f1[_0x0a9e('0x23')]);if(_0x5180f1[_0x0a9e('0x23')]!=_0x5180f1[_0x0a9e('0x13')]){_0x40cb85=_0x40cb85[_0x0a9e('0x24')](getUserApplications(_0x5180f1,_0x5180f1[_0x0a9e('0x13')]));}return _0x2ec755['VoiceExtension'][_0x0a9e('0x2f')](_0x40cb85)['then'](function(){if(_0x5180f1[_0x0a9e('0x30')]){return getChanSpyPromise(_0x2ec755,_0x5180f1);}})[_0x0a9e('0x31')](function(_0x2b7e7f){if(_0x2b7e7f&&_0x2b7e7f[_0x0a9e('0x4')]){return _0x2ec755['VoiceExtension'][_0x0a9e('0x2f')](_0x2b7e7f);}})[_0x0a9e('0x31')](function(){_0x46f0a5();})[_0x0a9e('0x25')](function(_0x15de0e){_0x46f0a5(_0x15de0e);});},'afterUpdate':function(_0x4853b1,_0x54135e,_0x1e8694){return BPromise['resolve']()[_0x0a9e('0x31')](function(){if(_0x4853b1[_0x0a9e('0x2d')](_0x0a9e('0x30'))||_0x4853b1[_0x0a9e('0x2d')]('context')){return _0x2ec755[_0x0a9e('0x32')][_0x0a9e('0x33')]({'where':{'UserId':_0x4853b1['id'],'type':_0x0a9e('0x7'),'ChanSpyId':{'$ne':null}}})['then'](function(){if(_0x4853b1[_0x0a9e('0x30')]){return getChanSpyPromise(_0x2ec755,_0x4853b1)[_0x0a9e('0x31')](function(_0x289467){if(_0x289467&&_0x289467['length']){return _0x2ec755['VoiceExtension'][_0x0a9e('0x2f')](_0x289467);}});}});}})[_0x0a9e('0x31')](function(){if(_0x4853b1['changed'](_0x0a9e('0x19'))||_0x4853b1[_0x0a9e('0x2d')](_0x0a9e('0x5'))){return _0x2ec755[_0x0a9e('0x32')]['destroy']({'where':{'UserId':_0x4853b1['id'],'type':'system','ChanSpyId':null}})[_0x0a9e('0x31')](function(){var _0x45b3d0=getUserApplications(_0x4853b1,_0x4853b1['internal']);if(_0x4853b1[_0x0a9e('0x23')]!=_0x4853b1[_0x0a9e('0x13')]){_0x45b3d0=_0x45b3d0[_0x0a9e('0x24')](getUserApplications(_0x4853b1,_0x4853b1['name']));}return _0x2ec755[_0x0a9e('0x32')][_0x0a9e('0x2f')](_0x45b3d0);});}})[_0x0a9e('0x31')](function(){_0x1e8694();})[_0x0a9e('0x25')](function(_0x13cb1f){_0x1e8694(_0x13cb1f);});},'afterDestroy':function(_0xeaa5e5,_0x2881b3){return _0x2ec755[_0x0a9e('0x32')][_0x0a9e('0x33')]({'where':{'UserId':_0xeaa5e5['id'],'type':_0x0a9e('0x7')}});}},'instanceMethods':{'authenticate':function(_0x2c3711,_0x148007){if(!_0x148007){return this[_0x0a9e('0x10')]===this[_0x0a9e('0x34')](_0x2c3711);}var _0x37f6cc=this;this['encryptPassword'](_0x2c3711,function(_0x52628e,_0x473fdc){if(_0x52628e){_0x148007(_0x52628e);}if(_0x37f6cc[_0x0a9e('0x10')]===_0x473fdc){_0x148007(null,!![]);}else{_0x148007(null,![]);}});},'makeSalt':function(_0x405c8f,_0x315881){var _0x1a69dd=0x10;if(typeof arguments[0x0]===_0x0a9e('0x35')){_0x315881=arguments[0x0];_0x405c8f=_0x1a69dd;}else if(typeof arguments[0x1]===_0x0a9e('0x35')){_0x315881=arguments[0x1];}else{throw new Error(_0x0a9e('0x36'));}if(!_0x405c8f){_0x405c8f=_0x1a69dd;}return crypto[_0x0a9e('0x37')](_0x405c8f,function(_0x25c6e6,_0x2c2cb0){if(_0x25c6e6){_0x315881(_0x25c6e6);}return _0x315881(null,_0x2c2cb0[_0x0a9e('0x38')]('base64'));});},'encryptPassword':function(_0x117c99,_0x138b8b){if(!_0x117c99||!this[_0x0a9e('0x39')]){return _0x138b8b?_0x138b8b(null):null;}var _0xfbd5ad=0x2710;var _0x2e8caf=0x40;var _0xbe9d19=new Buffer(this[_0x0a9e('0x39')],_0x0a9e('0x3a'));if(!_0x138b8b){return crypto['pbkdf2Sync'](_0x117c99,_0xbe9d19,_0xfbd5ad,_0x2e8caf,_0x0a9e('0x3b'))['toString'](_0x0a9e('0x3a'));}return crypto[_0x0a9e('0x3c')](_0x117c99,_0xbe9d19,_0xfbd5ad,_0x2e8caf,_0x0a9e('0x3b'),function(_0x562fb4,_0x14bec2){if(_0x562fb4){_0x138b8b(_0x562fb4);}return _0x138b8b(null,_0x14bec2[_0x0a9e('0x38')](_0x0a9e('0x3a')));});},'updatePassword':function(_0x34aa5a){if(!this[_0x0a9e('0x10')]){return _0x34aa5a(null);}if(!validatePresenceOf(this[_0x0a9e('0x10')])&&authTypes[_0x0a9e('0x3d')](this['provider'])===-0x1){_0x34aa5a(new Error(_0x0a9e('0x3e')));}var _0x1bde3c=this;this[_0x0a9e('0x3f')](function(_0x16f976,_0x409789){if(_0x16f976){return _0x34aa5a(_0x16f976);}_0x1bde3c[_0x0a9e('0x39')]=_0x409789;_0x1bde3c[_0x0a9e('0x34')](_0x1bde3c[_0x0a9e('0x10')],function(_0x34febd,_0x58a5c2){if(_0x34febd){_0x34aa5a(_0x34febd);}_0x1bde3c[_0x0a9e('0x10')]=_0x58a5c2;_0x34aa5a(null);});});},'updateMd5Password':function(){this[_0x0a9e('0x40')]=md5(this['name']+':asterisk:'+this['password']);return this[_0x0a9e('0x40')];}}});};