d50d5cf433fb18f0207dd4c618b71c013c255ae5
[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 _0xac5e=['SIP/%s,%s','name','options','record','StopMixMonitor','wav','WAV','gsm','context','CDR(type)=internal','extensionMonitor','includes','_XCALLY_MIXMONITOR_FORMAT=%s','Dial','SIP/%s,,tTxX','Hangup','priority','defaults','findAll','then','internal','concat','catch','models','define','User','utf8_bin','userProfileId','resolve','UserProfile','find','privacyEnabled','updateMd5Password','updatePassword','changed','userSetting','error','VoiceExtension','bulkCreate','chanspy','destroy','encryptPassword','function','Missing\x20Callback','randomBytes','base64','salt','sha1','toString','pbkdf2','password','indexOf','provider','Invalid\x20password','makeSalt','md5secret',':asterisk:','bluebird','util','md5','lodash','google','length','system','format','%s%s','prefix','merge','Set','CDR(type)=%s','push','MixMonitor','${UNIQUEID}.%s,a','recordingFormat','auth','Authenticate','ChanSpy'];(function(_0x4b7b62,_0x4920a9){var _0x2280df=function(_0x21e1f8){while(--_0x21e1f8){_0x4b7b62['push'](_0x4b7b62['shift']());}};_0x2280df(++_0x4920a9);}(_0xac5e,0x120));var _0xeac5=function(_0x357df1,_0x3dbbe4){_0x357df1=_0x357df1-0x0;var _0x5461cb=_0xac5e[_0x357df1];return _0x5461cb;};'use strict';var BPromise=require(_0xeac5('0x0'));var crypto=require('crypto');var util=require(_0xeac5('0x1'));var md5=require(_0xeac5('0x2'));var _=require(_0xeac5('0x3'));var attributes=require('./user.attributes');var authTypes=[_0xeac5('0x4')];var validatePresenceOf=function(_0x35f234){return _0x35f234&&_0x35f234[_0xeac5('0x5')];};var getChanSpyApplications=function(_0x2c5e20,_0x4de4c0,_0x4e9447){var _0x2ffddb={'context':_0x2c5e20['context']||'from-sip','type':_0xeac5('0x6'),'isApp':![],'UserId':_0x2c5e20['id']};var _0x350b1a=[];for(var _0x2a6ad7=0x0,_0x2a24a7=0x1;_0x2a6ad7<_0x4e9447[_0xeac5('0x5')];_0x2a6ad7++,_0x2a24a7=0x1){var _0x3341a3={'exten':util[_0xeac5('0x7')](_0xeac5('0x8'),_0x4e9447[_0x2a6ad7][_0xeac5('0x9')],_0x4de4c0),'ChanSpyId':_0x4e9447[_0x2a6ad7]['id']};_0x350b1a['push'](_[_0xeac5('0xa')]({'app':_0xeac5('0xb'),'appdata':util[_0xeac5('0x7')](_0xeac5('0xc'),'chanspy'),'priority':_0x2a24a7++},_0x2ffddb,_0x3341a3));if(_0x4e9447[_0x2a6ad7]['record']){_0x350b1a[_0xeac5('0xd')](_[_0xeac5('0xa')]({'app':_0xeac5('0xe'),'appdata':util[_0xeac5('0x7')](_0xeac5('0xf'),_0x4e9447[_0x2a6ad7][_0xeac5('0x10')]),'priority':_0x2a24a7++},_0x2ffddb,_0x3341a3));}if(_0x4e9447[_0x2a6ad7][_0xeac5('0x11')]){_0x350b1a[_0xeac5('0xd')](_[_0xeac5('0xa')]({'app':_0xeac5('0x12'),'appdata':_0x4e9447[_0x2a6ad7]['password'],'priority':_0x2a24a7++},_0x2ffddb,_0x3341a3));}_0x350b1a[_0xeac5('0xd')](_[_0xeac5('0xa')]({'app':_0xeac5('0x13'),'appdata':util[_0xeac5('0x7')](_0xeac5('0x14'),_0x2c5e20[_0xeac5('0x15')],_0x4e9447[_0x2a6ad7][_0xeac5('0x16')]?_0x4e9447[_0x2a6ad7][_0xeac5('0x16')]['join'](''):''),'priority':_0x2a24a7++},_0x2ffddb,_0x3341a3));if(_0x4e9447[_0x2a6ad7][_0xeac5('0x17')]){_0x350b1a[_0xeac5('0xd')](_[_0xeac5('0xa')]({'app':_0xeac5('0x18'),'priority':_0x2a24a7++},_0x2ffddb,_0x3341a3));}}return _0x350b1a;};var getUserApplications=function(_0x11cea6,_0x1caec3){var _0x313af7=[_0xeac5('0x19'),_0xeac5('0x1a'),_0xeac5('0x1b')];var _0x362ef9={'context':_0x11cea6[_0xeac5('0x1c')]||'from-sip','exten':_0x1caec3,'type':_0xeac5('0x6'),'isApp':![],'UserId':_0x11cea6['id']};var _0x30d4f4=[{'app':_0xeac5('0xb'),'appdata':_0xeac5('0x1d')}];if(_0x11cea6[_0xeac5('0x1e')]&&_[_0xeac5('0x1f')](_0x313af7,_0x11cea6['extensionMonitor'])){_0x30d4f4[_0xeac5('0xd')]({'app':_0xeac5('0xb'),'appdata':'_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}'});_0x30d4f4[_0xeac5('0xd')]({'app':_0xeac5('0xb'),'appdata':util[_0xeac5('0x7')](_0xeac5('0x20'),_0x11cea6[_0xeac5('0x1e')])});_0x30d4f4[_0xeac5('0xd')]({'app':_0xeac5('0x21'),'appdata':util[_0xeac5('0x7')]('SIP/%s,,tTxXU(xcally-mixmonitor-context)',_0x11cea6[_0xeac5('0x15')])});}else{_0x30d4f4['push']({'app':_0xeac5('0x21'),'appdata':util[_0xeac5('0x7')](_0xeac5('0x22'),_0x11cea6[_0xeac5('0x15')])});}_0x30d4f4['push']({'app':_0xeac5('0x23')});for(var _0x3c67a7=0x0;_0x3c67a7<_0x30d4f4[_0xeac5('0x5')];_0x3c67a7++){_0x30d4f4[_0x3c67a7][_0xeac5('0x24')]=_0x3c67a7+0x1;_0x30d4f4[_0x3c67a7]=_[_0xeac5('0x25')](_0x30d4f4[_0x3c67a7],_0x362ef9);}return _0x30d4f4;};var getChanSpyPromise=function(_0x25b513,_0x199dce){return new BPromise(function(_0x7df8b4,_0x219df2){return _0x25b513['VoiceChanSpy'][_0xeac5('0x26')]()[_0xeac5('0x27')](function(_0x2597ed){if(_0x2597ed){var _0x320adf=getChanSpyApplications(_0x199dce,_0x199dce[_0xeac5('0x28')],_0x2597ed);if(_0x199dce[_0xeac5('0x28')]!=_0x199dce[_0xeac5('0x15')]){_0x320adf=_0x320adf[_0xeac5('0x29')](getChanSpyApplications(_0x199dce,_0x199dce[_0xeac5('0x15')],_0x2597ed));}return _0x7df8b4(_0x320adf);}return _0x7df8b4([]);})[_0xeac5('0x2a')](function(_0x1bdbc7){_0x219df2(_0x1bdbc7);});});};module['exports']=function(_0x40818a){var _0x292c0d=_0x40818a[_0xeac5('0x2b')];return _0x40818a[_0xeac5('0x2c')](_0xeac5('0x2d'),attributes,{'tableName':'users','charset':'utf8','collate':_0xeac5('0x2e'),'hooks':{'beforeCreate':function(_0x11e7d2,_0x1cbf0c,_0x3e13e7){var _0x457705=typeof _0x11e7d2[_0xeac5('0x2f')]==='undefined'?BPromise[_0xeac5('0x30')](null):_0x292c0d[_0xeac5('0x31')][_0xeac5('0x32')]({'where':{'id':_0x11e7d2['userProfileId']}});_0x457705[_0xeac5('0x27')](function(_0x230e8a){if(_0x230e8a){_0x11e7d2[_0xeac5('0x33')]=_0x230e8a[_0xeac5('0x33')];}})[_0xeac5('0x27')](function(){_0x11e7d2[_0xeac5('0x34')]();_0x11e7d2[_0xeac5('0x35')](_0x3e13e7);})[_0xeac5('0x2a')](function(_0x4baef5){_0x3e13e7(_0x4baef5);});},'beforeUpdate':function(_0x54bccb,_0x13c157,_0x3ac7e8){Promise['resolve']()['then'](function(){if(_0x54bccb[_0xeac5('0x36')](_0xeac5('0x2f'))===![])return;return _0x292c0d['UserProfile']['find']({'where':{'id':_0x54bccb[_0xeac5('0x2f')]}})['then'](function(_0x28a589){if(_0x28a589)_0x54bccb[_0xeac5('0x33')]=_0x28a589[_0xeac5('0x33')];return;});})['then'](function(){if(_0x54bccb[_0xeac5('0x36')]('password')){_0x54bccb['updateMd5Password']();return _0x54bccb['updatePassword'](_0x3ac7e8);}_0x3ac7e8();})[_0xeac5('0x2a')](function(_0x2cdef4){_0x3ac7e8(_0x2cdef4);});},'afterCreate':function(_0x3f8b7c,_0x56232a,_0x1d2070){var _0x11c0a6=getUserApplications(_0x3f8b7c,_0x3f8b7c[_0xeac5('0x28')]);if(_0x3f8b7c['internal']!=_0x3f8b7c['name']){_0x11c0a6=_0x11c0a6['concat'](getUserApplications(_0x3f8b7c,_0x3f8b7c[_0xeac5('0x15')]));}return Promise['resolve']()[_0xeac5('0x27')](function(){if(!_0x3f8b7c[_0xeac5('0x37')]){return _0x3f8b7c['createUserSetting']({'clientSideLoggingLevel':_0xeac5('0x38'),'clientSideLoggingEnabled':![]});}})[_0xeac5('0x27')](function(){return _0x292c0d[_0xeac5('0x39')][_0xeac5('0x3a')](_0x11c0a6);})[_0xeac5('0x27')](function(){if(_0x3f8b7c['chanspy']){return getChanSpyPromise(_0x292c0d,_0x3f8b7c);}})[_0xeac5('0x27')](function(_0x21fbec){if(_0x21fbec&&_0x21fbec[_0xeac5('0x5')]){return _0x292c0d[_0xeac5('0x39')][_0xeac5('0x3a')](_0x21fbec);}})[_0xeac5('0x27')](function(){_0x1d2070();})[_0xeac5('0x2a')](function(_0x593792){_0x1d2070(_0x593792);});},'afterUpdate':function(_0x132b88,_0x271ebf,_0x5dcff1){return BPromise[_0xeac5('0x30')]()[_0xeac5('0x27')](function(){if(_0x132b88[_0xeac5('0x36')](_0xeac5('0x3b'))||_0x132b88[_0xeac5('0x36')]('context')){return _0x292c0d[_0xeac5('0x39')][_0xeac5('0x3c')]({'where':{'UserId':_0x132b88['id'],'type':_0xeac5('0x6'),'ChanSpyId':{'$ne':null}}})[_0xeac5('0x27')](function(){if(_0x132b88[_0xeac5('0x3b')]){return getChanSpyPromise(_0x292c0d,_0x132b88)[_0xeac5('0x27')](function(_0x4ab30f){if(_0x4ab30f&&_0x4ab30f[_0xeac5('0x5')]){return _0x292c0d['VoiceExtension'][_0xeac5('0x3a')](_0x4ab30f);}});}});}})[_0xeac5('0x27')](function(){if(_0x132b88[_0xeac5('0x36')](_0xeac5('0x1e'))||_0x132b88[_0xeac5('0x36')]('context')){return _0x292c0d['VoiceExtension'][_0xeac5('0x3c')]({'where':{'UserId':_0x132b88['id'],'type':'system','ChanSpyId':null}})['then'](function(){var _0x1dae02=getUserApplications(_0x132b88,_0x132b88[_0xeac5('0x28')]);if(_0x132b88['internal']!=_0x132b88[_0xeac5('0x15')]){_0x1dae02=_0x1dae02[_0xeac5('0x29')](getUserApplications(_0x132b88,_0x132b88[_0xeac5('0x15')]));}return _0x292c0d['VoiceExtension'][_0xeac5('0x3a')](_0x1dae02);});}})['then'](function(){_0x5dcff1();})[_0xeac5('0x2a')](function(_0x31b101){_0x5dcff1(_0x31b101);});},'afterDestroy':function(_0x211328){return _0x292c0d[_0xeac5('0x39')][_0xeac5('0x3c')]({'where':{'UserId':_0x211328['id'],'type':_0xeac5('0x6')}});}},'instanceMethods':{'authenticate':function(_0x194c0a,_0x4b7630){if(!_0x4b7630){return this['password']===this[_0xeac5('0x3d')](_0x194c0a);}var _0x5dd318=this;this['encryptPassword'](_0x194c0a,function(_0x201d46,_0x25d112){if(_0x201d46){_0x4b7630(_0x201d46);}if(_0x5dd318['password']===_0x25d112){_0x4b7630(null,!![]);}else{_0x4b7630(null,![]);}});},'makeSalt':function(_0x117bd3,_0x4af9d0){var _0x2ff5a2=0x10;if(typeof arguments[0x0]===_0xeac5('0x3e')){_0x4af9d0=arguments[0x0];_0x117bd3=_0x2ff5a2;}else if(typeof arguments[0x1]===_0xeac5('0x3e')){_0x4af9d0=arguments[0x1];}else{throw new Error(_0xeac5('0x3f'));}if(!_0x117bd3){_0x117bd3=_0x2ff5a2;}return crypto[_0xeac5('0x40')](_0x117bd3,function(_0x5e1fd3,_0x51e9d1){if(_0x5e1fd3){_0x4af9d0(_0x5e1fd3);}return _0x4af9d0(null,_0x51e9d1['toString'](_0xeac5('0x41')));});},'encryptPassword':function(_0x2e1087,_0x11fc39){if(!_0x2e1087||!this[_0xeac5('0x42')]){return _0x11fc39?_0x11fc39(null):null;}var _0x41d3e4=0x2710;var _0x244482=0x40;var _0x252ce9=new Buffer(this[_0xeac5('0x42')],'base64');if(!_0x11fc39){return crypto['pbkdf2Sync'](_0x2e1087,_0x252ce9,_0x41d3e4,_0x244482,_0xeac5('0x43'))[_0xeac5('0x44')](_0xeac5('0x41'));}return crypto[_0xeac5('0x45')](_0x2e1087,_0x252ce9,_0x41d3e4,_0x244482,_0xeac5('0x43'),function(_0x4d3699,_0x2200e7){if(_0x4d3699){_0x11fc39(_0x4d3699);}return _0x11fc39(null,_0x2200e7[_0xeac5('0x44')]('base64'));});},'updatePassword':function(_0x183c2a){if(!this['password']){return _0x183c2a(null);}if(!validatePresenceOf(this[_0xeac5('0x46')])&&authTypes[_0xeac5('0x47')](this[_0xeac5('0x48')])===-0x1){_0x183c2a(new Error(_0xeac5('0x49')));}var _0x220c63=this;this[_0xeac5('0x4a')](function(_0x30f11a,_0x332d91){if(_0x30f11a){return _0x183c2a(_0x30f11a);}_0x220c63['salt']=_0x332d91;_0x220c63['encryptPassword'](_0x220c63[_0xeac5('0x46')],function(_0x10bd67,_0xb7cdbd){if(_0x10bd67){_0x183c2a(_0x10bd67);}_0x220c63['password']=_0xb7cdbd;_0x183c2a(null);});});},'updateMd5Password':function(){this[_0xeac5('0x4b')]=md5(this[_0xeac5('0x15')]+_0xeac5('0x4c')+this[_0xeac5('0x46')]);return this['md5secret'];}}});};