Built motion from commit 5b761a7b.|2.5.36
[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 _0xc5d3=['privacyEnabled','changed','find','bulkCreate','VoiceExtension','destroy','system','function','Missing\x20Callback','randomBytes','toString','base64','salt','pbkdf2Sync','sha1','indexOf','Invalid\x20password','encryptPassword','md5secret','bluebird','crypto','util','md5','lodash','google','from-sip','length','format','push','merge','CDR(type)=%s','chanspy','record','Authenticate','password','ChanSpy','SIP/%s,%s','options','join','StopMixMonitor','wav','gsm','context','Set','CDR(type)=internal','extensionMonitor','includes','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=%s','SIP/%s,,tTxXU(xcally-mixmonitor-context)','Dial','SIP/%s,,tTxX','priority','defaults','findAll','then','internal','name','catch','exports','models','User','users','utf8','undefined','resolve','UserProfile','userProfileId'];(function(_0x2711ec,_0x264584){var _0x468e1c=function(_0x16cb28){while(--_0x16cb28){_0x2711ec['push'](_0x2711ec['shift']());}};_0x468e1c(++_0x264584);}(_0xc5d3,0x123));var _0x3c5d=function(_0x5e2751,_0x518a55){_0x5e2751=_0x5e2751-0x0;var _0x139a39=_0xc5d3[_0x5e2751];return _0x139a39;};'use strict';var BPromise=require(_0x3c5d('0x0'));var crypto=require(_0x3c5d('0x1'));var util=require(_0x3c5d('0x2'));var md5=require(_0x3c5d('0x3'));var _=require(_0x3c5d('0x4'));var attributes=require('./user.attributes');var authTypes=[_0x3c5d('0x5')];var validatePresenceOf=function(_0x1e1b08){return _0x1e1b08&&_0x1e1b08['length'];};var getChanSpyApplications=function(_0x302a91,_0x23b0b1,_0x3835a0){var _0x23c73a={'context':_0x302a91['context']||_0x3c5d('0x6'),'type':'system','isApp':![],'UserId':_0x302a91['id']};var _0xb5a1c1=[];for(var _0x1b56bf=0x0,_0x247305=0x1;_0x1b56bf<_0x3835a0[_0x3c5d('0x7')];_0x1b56bf++,_0x247305=0x1){var _0x52ad71={'exten':util[_0x3c5d('0x8')]('%s%s',_0x3835a0[_0x1b56bf]['prefix'],_0x23b0b1),'ChanSpyId':_0x3835a0[_0x1b56bf]['id']};_0xb5a1c1[_0x3c5d('0x9')](_[_0x3c5d('0xa')]({'app':'Set','appdata':util['format'](_0x3c5d('0xb'),_0x3c5d('0xc')),'priority':_0x247305++},_0x23c73a,_0x52ad71));if(_0x3835a0[_0x1b56bf][_0x3c5d('0xd')]){_0xb5a1c1[_0x3c5d('0x9')](_[_0x3c5d('0xa')]({'app':'MixMonitor','appdata':util[_0x3c5d('0x8')]('${UNIQUEID}.%s,a',_0x3835a0[_0x1b56bf]['recordingFormat']),'priority':_0x247305++},_0x23c73a,_0x52ad71));}if(_0x3835a0[_0x1b56bf]['auth']){_0xb5a1c1[_0x3c5d('0x9')](_[_0x3c5d('0xa')]({'app':_0x3c5d('0xe'),'appdata':_0x3835a0[_0x1b56bf][_0x3c5d('0xf')],'priority':_0x247305++},_0x23c73a,_0x52ad71));}_0xb5a1c1['push'](_[_0x3c5d('0xa')]({'app':_0x3c5d('0x10'),'appdata':util[_0x3c5d('0x8')](_0x3c5d('0x11'),_0x302a91['name'],_0x3835a0[_0x1b56bf][_0x3c5d('0x12')]?_0x3835a0[_0x1b56bf][_0x3c5d('0x12')][_0x3c5d('0x13')](''):''),'priority':_0x247305++},_0x23c73a,_0x52ad71));if(_0x3835a0[_0x1b56bf][_0x3c5d('0xd')]){_0xb5a1c1[_0x3c5d('0x9')](_['merge']({'app':_0x3c5d('0x14'),'priority':_0x247305++},_0x23c73a,_0x52ad71));}}return _0xb5a1c1;};var getUserApplications=function(_0x425e75,_0x23d335){var _0x580696=[_0x3c5d('0x15'),'WAV',_0x3c5d('0x16')];var _0x44e998={'context':_0x425e75[_0x3c5d('0x17')]||_0x3c5d('0x6'),'exten':_0x23d335,'type':'system','isApp':![],'UserId':_0x425e75['id']};var _0x3700cf=[{'app':_0x3c5d('0x18'),'appdata':_0x3c5d('0x19')}];if(_0x425e75[_0x3c5d('0x1a')]&&_[_0x3c5d('0x1b')](_0x580696,_0x425e75[_0x3c5d('0x1a')])){_0x3700cf[_0x3c5d('0x9')]({'app':_0x3c5d('0x18'),'appdata':_0x3c5d('0x1c')});_0x3700cf[_0x3c5d('0x9')]({'app':_0x3c5d('0x18'),'appdata':util[_0x3c5d('0x8')](_0x3c5d('0x1d'),_0x425e75[_0x3c5d('0x1a')])});_0x3700cf[_0x3c5d('0x9')]({'app':'Dial','appdata':util[_0x3c5d('0x8')](_0x3c5d('0x1e'),_0x425e75['name'])});}else{_0x3700cf['push']({'app':_0x3c5d('0x1f'),'appdata':util['format'](_0x3c5d('0x20'),_0x425e75['name'])});}_0x3700cf[_0x3c5d('0x9')]({'app':'Hangup'});for(var _0x51eadb=0x0;_0x51eadb<_0x3700cf[_0x3c5d('0x7')];_0x51eadb++){_0x3700cf[_0x51eadb][_0x3c5d('0x21')]=_0x51eadb+0x1;_0x3700cf[_0x51eadb]=_[_0x3c5d('0x22')](_0x3700cf[_0x51eadb],_0x44e998);}return _0x3700cf;};var getChanSpyPromise=function(_0x530035,_0x1879d2){return new BPromise(function(_0x272b24,_0x5924ca){return _0x530035['VoiceChanSpy'][_0x3c5d('0x23')]()[_0x3c5d('0x24')](function(_0x4cf0a8){if(_0x4cf0a8){var _0x4a7980=getChanSpyApplications(_0x1879d2,_0x1879d2[_0x3c5d('0x25')],_0x4cf0a8);if(_0x1879d2['internal']!=_0x1879d2['name']){_0x4a7980=_0x4a7980['concat'](getChanSpyApplications(_0x1879d2,_0x1879d2[_0x3c5d('0x26')],_0x4cf0a8));}return _0x272b24(_0x4a7980);}return _0x272b24([]);})[_0x3c5d('0x27')](function(_0x5942b3){_0x5924ca(_0x5942b3);});});};module[_0x3c5d('0x28')]=function(_0x182d77,_0x314af8){var _0x2f16f6=_0x182d77[_0x3c5d('0x29')];return _0x182d77['define'](_0x3c5d('0x2a'),attributes,{'tableName':_0x3c5d('0x2b'),'charset':_0x3c5d('0x2c'),'collate':'utf8_bin','hooks':{'beforeCreate':function(_0x298597,_0x5d33e2,_0xe7df03){var _0x2819a4=typeof _0x298597['userProfileId']===_0x3c5d('0x2d')?BPromise[_0x3c5d('0x2e')](null):_0x2f16f6[_0x3c5d('0x2f')]['find']({'where':{'id':_0x298597[_0x3c5d('0x30')]}});_0x2819a4[_0x3c5d('0x24')](function(_0x3adeda){if(_0x3adeda){_0x298597[_0x3c5d('0x31')]=_0x3adeda[_0x3c5d('0x31')];}})[_0x3c5d('0x24')](function(){_0x298597['updateMd5Password']();_0x298597['updatePassword'](_0xe7df03);})[_0x3c5d('0x27')](function(_0x15334f){_0xe7df03(_0x15334f);});},'beforeUpdate':function(_0x512292,_0x364453,_0x17359d){Promise['resolve']()['then'](function(){if(_0x512292[_0x3c5d('0x32')](_0x3c5d('0x30'))===![])return;return _0x2f16f6['UserProfile'][_0x3c5d('0x33')]({'where':{'id':_0x512292[_0x3c5d('0x30')]}})[_0x3c5d('0x24')](function(_0x1c8cf7){if(_0x1c8cf7)_0x512292['privacyEnabled']=_0x1c8cf7['privacyEnabled'];return;});})['then'](function(){if(_0x512292[_0x3c5d('0x32')](_0x3c5d('0xf'))){_0x512292['updateMd5Password']();return _0x512292['updatePassword'](_0x17359d);}_0x17359d();})[_0x3c5d('0x27')](function(_0x58aa70){_0x17359d(_0x58aa70);});},'afterCreate':function(_0x3d4fe2,_0x5348e9,_0x5d8178){var _0x114e13=getUserApplications(_0x3d4fe2,_0x3d4fe2[_0x3c5d('0x25')]);if(_0x3d4fe2[_0x3c5d('0x25')]!=_0x3d4fe2[_0x3c5d('0x26')]){_0x114e13=_0x114e13['concat'](getUserApplications(_0x3d4fe2,_0x3d4fe2[_0x3c5d('0x26')]));}return _0x2f16f6['VoiceExtension'][_0x3c5d('0x34')](_0x114e13)[_0x3c5d('0x24')](function(){if(_0x3d4fe2[_0x3c5d('0xc')]){return getChanSpyPromise(_0x2f16f6,_0x3d4fe2);}})[_0x3c5d('0x24')](function(_0x5e4a7f){if(_0x5e4a7f&&_0x5e4a7f[_0x3c5d('0x7')]){return _0x2f16f6['VoiceExtension'][_0x3c5d('0x34')](_0x5e4a7f);}})[_0x3c5d('0x24')](function(){_0x5d8178();})['catch'](function(_0x426488){_0x5d8178(_0x426488);});},'afterUpdate':function(_0x1fcf50,_0x4dc525,_0x4ac000){return BPromise['resolve']()[_0x3c5d('0x24')](function(){if(_0x1fcf50[_0x3c5d('0x32')](_0x3c5d('0xc'))||_0x1fcf50[_0x3c5d('0x32')](_0x3c5d('0x17'))){return _0x2f16f6[_0x3c5d('0x35')][_0x3c5d('0x36')]({'where':{'UserId':_0x1fcf50['id'],'type':_0x3c5d('0x37'),'ChanSpyId':{'$ne':null}}})[_0x3c5d('0x24')](function(){if(_0x1fcf50[_0x3c5d('0xc')]){return getChanSpyPromise(_0x2f16f6,_0x1fcf50)[_0x3c5d('0x24')](function(_0x25c194){if(_0x25c194&&_0x25c194[_0x3c5d('0x7')]){return _0x2f16f6[_0x3c5d('0x35')][_0x3c5d('0x34')](_0x25c194);}});}});}})[_0x3c5d('0x24')](function(){if(_0x1fcf50['changed'](_0x3c5d('0x1a'))||_0x1fcf50['changed'](_0x3c5d('0x17'))){return _0x2f16f6[_0x3c5d('0x35')][_0x3c5d('0x36')]({'where':{'UserId':_0x1fcf50['id'],'type':_0x3c5d('0x37'),'ChanSpyId':null}})[_0x3c5d('0x24')](function(){var _0x2e4d6d=getUserApplications(_0x1fcf50,_0x1fcf50[_0x3c5d('0x25')]);if(_0x1fcf50[_0x3c5d('0x25')]!=_0x1fcf50[_0x3c5d('0x26')]){_0x2e4d6d=_0x2e4d6d['concat'](getUserApplications(_0x1fcf50,_0x1fcf50[_0x3c5d('0x26')]));}return _0x2f16f6['VoiceExtension']['bulkCreate'](_0x2e4d6d);});}})['then'](function(){_0x4ac000();})[_0x3c5d('0x27')](function(_0x4730a6){_0x4ac000(_0x4730a6);});},'afterDestroy':function(_0x248b14,_0x173845){return _0x2f16f6['VoiceExtension'][_0x3c5d('0x36')]({'where':{'UserId':_0x248b14['id'],'type':_0x3c5d('0x37')}});}},'instanceMethods':{'authenticate':function(_0x3cab07,_0x2203a5){if(!_0x2203a5){return this[_0x3c5d('0xf')]===this['encryptPassword'](_0x3cab07);}var _0x5c3529=this;this['encryptPassword'](_0x3cab07,function(_0x58d808,_0x5c19a0){if(_0x58d808){_0x2203a5(_0x58d808);}if(_0x5c3529[_0x3c5d('0xf')]===_0x5c19a0){_0x2203a5(null,!![]);}else{_0x2203a5(null,![]);}});},'makeSalt':function(_0x4c5808,_0x15eac7){var _0x9a39ef=0x10;if(typeof arguments[0x0]===_0x3c5d('0x38')){_0x15eac7=arguments[0x0];_0x4c5808=_0x9a39ef;}else if(typeof arguments[0x1]===_0x3c5d('0x38')){_0x15eac7=arguments[0x1];}else{throw new Error(_0x3c5d('0x39'));}if(!_0x4c5808){_0x4c5808=_0x9a39ef;}return crypto[_0x3c5d('0x3a')](_0x4c5808,function(_0x17000f,_0x5e5d74){if(_0x17000f){_0x15eac7(_0x17000f);}return _0x15eac7(null,_0x5e5d74[_0x3c5d('0x3b')](_0x3c5d('0x3c')));});},'encryptPassword':function(_0x439e4d,_0x350055){if(!_0x439e4d||!this[_0x3c5d('0x3d')]){return _0x350055?_0x350055(null):null;}var _0x3411f6=0x2710;var _0x2bd84b=0x40;var _0x5859fb=new Buffer(this[_0x3c5d('0x3d')],_0x3c5d('0x3c'));if(!_0x350055){return crypto[_0x3c5d('0x3e')](_0x439e4d,_0x5859fb,_0x3411f6,_0x2bd84b,_0x3c5d('0x3f'))[_0x3c5d('0x3b')](_0x3c5d('0x3c'));}return crypto['pbkdf2'](_0x439e4d,_0x5859fb,_0x3411f6,_0x2bd84b,_0x3c5d('0x3f'),function(_0x2f6c59,_0x3f3863){if(_0x2f6c59){_0x350055(_0x2f6c59);}return _0x350055(null,_0x3f3863['toString'](_0x3c5d('0x3c')));});},'updatePassword':function(_0x44dc98){if(!this[_0x3c5d('0xf')]){return _0x44dc98(null);}if(!validatePresenceOf(this['password'])&&authTypes[_0x3c5d('0x40')](this['provider'])===-0x1){_0x44dc98(new Error(_0x3c5d('0x41')));}var _0x5044bd=this;this['makeSalt'](function(_0x16d484,_0x8bb232){if(_0x16d484){return _0x44dc98(_0x16d484);}_0x5044bd[_0x3c5d('0x3d')]=_0x8bb232;_0x5044bd[_0x3c5d('0x42')](_0x5044bd[_0x3c5d('0xf')],function(_0x4e1b31,_0x64cc8c){if(_0x4e1b31){_0x44dc98(_0x4e1b31);}_0x5044bd[_0x3c5d('0xf')]=_0x64cc8c;_0x44dc98(null);});});},'updateMd5Password':function(){this[_0x3c5d('0x43')]=md5(this[_0x3c5d('0x26')]+':asterisk:'+this[_0x3c5d('0xf')]);return this[_0x3c5d('0x43')];}}});};