Built motion from commit 497d3d92.|2.5.43
[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 _0xbb3b=['User','utf8','userProfileId','resolve','find','privacyEnabled','updatePassword','changed','UserProfile','updateMd5Password','catch','internal','VoiceExtension','destroy','bulkCreate','encryptPassword','function','Missing\x20Callback','toString','base64','salt','pbkdf2Sync','pbkdf2','sha1','provider','Invalid\x20password','makeSalt','md5secret',':asterisk:','crypto','./user.attributes','google','length','context','from-sip','system','format','%s%s','prefix','merge','Set','CDR(type)=%s','chanspy','record','push','MixMonitor','${UNIQUEID}.%s,a','recordingFormat','auth','Authenticate','password','ChanSpy','SIP/%s,%s','options','join','wav','WAV','gsm','CDR(type)=internal','extensionMonitor','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=%s','Dial','SIP/%s,,tTxXU(xcally-mixmonitor-context)','name','SIP/%s,,tTxX','priority','defaults','findAll','then','concat','define'];(function(_0x53ccf2,_0x13cb67){var _0x5a2964=function(_0x3b0514){while(--_0x3b0514){_0x53ccf2['push'](_0x53ccf2['shift']());}};_0x5a2964(++_0x13cb67);}(_0xbb3b,0x185));var _0xbbb3=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xbb3b[_0x3dd15e];return _0x231fd0;};'use strict';var BPromise=require('bluebird');var crypto=require(_0xbbb3('0x0'));var util=require('util');var md5=require('md5');var _=require('lodash');var attributes=require(_0xbbb3('0x1'));var authTypes=[_0xbbb3('0x2')];var validatePresenceOf=function(_0xb04709){return _0xb04709&&_0xb04709[_0xbbb3('0x3')];};var getChanSpyApplications=function(_0x12f222,_0x14ec5b,_0x210ef4){var _0xa54d05={'context':_0x12f222[_0xbbb3('0x4')]||_0xbbb3('0x5'),'type':_0xbbb3('0x6'),'isApp':![],'UserId':_0x12f222['id']};var _0x3d4d84=[];for(var _0x42047a=0x0,_0x30bf19=0x1;_0x42047a<_0x210ef4['length'];_0x42047a++,_0x30bf19=0x1){var _0x668141={'exten':util[_0xbbb3('0x7')](_0xbbb3('0x8'),_0x210ef4[_0x42047a][_0xbbb3('0x9')],_0x14ec5b),'ChanSpyId':_0x210ef4[_0x42047a]['id']};_0x3d4d84['push'](_[_0xbbb3('0xa')]({'app':_0xbbb3('0xb'),'appdata':util[_0xbbb3('0x7')](_0xbbb3('0xc'),_0xbbb3('0xd')),'priority':_0x30bf19++},_0xa54d05,_0x668141));if(_0x210ef4[_0x42047a][_0xbbb3('0xe')]){_0x3d4d84[_0xbbb3('0xf')](_[_0xbbb3('0xa')]({'app':_0xbbb3('0x10'),'appdata':util['format'](_0xbbb3('0x11'),_0x210ef4[_0x42047a][_0xbbb3('0x12')]),'priority':_0x30bf19++},_0xa54d05,_0x668141));}if(_0x210ef4[_0x42047a][_0xbbb3('0x13')]){_0x3d4d84[_0xbbb3('0xf')](_[_0xbbb3('0xa')]({'app':_0xbbb3('0x14'),'appdata':_0x210ef4[_0x42047a][_0xbbb3('0x15')],'priority':_0x30bf19++},_0xa54d05,_0x668141));}_0x3d4d84[_0xbbb3('0xf')](_[_0xbbb3('0xa')]({'app':_0xbbb3('0x16'),'appdata':util['format'](_0xbbb3('0x17'),_0x12f222['name'],_0x210ef4[_0x42047a][_0xbbb3('0x18')]?_0x210ef4[_0x42047a]['options'][_0xbbb3('0x19')](''):''),'priority':_0x30bf19++},_0xa54d05,_0x668141));if(_0x210ef4[_0x42047a]['record']){_0x3d4d84['push'](_['merge']({'app':'StopMixMonitor','priority':_0x30bf19++},_0xa54d05,_0x668141));}}return _0x3d4d84;};var getUserApplications=function(_0x1bf0b9,_0x173c46){var _0x31ab18=[_0xbbb3('0x1a'),_0xbbb3('0x1b'),_0xbbb3('0x1c')];var _0x407278={'context':_0x1bf0b9[_0xbbb3('0x4')]||'from-sip','exten':_0x173c46,'type':_0xbbb3('0x6'),'isApp':![],'UserId':_0x1bf0b9['id']};var _0x3513bc=[{'app':'Set','appdata':_0xbbb3('0x1d')}];if(_0x1bf0b9[_0xbbb3('0x1e')]&&_['includes'](_0x31ab18,_0x1bf0b9[_0xbbb3('0x1e')])){_0x3513bc[_0xbbb3('0xf')]({'app':_0xbbb3('0xb'),'appdata':_0xbbb3('0x1f')});_0x3513bc[_0xbbb3('0xf')]({'app':_0xbbb3('0xb'),'appdata':util[_0xbbb3('0x7')](_0xbbb3('0x20'),_0x1bf0b9[_0xbbb3('0x1e')])});_0x3513bc['push']({'app':_0xbbb3('0x21'),'appdata':util[_0xbbb3('0x7')](_0xbbb3('0x22'),_0x1bf0b9[_0xbbb3('0x23')])});}else{_0x3513bc[_0xbbb3('0xf')]({'app':_0xbbb3('0x21'),'appdata':util[_0xbbb3('0x7')](_0xbbb3('0x24'),_0x1bf0b9[_0xbbb3('0x23')])});}_0x3513bc[_0xbbb3('0xf')]({'app':'Hangup'});for(var _0x285ae6=0x0;_0x285ae6<_0x3513bc[_0xbbb3('0x3')];_0x285ae6++){_0x3513bc[_0x285ae6][_0xbbb3('0x25')]=_0x285ae6+0x1;_0x3513bc[_0x285ae6]=_[_0xbbb3('0x26')](_0x3513bc[_0x285ae6],_0x407278);}return _0x3513bc;};var getChanSpyPromise=function(_0x3d8bef,_0xa315f4){return new BPromise(function(_0x276199,_0x132cf7){return _0x3d8bef['VoiceChanSpy'][_0xbbb3('0x27')]()[_0xbbb3('0x28')](function(_0x132649){if(_0x132649){var _0x34c3fd=getChanSpyApplications(_0xa315f4,_0xa315f4['internal'],_0x132649);if(_0xa315f4['internal']!=_0xa315f4[_0xbbb3('0x23')]){_0x34c3fd=_0x34c3fd[_0xbbb3('0x29')](getChanSpyApplications(_0xa315f4,_0xa315f4[_0xbbb3('0x23')],_0x132649));}return _0x276199(_0x34c3fd);}return _0x276199([]);})['catch'](function(_0x37e13b){_0x132cf7(_0x37e13b);});});};module['exports']=function(_0x124016,_0x4d0a66){var _0x582589=_0x124016['models'];return _0x124016[_0xbbb3('0x2a')](_0xbbb3('0x2b'),attributes,{'tableName':'users','charset':_0xbbb3('0x2c'),'collate':'utf8_bin','hooks':{'beforeCreate':function(_0x583395,_0x4e4aca,_0x1b0f0d){var _0x1247a3=typeof _0x583395[_0xbbb3('0x2d')]==='undefined'?BPromise[_0xbbb3('0x2e')](null):_0x582589['UserProfile'][_0xbbb3('0x2f')]({'where':{'id':_0x583395[_0xbbb3('0x2d')]}});_0x1247a3[_0xbbb3('0x28')](function(_0x3d78cb){if(_0x3d78cb){_0x583395[_0xbbb3('0x30')]=_0x3d78cb[_0xbbb3('0x30')];}})['then'](function(){_0x583395['updateMd5Password']();_0x583395[_0xbbb3('0x31')](_0x1b0f0d);})['catch'](function(_0x288ab8){_0x1b0f0d(_0x288ab8);});},'beforeUpdate':function(_0x144ef7,_0x7386ea,_0x716894){Promise[_0xbbb3('0x2e')]()[_0xbbb3('0x28')](function(){if(_0x144ef7[_0xbbb3('0x32')](_0xbbb3('0x2d'))===![])return;return _0x582589[_0xbbb3('0x33')][_0xbbb3('0x2f')]({'where':{'id':_0x144ef7['userProfileId']}})[_0xbbb3('0x28')](function(_0x1ed953){if(_0x1ed953)_0x144ef7[_0xbbb3('0x30')]=_0x1ed953[_0xbbb3('0x30')];return;});})[_0xbbb3('0x28')](function(){if(_0x144ef7[_0xbbb3('0x32')]('password')){_0x144ef7[_0xbbb3('0x34')]();return _0x144ef7[_0xbbb3('0x31')](_0x716894);}_0x716894();})[_0xbbb3('0x35')](function(_0xb5700d){_0x716894(_0xb5700d);});},'afterCreate':function(_0x3c60eb,_0x518cb6,_0x1e8394){var _0x1e88bb=getUserApplications(_0x3c60eb,_0x3c60eb[_0xbbb3('0x36')]);if(_0x3c60eb[_0xbbb3('0x36')]!=_0x3c60eb[_0xbbb3('0x23')]){_0x1e88bb=_0x1e88bb['concat'](getUserApplications(_0x3c60eb,_0x3c60eb['name']));}return _0x582589['VoiceExtension']['bulkCreate'](_0x1e88bb)['then'](function(){if(_0x3c60eb[_0xbbb3('0xd')]){return getChanSpyPromise(_0x582589,_0x3c60eb);}})[_0xbbb3('0x28')](function(_0x52c4ef){if(_0x52c4ef&&_0x52c4ef[_0xbbb3('0x3')]){return _0x582589[_0xbbb3('0x37')]['bulkCreate'](_0x52c4ef);}})[_0xbbb3('0x28')](function(){_0x1e8394();})[_0xbbb3('0x35')](function(_0x6a9929){_0x1e8394(_0x6a9929);});},'afterUpdate':function(_0x1d297e,_0x4acf66,_0xd327b6){return BPromise[_0xbbb3('0x2e')]()[_0xbbb3('0x28')](function(){if(_0x1d297e[_0xbbb3('0x32')](_0xbbb3('0xd'))||_0x1d297e[_0xbbb3('0x32')]('context')){return _0x582589[_0xbbb3('0x37')]['destroy']({'where':{'UserId':_0x1d297e['id'],'type':_0xbbb3('0x6'),'ChanSpyId':{'$ne':null}}})[_0xbbb3('0x28')](function(){if(_0x1d297e['chanspy']){return getChanSpyPromise(_0x582589,_0x1d297e)['then'](function(_0x4df62b){if(_0x4df62b&&_0x4df62b[_0xbbb3('0x3')]){return _0x582589[_0xbbb3('0x37')]['bulkCreate'](_0x4df62b);}});}});}})['then'](function(){if(_0x1d297e[_0xbbb3('0x32')](_0xbbb3('0x1e'))||_0x1d297e[_0xbbb3('0x32')](_0xbbb3('0x4'))){return _0x582589['VoiceExtension'][_0xbbb3('0x38')]({'where':{'UserId':_0x1d297e['id'],'type':_0xbbb3('0x6'),'ChanSpyId':null}})['then'](function(){var _0x2622a0=getUserApplications(_0x1d297e,_0x1d297e[_0xbbb3('0x36')]);if(_0x1d297e[_0xbbb3('0x36')]!=_0x1d297e[_0xbbb3('0x23')]){_0x2622a0=_0x2622a0[_0xbbb3('0x29')](getUserApplications(_0x1d297e,_0x1d297e[_0xbbb3('0x23')]));}return _0x582589[_0xbbb3('0x37')][_0xbbb3('0x39')](_0x2622a0);});}})['then'](function(){_0xd327b6();})[_0xbbb3('0x35')](function(_0x46ac05){_0xd327b6(_0x46ac05);});},'afterDestroy':function(_0x478404,_0x38b274){return _0x582589[_0xbbb3('0x37')][_0xbbb3('0x38')]({'where':{'UserId':_0x478404['id'],'type':_0xbbb3('0x6')}});}},'instanceMethods':{'authenticate':function(_0x3e5f9b,_0x36a06a){if(!_0x36a06a){return this['password']===this[_0xbbb3('0x3a')](_0x3e5f9b);}var _0x4d636c=this;this[_0xbbb3('0x3a')](_0x3e5f9b,function(_0x1c23cd,_0x39ec1f){if(_0x1c23cd){_0x36a06a(_0x1c23cd);}if(_0x4d636c[_0xbbb3('0x15')]===_0x39ec1f){_0x36a06a(null,!![]);}else{_0x36a06a(null,![]);}});},'makeSalt':function(_0x5ee988,_0x3ae601){var _0x3c991e=0x10;if(typeof arguments[0x0]===_0xbbb3('0x3b')){_0x3ae601=arguments[0x0];_0x5ee988=_0x3c991e;}else if(typeof arguments[0x1]===_0xbbb3('0x3b')){_0x3ae601=arguments[0x1];}else{throw new Error(_0xbbb3('0x3c'));}if(!_0x5ee988){_0x5ee988=_0x3c991e;}return crypto['randomBytes'](_0x5ee988,function(_0x34716e,_0x44f523){if(_0x34716e){_0x3ae601(_0x34716e);}return _0x3ae601(null,_0x44f523[_0xbbb3('0x3d')](_0xbbb3('0x3e')));});},'encryptPassword':function(_0x14e60c,_0x3abbf6){if(!_0x14e60c||!this['salt']){return _0x3abbf6?_0x3abbf6(null):null;}var _0x2913be=0x2710;var _0x371484=0x40;var _0x49edca=new Buffer(this[_0xbbb3('0x3f')],_0xbbb3('0x3e'));if(!_0x3abbf6){return crypto[_0xbbb3('0x40')](_0x14e60c,_0x49edca,_0x2913be,_0x371484,'sha1')[_0xbbb3('0x3d')](_0xbbb3('0x3e'));}return crypto[_0xbbb3('0x41')](_0x14e60c,_0x49edca,_0x2913be,_0x371484,_0xbbb3('0x42'),function(_0x5e9674,_0x3eaca4){if(_0x5e9674){_0x3abbf6(_0x5e9674);}return _0x3abbf6(null,_0x3eaca4[_0xbbb3('0x3d')](_0xbbb3('0x3e')));});},'updatePassword':function(_0x17366e){if(!this[_0xbbb3('0x15')]){return _0x17366e(null);}if(!validatePresenceOf(this['password'])&&authTypes['indexOf'](this[_0xbbb3('0x43')])===-0x1){_0x17366e(new Error(_0xbbb3('0x44')));}var _0x149e61=this;this[_0xbbb3('0x45')](function(_0x33b6fc,_0x21a645){if(_0x33b6fc){return _0x17366e(_0x33b6fc);}_0x149e61[_0xbbb3('0x3f')]=_0x21a645;_0x149e61['encryptPassword'](_0x149e61[_0xbbb3('0x15')],function(_0xb501ab,_0x11b83c){if(_0xb501ab){_0x17366e(_0xb501ab);}_0x149e61[_0xbbb3('0x15')]=_0x11b83c;_0x17366e(null);});});},'updateMd5Password':function(){this[_0xbbb3('0x46')]=md5(this[_0xbbb3('0x23')]+_0xbbb3('0x47')+this[_0xbbb3('0x15')]);return this[_0xbbb3('0x46')];}}});};