b8ad3c47ae86732019aa94e696b9be8d5e38d5a7
[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 _0x41e7=['WAV','gsm','CDR(type)=internal','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=%s','extensionMonitor','Dial','name','SIP/%s,,tTxX','defaults','findAll','then','internal','catch','models','define','users','utf8','utf8_bin','undefined','resolve','find','privacyEnabled','updatePassword','UserProfile','userProfileId','changed','concat','userSetting','createUserSetting','VoiceExtension','bulkCreate','destroy','encryptPassword','function','randomBytes','salt','base64','sha1','toString','indexOf','provider','Invalid\x20password','makeSalt','md5secret',':asterisk:','bluebird','crypto','md5','lodash','./user.attributes','google','length','context','from-sip','system','format','%s%s','prefix','push','merge','Set','CDR(type)=%s','chanspy','record','MixMonitor','${UNIQUEID}.%s,a','recordingFormat','auth','password','SIP/%s,%s','options','StopMixMonitor','wav'];(function(_0x1e175d,_0x41ff45){var _0x3a3ce5=function(_0x186001){while(--_0x186001){_0x1e175d['push'](_0x1e175d['shift']());}};_0x3a3ce5(++_0x41ff45);}(_0x41e7,0x1ea));var _0x741e=function(_0x432682,_0x253044){_0x432682=_0x432682-0x0;var _0x463d05=_0x41e7[_0x432682];return _0x463d05;};'use strict';var BPromise=require(_0x741e('0x0'));var crypto=require(_0x741e('0x1'));var util=require('util');var md5=require(_0x741e('0x2'));var _=require(_0x741e('0x3'));var attributes=require(_0x741e('0x4'));var authTypes=[_0x741e('0x5')];var validatePresenceOf=function(_0x593282){return _0x593282&&_0x593282[_0x741e('0x6')];};var getChanSpyApplications=function(_0x7d72ba,_0x74d9f1,_0x52e7be){var _0x27a75b={'context':_0x7d72ba[_0x741e('0x7')]||_0x741e('0x8'),'type':_0x741e('0x9'),'isApp':![],'UserId':_0x7d72ba['id']};var _0x3815b3=[];for(var _0x3ed6bd=0x0,_0x1f0774=0x1;_0x3ed6bd<_0x52e7be[_0x741e('0x6')];_0x3ed6bd++,_0x1f0774=0x1){var _0x1cbfba={'exten':util[_0x741e('0xa')](_0x741e('0xb'),_0x52e7be[_0x3ed6bd][_0x741e('0xc')],_0x74d9f1),'ChanSpyId':_0x52e7be[_0x3ed6bd]['id']};_0x3815b3[_0x741e('0xd')](_[_0x741e('0xe')]({'app':_0x741e('0xf'),'appdata':util[_0x741e('0xa')](_0x741e('0x10'),_0x741e('0x11')),'priority':_0x1f0774++},_0x27a75b,_0x1cbfba));if(_0x52e7be[_0x3ed6bd][_0x741e('0x12')]){_0x3815b3['push'](_['merge']({'app':_0x741e('0x13'),'appdata':util[_0x741e('0xa')](_0x741e('0x14'),_0x52e7be[_0x3ed6bd][_0x741e('0x15')]),'priority':_0x1f0774++},_0x27a75b,_0x1cbfba));}if(_0x52e7be[_0x3ed6bd][_0x741e('0x16')]){_0x3815b3['push'](_[_0x741e('0xe')]({'app':'Authenticate','appdata':_0x52e7be[_0x3ed6bd][_0x741e('0x17')],'priority':_0x1f0774++},_0x27a75b,_0x1cbfba));}_0x3815b3[_0x741e('0xd')](_['merge']({'app':'ChanSpy','appdata':util['format'](_0x741e('0x18'),_0x7d72ba['name'],_0x52e7be[_0x3ed6bd]['options']?_0x52e7be[_0x3ed6bd][_0x741e('0x19')]['join'](''):''),'priority':_0x1f0774++},_0x27a75b,_0x1cbfba));if(_0x52e7be[_0x3ed6bd][_0x741e('0x12')]){_0x3815b3[_0x741e('0xd')](_[_0x741e('0xe')]({'app':_0x741e('0x1a'),'priority':_0x1f0774++},_0x27a75b,_0x1cbfba));}}return _0x3815b3;};var getUserApplications=function(_0x520e7e,_0xa6ed1a){var _0x201db7=[_0x741e('0x1b'),_0x741e('0x1c'),_0x741e('0x1d')];var _0x31856b={'context':_0x520e7e[_0x741e('0x7')]||_0x741e('0x8'),'exten':_0xa6ed1a,'type':'system','isApp':![],'UserId':_0x520e7e['id']};var _0x3ae534=[{'app':_0x741e('0xf'),'appdata':_0x741e('0x1e')}];if(_0x520e7e['extensionMonitor']&&_['includes'](_0x201db7,_0x520e7e['extensionMonitor'])){_0x3ae534[_0x741e('0xd')]({'app':_0x741e('0xf'),'appdata':_0x741e('0x1f')});_0x3ae534[_0x741e('0xd')]({'app':_0x741e('0xf'),'appdata':util[_0x741e('0xa')](_0x741e('0x20'),_0x520e7e[_0x741e('0x21')])});_0x3ae534['push']({'app':_0x741e('0x22'),'appdata':util[_0x741e('0xa')]('SIP/%s,,tTxXU(xcally-mixmonitor-context)',_0x520e7e[_0x741e('0x23')])});}else{_0x3ae534['push']({'app':'Dial','appdata':util[_0x741e('0xa')](_0x741e('0x24'),_0x520e7e[_0x741e('0x23')])});}_0x3ae534[_0x741e('0xd')]({'app':'Hangup'});for(var _0x245d95=0x0;_0x245d95<_0x3ae534['length'];_0x245d95++){_0x3ae534[_0x245d95]['priority']=_0x245d95+0x1;_0x3ae534[_0x245d95]=_[_0x741e('0x25')](_0x3ae534[_0x245d95],_0x31856b);}return _0x3ae534;};var getChanSpyPromise=function(_0x549b3d,_0x16d311){return new BPromise(function(_0x37d87b,_0x5d0e5){return _0x549b3d['VoiceChanSpy'][_0x741e('0x26')]()[_0x741e('0x27')](function(_0x5e8559){if(_0x5e8559){var _0x1bcde1=getChanSpyApplications(_0x16d311,_0x16d311[_0x741e('0x28')],_0x5e8559);if(_0x16d311[_0x741e('0x28')]!=_0x16d311[_0x741e('0x23')]){_0x1bcde1=_0x1bcde1['concat'](getChanSpyApplications(_0x16d311,_0x16d311[_0x741e('0x23')],_0x5e8559));}return _0x37d87b(_0x1bcde1);}return _0x37d87b([]);})[_0x741e('0x29')](function(_0x9bfc3f){_0x5d0e5(_0x9bfc3f);});});};module['exports']=function(_0x479639){var _0x54eb5d=_0x479639[_0x741e('0x2a')];return _0x479639[_0x741e('0x2b')]('User',attributes,{'tableName':_0x741e('0x2c'),'charset':_0x741e('0x2d'),'collate':_0x741e('0x2e'),'hooks':{'beforeCreate':function(_0x565a42,_0x1a8b72,_0x463e0e){var _0x4ff62=typeof _0x565a42['userProfileId']===_0x741e('0x2f')?BPromise[_0x741e('0x30')](null):_0x54eb5d['UserProfile'][_0x741e('0x31')]({'where':{'id':_0x565a42['userProfileId']}});_0x4ff62[_0x741e('0x27')](function(_0x5c7c27){if(_0x5c7c27){_0x565a42[_0x741e('0x32')]=_0x5c7c27[_0x741e('0x32')];}})[_0x741e('0x27')](function(){_0x565a42['updateMd5Password']();_0x565a42[_0x741e('0x33')](_0x463e0e);})[_0x741e('0x29')](function(_0x37c00b){_0x463e0e(_0x37c00b);});},'beforeUpdate':function(_0xd9c4ca,_0x9f8b17,_0x6d33e7){Promise[_0x741e('0x30')]()[_0x741e('0x27')](function(){if(_0xd9c4ca['changed']('userProfileId')===![])return;return _0x54eb5d[_0x741e('0x34')][_0x741e('0x31')]({'where':{'id':_0xd9c4ca[_0x741e('0x35')]}})[_0x741e('0x27')](function(_0x30018b){if(_0x30018b)_0xd9c4ca[_0x741e('0x32')]=_0x30018b[_0x741e('0x32')];return;});})[_0x741e('0x27')](function(){if(_0xd9c4ca[_0x741e('0x36')](_0x741e('0x17'))){_0xd9c4ca['updateMd5Password']();return _0xd9c4ca['updatePassword'](_0x6d33e7);}_0x6d33e7();})[_0x741e('0x29')](function(_0x3cd3c8){_0x6d33e7(_0x3cd3c8);});},'afterCreate':function(_0xb4a1e7,_0x4d0451,_0x4b2abc){var _0x3b8eb0=getUserApplications(_0xb4a1e7,_0xb4a1e7[_0x741e('0x28')]);if(_0xb4a1e7[_0x741e('0x28')]!=_0xb4a1e7['name']){_0x3b8eb0=_0x3b8eb0[_0x741e('0x37')](getUserApplications(_0xb4a1e7,_0xb4a1e7[_0x741e('0x23')]));}return Promise['resolve']()[_0x741e('0x27')](function(){if(!_0xb4a1e7[_0x741e('0x38')]){return _0xb4a1e7[_0x741e('0x39')]({'clientSideLoggingLevel':'error','clientSideLoggingEnabled':![]});}})[_0x741e('0x27')](function(){return _0x54eb5d[_0x741e('0x3a')][_0x741e('0x3b')](_0x3b8eb0);})['then'](function(){if(_0xb4a1e7[_0x741e('0x11')]){return getChanSpyPromise(_0x54eb5d,_0xb4a1e7);}})[_0x741e('0x27')](function(_0x5f4c78){if(_0x5f4c78&&_0x5f4c78[_0x741e('0x6')]){return _0x54eb5d[_0x741e('0x3a')][_0x741e('0x3b')](_0x5f4c78);}})[_0x741e('0x27')](function(){_0x4b2abc();})[_0x741e('0x29')](function(_0x7d6a0e){_0x4b2abc(_0x7d6a0e);});},'afterUpdate':function(_0x13aaae,_0x5b3af4,_0x1ea26d){return BPromise[_0x741e('0x30')]()['then'](function(){if(_0x13aaae[_0x741e('0x36')](_0x741e('0x11'))||_0x13aaae[_0x741e('0x36')]('context')){return _0x54eb5d[_0x741e('0x3a')]['destroy']({'where':{'UserId':_0x13aaae['id'],'type':'system','ChanSpyId':{'$ne':null}}})[_0x741e('0x27')](function(){if(_0x13aaae[_0x741e('0x11')]){return getChanSpyPromise(_0x54eb5d,_0x13aaae)[_0x741e('0x27')](function(_0x558904){if(_0x558904&&_0x558904['length']){return _0x54eb5d[_0x741e('0x3a')][_0x741e('0x3b')](_0x558904);}});}});}})['then'](function(){if(_0x13aaae[_0x741e('0x36')](_0x741e('0x21'))||_0x13aaae[_0x741e('0x36')](_0x741e('0x7'))){return _0x54eb5d[_0x741e('0x3a')][_0x741e('0x3c')]({'where':{'UserId':_0x13aaae['id'],'type':_0x741e('0x9'),'ChanSpyId':null}})[_0x741e('0x27')](function(){var _0x36a7f3=getUserApplications(_0x13aaae,_0x13aaae[_0x741e('0x28')]);if(_0x13aaae[_0x741e('0x28')]!=_0x13aaae['name']){_0x36a7f3=_0x36a7f3[_0x741e('0x37')](getUserApplications(_0x13aaae,_0x13aaae[_0x741e('0x23')]));}return _0x54eb5d['VoiceExtension'][_0x741e('0x3b')](_0x36a7f3);});}})[_0x741e('0x27')](function(){_0x1ea26d();})['catch'](function(_0x328c3f){_0x1ea26d(_0x328c3f);});},'afterDestroy':function(_0x1048b5){return _0x54eb5d['VoiceExtension'][_0x741e('0x3c')]({'where':{'UserId':_0x1048b5['id'],'type':_0x741e('0x9')}});}},'instanceMethods':{'authenticate':function(_0x3e6df5,_0x3074ae){if(!_0x3074ae){return this[_0x741e('0x17')]===this[_0x741e('0x3d')](_0x3e6df5);}var _0x1335e8=this;this[_0x741e('0x3d')](_0x3e6df5,function(_0xcc7fbc,_0x2a506b){if(_0xcc7fbc){_0x3074ae(_0xcc7fbc);}if(_0x1335e8[_0x741e('0x17')]===_0x2a506b){_0x3074ae(null,!![]);}else{_0x3074ae(null,![]);}});},'makeSalt':function(_0x26aabb,_0x3ecebf){var _0x1f2fca=0x10;if(typeof arguments[0x0]==='function'){_0x3ecebf=arguments[0x0];_0x26aabb=_0x1f2fca;}else if(typeof arguments[0x1]===_0x741e('0x3e')){_0x3ecebf=arguments[0x1];}else{throw new Error('Missing\x20Callback');}if(!_0x26aabb){_0x26aabb=_0x1f2fca;}return crypto[_0x741e('0x3f')](_0x26aabb,function(_0x507004,_0x3bf038){if(_0x507004){_0x3ecebf(_0x507004);}return _0x3ecebf(null,_0x3bf038['toString']('base64'));});},'encryptPassword':function(_0x17d8ae,_0x2892c2){if(!_0x17d8ae||!this[_0x741e('0x40')]){return _0x2892c2?_0x2892c2(null):null;}var _0x48665e=0x2710;var _0x24acfe=0x40;var _0x4680e0=new Buffer(this[_0x741e('0x40')],_0x741e('0x41'));if(!_0x2892c2){return crypto['pbkdf2Sync'](_0x17d8ae,_0x4680e0,_0x48665e,_0x24acfe,_0x741e('0x42'))[_0x741e('0x43')](_0x741e('0x41'));}return crypto['pbkdf2'](_0x17d8ae,_0x4680e0,_0x48665e,_0x24acfe,_0x741e('0x42'),function(_0x36b558,_0x4ed670){if(_0x36b558){_0x2892c2(_0x36b558);}return _0x2892c2(null,_0x4ed670[_0x741e('0x43')](_0x741e('0x41')));});},'updatePassword':function(_0xcca24c){if(!this['password']){return _0xcca24c(null);}if(!validatePresenceOf(this[_0x741e('0x17')])&&authTypes[_0x741e('0x44')](this[_0x741e('0x45')])===-0x1){_0xcca24c(new Error(_0x741e('0x46')));}var _0x1f9e03=this;this[_0x741e('0x47')](function(_0x227c72,_0x40a95f){if(_0x227c72){return _0xcca24c(_0x227c72);}_0x1f9e03[_0x741e('0x40')]=_0x40a95f;_0x1f9e03['encryptPassword'](_0x1f9e03['password'],function(_0x26012d,_0x351ace){if(_0x26012d){_0xcca24c(_0x26012d);}_0x1f9e03[_0x741e('0x17')]=_0x351ace;_0xcca24c(null);});});},'updateMd5Password':function(){this[_0x741e('0x48')]=md5(this[_0x741e('0x23')]+_0x741e('0x49')+this['password']);return this['md5secret'];}}});};