Built motion from commit (unavailable).|2.5.4
[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 _0xef3e=['User','utf8','utf8_bin','updateMd5Password','changed','updatePassword','VoiceExtension','bulkCreate','resolve','destroy','encryptPassword','function','Missing\x20Callback','base64','pbkdf2Sync','sha1','toString','pbkdf2','Invalid\x20password','makeSalt','md5secret',':asterisk:','bluebird','crypto','util','md5','lodash','length','context','from-sip','system','push','merge','Set','format','CDR(type)=%s','chanspy','record','MixMonitor','${UNIQUEID}.%s,a','recordingFormat','auth','Authenticate','password','ChanSpy','SIP/%s,%s','name','options','join','StopMixMonitor','wav','includes','_XCALLY_MIXMONITOR_FORMAT=%s','extensionMonitor','Dial','SIP/%s,,tTxXU(xcally-mixmonitor-context)','SIP/%s,,tTxX','Hangup','priority','defaults','VoiceChanSpy','findAll','then','internal','concat','catch','exports','models','define'];(function(_0x3e6aed,_0x97a2f1){var _0x3220dd=function(_0x252cd2){while(--_0x252cd2){_0x3e6aed['push'](_0x3e6aed['shift']());}};_0x3220dd(++_0x97a2f1);}(_0xef3e,0xa0));var _0xeef3=function(_0x3511d3,_0x4107ae){_0x3511d3=_0x3511d3-0x0;var _0x5d6321=_0xef3e[_0x3511d3];return _0x5d6321;};'use strict';var BPromise=require(_0xeef3('0x0'));var crypto=require(_0xeef3('0x1'));var util=require(_0xeef3('0x2'));var md5=require(_0xeef3('0x3'));var _=require(_0xeef3('0x4'));var attributes=require('./user.attributes');var authTypes=['google'];var validatePresenceOf=function(_0x3748d1){return _0x3748d1&&_0x3748d1[_0xeef3('0x5')];};var getChanSpyApplications=function(_0x5772eb,_0x3d4e5b,_0x11535e){var _0x5aab7a={'context':_0x5772eb[_0xeef3('0x6')]||_0xeef3('0x7'),'type':_0xeef3('0x8'),'isApp':![],'UserId':_0x5772eb['id']};var _0x5f1a49=[];for(var _0x3f734c=0x0,_0x29a4be=0x1;_0x3f734c<_0x11535e[_0xeef3('0x5')];_0x3f734c++,_0x29a4be=0x1){var _0x5ed62b={'exten':util['format']('%s%s',_0x11535e[_0x3f734c]['prefix'],_0x3d4e5b),'ChanSpyId':_0x11535e[_0x3f734c]['id']};_0x5f1a49[_0xeef3('0x9')](_[_0xeef3('0xa')]({'app':_0xeef3('0xb'),'appdata':util[_0xeef3('0xc')](_0xeef3('0xd'),_0xeef3('0xe')),'priority':_0x29a4be++},_0x5aab7a,_0x5ed62b));if(_0x11535e[_0x3f734c][_0xeef3('0xf')]){_0x5f1a49[_0xeef3('0x9')](_['merge']({'app':_0xeef3('0x10'),'appdata':util['format'](_0xeef3('0x11'),_0x11535e[_0x3f734c][_0xeef3('0x12')]),'priority':_0x29a4be++},_0x5aab7a,_0x5ed62b));}if(_0x11535e[_0x3f734c][_0xeef3('0x13')]){_0x5f1a49[_0xeef3('0x9')](_['merge']({'app':_0xeef3('0x14'),'appdata':_0x11535e[_0x3f734c][_0xeef3('0x15')],'priority':_0x29a4be++},_0x5aab7a,_0x5ed62b));}_0x5f1a49['push'](_[_0xeef3('0xa')]({'app':_0xeef3('0x16'),'appdata':util[_0xeef3('0xc')](_0xeef3('0x17'),_0x5772eb[_0xeef3('0x18')],_0x11535e[_0x3f734c][_0xeef3('0x19')]?_0x11535e[_0x3f734c][_0xeef3('0x19')][_0xeef3('0x1a')](''):''),'priority':_0x29a4be++},_0x5aab7a,_0x5ed62b));if(_0x11535e[_0x3f734c][_0xeef3('0xf')]){_0x5f1a49[_0xeef3('0x9')](_[_0xeef3('0xa')]({'app':_0xeef3('0x1b'),'priority':_0x29a4be++},_0x5aab7a,_0x5ed62b));}}return _0x5f1a49;};var getUserApplications=function(_0x26927d,_0x4fb532){var _0x517b87=[_0xeef3('0x1c'),'WAV','gsm'];var _0x2f95e0={'context':_0x26927d[_0xeef3('0x6')]||_0xeef3('0x7'),'exten':_0x4fb532,'type':'system','isApp':![],'UserId':_0x26927d['id']};var _0x38cdcd=[{'app':_0xeef3('0xb'),'appdata':'CDR(type)=internal'}];if(_0x26927d['extensionMonitor']&&_[_0xeef3('0x1d')](_0x517b87,_0x26927d['extensionMonitor'])){_0x38cdcd[_0xeef3('0x9')]({'app':_0xeef3('0xb'),'appdata':'_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}'});_0x38cdcd[_0xeef3('0x9')]({'app':_0xeef3('0xb'),'appdata':util[_0xeef3('0xc')](_0xeef3('0x1e'),_0x26927d[_0xeef3('0x1f')])});_0x38cdcd[_0xeef3('0x9')]({'app':_0xeef3('0x20'),'appdata':util[_0xeef3('0xc')](_0xeef3('0x21'),_0x26927d[_0xeef3('0x18')])});}else{_0x38cdcd['push']({'app':_0xeef3('0x20'),'appdata':util[_0xeef3('0xc')](_0xeef3('0x22'),_0x26927d[_0xeef3('0x18')])});}_0x38cdcd[_0xeef3('0x9')]({'app':_0xeef3('0x23')});for(var _0x4b5f0d=0x0;_0x4b5f0d<_0x38cdcd['length'];_0x4b5f0d++){_0x38cdcd[_0x4b5f0d][_0xeef3('0x24')]=_0x4b5f0d+0x1;_0x38cdcd[_0x4b5f0d]=_[_0xeef3('0x25')](_0x38cdcd[_0x4b5f0d],_0x2f95e0);}return _0x38cdcd;};var getChanSpyPromise=function(_0x5975ea,_0x579fe2){return new BPromise(function(_0x35271b,_0x2c929b){return _0x5975ea[_0xeef3('0x26')][_0xeef3('0x27')]()[_0xeef3('0x28')](function(_0x51b1b8){if(_0x51b1b8){var _0x2f45c2=getChanSpyApplications(_0x579fe2,_0x579fe2[_0xeef3('0x29')],_0x51b1b8);if(_0x579fe2['internal']!=_0x579fe2[_0xeef3('0x18')]){_0x2f45c2=_0x2f45c2[_0xeef3('0x2a')](getChanSpyApplications(_0x579fe2,_0x579fe2[_0xeef3('0x18')],_0x51b1b8));}return _0x35271b(_0x2f45c2);}return _0x35271b([]);})[_0xeef3('0x2b')](function(_0x35f5a8){_0x2c929b(_0x35f5a8);});});};module[_0xeef3('0x2c')]=function(_0x534e4a,_0x140140){var _0x51025c=_0x534e4a[_0xeef3('0x2d')];return _0x534e4a[_0xeef3('0x2e')](_0xeef3('0x2f'),attributes,{'tableName':'users','charset':_0xeef3('0x30'),'collate':_0xeef3('0x31'),'hooks':{'beforeCreate':function(_0x802ae2,_0x2dafb2,_0x13c9de){_0x802ae2[_0xeef3('0x32')]();_0x802ae2['updatePassword'](_0x13c9de);},'beforeUpdate':function(_0x13c3c6,_0x5dd706,_0x1ffac3){if(_0x13c3c6[_0xeef3('0x33')]('password')){_0x13c3c6[_0xeef3('0x32')]();return _0x13c3c6[_0xeef3('0x34')](_0x1ffac3);}_0x1ffac3();},'afterCreate':function(_0x560bca,_0x17b99d,_0x65fe8d){var _0x32dce9=getUserApplications(_0x560bca,_0x560bca['internal']);if(_0x560bca[_0xeef3('0x29')]!=_0x560bca[_0xeef3('0x18')]){_0x32dce9=_0x32dce9['concat'](getUserApplications(_0x560bca,_0x560bca['name']));}return _0x51025c[_0xeef3('0x35')][_0xeef3('0x36')](_0x32dce9)[_0xeef3('0x28')](function(){if(_0x560bca['chanspy']){return getChanSpyPromise(_0x51025c,_0x560bca);}})[_0xeef3('0x28')](function(_0x5b1909){if(_0x5b1909&&_0x5b1909['length']){return _0x51025c[_0xeef3('0x35')][_0xeef3('0x36')](_0x5b1909);}})[_0xeef3('0x28')](function(){_0x65fe8d();})[_0xeef3('0x2b')](function(_0x30ed92){_0x65fe8d(_0x30ed92);});},'afterUpdate':function(_0x3d68f1,_0x7178ee,_0xabc7e4){return BPromise[_0xeef3('0x37')]()[_0xeef3('0x28')](function(){if(_0x3d68f1['changed'](_0xeef3('0xe'))||_0x3d68f1[_0xeef3('0x33')](_0xeef3('0x6'))){return _0x51025c[_0xeef3('0x35')][_0xeef3('0x38')]({'where':{'UserId':_0x3d68f1['id'],'type':_0xeef3('0x8'),'ChanSpyId':{'$ne':null}}})[_0xeef3('0x28')](function(){if(_0x3d68f1['chanspy']){return getChanSpyPromise(_0x51025c,_0x3d68f1)[_0xeef3('0x28')](function(_0x26a4da){if(_0x26a4da&&_0x26a4da[_0xeef3('0x5')]){return _0x51025c['VoiceExtension'][_0xeef3('0x36')](_0x26a4da);}});}});}})[_0xeef3('0x28')](function(){if(_0x3d68f1[_0xeef3('0x33')](_0xeef3('0x1f'))||_0x3d68f1[_0xeef3('0x33')](_0xeef3('0x6'))){return _0x51025c[_0xeef3('0x35')][_0xeef3('0x38')]({'where':{'UserId':_0x3d68f1['id'],'type':_0xeef3('0x8'),'ChanSpyId':null}})['then'](function(){var _0x53b530=getUserApplications(_0x3d68f1,_0x3d68f1['internal']);if(_0x3d68f1[_0xeef3('0x29')]!=_0x3d68f1[_0xeef3('0x18')]){_0x53b530=_0x53b530['concat'](getUserApplications(_0x3d68f1,_0x3d68f1['name']));}return _0x51025c[_0xeef3('0x35')][_0xeef3('0x36')](_0x53b530);});}})[_0xeef3('0x28')](function(){_0xabc7e4();})[_0xeef3('0x2b')](function(_0xd3dda9){_0xabc7e4(_0xd3dda9);});},'afterDestroy':function(_0x369064,_0x5f431d){return _0x51025c[_0xeef3('0x35')]['destroy']({'where':{'UserId':_0x369064['id'],'type':_0xeef3('0x8')}});}},'instanceMethods':{'authenticate':function(_0x59d3b0,_0x5e3ffb){if(!_0x5e3ffb){return this[_0xeef3('0x15')]===this[_0xeef3('0x39')](_0x59d3b0);}var _0x377f44=this;this[_0xeef3('0x39')](_0x59d3b0,function(_0x5cfa4d,_0x11bc70){if(_0x5cfa4d){_0x5e3ffb(_0x5cfa4d);}if(_0x377f44[_0xeef3('0x15')]===_0x11bc70){_0x5e3ffb(null,!![]);}else{_0x5e3ffb(null,![]);}});},'makeSalt':function(_0x2ba04e,_0x117075){var _0x925da0=0x10;if(typeof arguments[0x0]===_0xeef3('0x3a')){_0x117075=arguments[0x0];_0x2ba04e=_0x925da0;}else if(typeof arguments[0x1]===_0xeef3('0x3a')){_0x117075=arguments[0x1];}else{throw new Error(_0xeef3('0x3b'));}if(!_0x2ba04e){_0x2ba04e=_0x925da0;}return crypto['randomBytes'](_0x2ba04e,function(_0x152d44,_0x5e260c){if(_0x152d44){_0x117075(_0x152d44);}return _0x117075(null,_0x5e260c['toString'](_0xeef3('0x3c')));});},'encryptPassword':function(_0x6cc222,_0x37ded9){if(!_0x6cc222||!this['salt']){return _0x37ded9?_0x37ded9(null):null;}var _0x398284=0x2710;var _0x1d92a5=0x40;var _0x4cd0ea=new Buffer(this['salt'],_0xeef3('0x3c'));if(!_0x37ded9){return crypto[_0xeef3('0x3d')](_0x6cc222,_0x4cd0ea,_0x398284,_0x1d92a5,_0xeef3('0x3e'))[_0xeef3('0x3f')]('base64');}return crypto[_0xeef3('0x40')](_0x6cc222,_0x4cd0ea,_0x398284,_0x1d92a5,_0xeef3('0x3e'),function(_0x34f00d,_0x492f58){if(_0x34f00d){_0x37ded9(_0x34f00d);}return _0x37ded9(null,_0x492f58[_0xeef3('0x3f')](_0xeef3('0x3c')));});},'updatePassword':function(_0x28d363){if(!this[_0xeef3('0x15')]){return _0x28d363(null);}if(!validatePresenceOf(this[_0xeef3('0x15')])&&authTypes['indexOf'](this['provider'])===-0x1){_0x28d363(new Error(_0xeef3('0x41')));}var _0x41ff22=this;this[_0xeef3('0x42')](function(_0x39d493,_0x1fb3b){if(_0x39d493){return _0x28d363(_0x39d493);}_0x41ff22['salt']=_0x1fb3b;_0x41ff22['encryptPassword'](_0x41ff22['password'],function(_0x173671,_0x4924f5){if(_0x173671){_0x28d363(_0x173671);}_0x41ff22[_0xeef3('0x15')]=_0x4924f5;_0x28d363(null);});});},'updateMd5Password':function(){this[_0xeef3('0x43')]=md5(this[_0xeef3('0x18')]+_0xeef3('0x44')+this['password']);return this[_0xeef3('0x43')];}}});};