Built motion from commit 355702c8.|2.5.41
[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 _0x73c5=['users','utf8','utf8_bin','userProfileId','undefined','UserProfile','find','privacyEnabled','updatePassword','changed','then','VoiceExtension','bulkCreate','resolve','destroy','encryptPassword','function','Missing\x20Callback','base64','salt','pbkdf2Sync','sha1','toString','pbkdf2','indexOf','provider','Invalid\x20password','makeSalt',':asterisk:','bluebird','util','md5','lodash','./user.attributes','google','context','system','length','format','%s%s','prefix','push','merge','CDR(type)=%s','chanspy','${UNIQUEID}.%s,a','recordingFormat','auth','Authenticate','password','ChanSpy','SIP/%s,%s','name','options','join','StopMixMonitor','from-sip','Set','CDR(type)=internal','includes','extensionMonitor','_XCALLY_MIXMONITOR_FORMAT=%s','Dial','SIP/%s,,tTxX','Hangup','priority','defaults','VoiceChanSpy','findAll','internal','concat','catch','models'];(function(_0x46e363,_0x45d7e2){var _0x3fb059=function(_0x5e6274){while(--_0x5e6274){_0x46e363['push'](_0x46e363['shift']());}};_0x3fb059(++_0x45d7e2);}(_0x73c5,0xaf));var _0x573c=function(_0x4555c2,_0x138d83){_0x4555c2=_0x4555c2-0x0;var _0x492d1d=_0x73c5[_0x4555c2];return _0x492d1d;};'use strict';var BPromise=require(_0x573c('0x0'));var crypto=require('crypto');var util=require(_0x573c('0x1'));var md5=require(_0x573c('0x2'));var _=require(_0x573c('0x3'));var attributes=require(_0x573c('0x4'));var authTypes=[_0x573c('0x5')];var validatePresenceOf=function(_0x311f32){return _0x311f32&&_0x311f32['length'];};var getChanSpyApplications=function(_0x1b86fe,_0x20bac2,_0x305dbe){var _0x573653={'context':_0x1b86fe[_0x573c('0x6')]||'from-sip','type':_0x573c('0x7'),'isApp':![],'UserId':_0x1b86fe['id']};var _0x2638a0=[];for(var _0x1e0904=0x0,_0x38f33f=0x1;_0x1e0904<_0x305dbe[_0x573c('0x8')];_0x1e0904++,_0x38f33f=0x1){var _0x5ddba5={'exten':util[_0x573c('0x9')](_0x573c('0xa'),_0x305dbe[_0x1e0904][_0x573c('0xb')],_0x20bac2),'ChanSpyId':_0x305dbe[_0x1e0904]['id']};_0x2638a0[_0x573c('0xc')](_[_0x573c('0xd')]({'app':'Set','appdata':util[_0x573c('0x9')](_0x573c('0xe'),_0x573c('0xf')),'priority':_0x38f33f++},_0x573653,_0x5ddba5));if(_0x305dbe[_0x1e0904]['record']){_0x2638a0[_0x573c('0xc')](_[_0x573c('0xd')]({'app':'MixMonitor','appdata':util[_0x573c('0x9')](_0x573c('0x10'),_0x305dbe[_0x1e0904][_0x573c('0x11')]),'priority':_0x38f33f++},_0x573653,_0x5ddba5));}if(_0x305dbe[_0x1e0904][_0x573c('0x12')]){_0x2638a0['push'](_[_0x573c('0xd')]({'app':_0x573c('0x13'),'appdata':_0x305dbe[_0x1e0904][_0x573c('0x14')],'priority':_0x38f33f++},_0x573653,_0x5ddba5));}_0x2638a0['push'](_[_0x573c('0xd')]({'app':_0x573c('0x15'),'appdata':util['format'](_0x573c('0x16'),_0x1b86fe[_0x573c('0x17')],_0x305dbe[_0x1e0904][_0x573c('0x18')]?_0x305dbe[_0x1e0904][_0x573c('0x18')][_0x573c('0x19')](''):''),'priority':_0x38f33f++},_0x573653,_0x5ddba5));if(_0x305dbe[_0x1e0904]['record']){_0x2638a0[_0x573c('0xc')](_[_0x573c('0xd')]({'app':_0x573c('0x1a'),'priority':_0x38f33f++},_0x573653,_0x5ddba5));}}return _0x2638a0;};var getUserApplications=function(_0x5d15a,_0x2a347e){var _0x4c408a=['wav','WAV','gsm'];var _0x45213f={'context':_0x5d15a[_0x573c('0x6')]||_0x573c('0x1b'),'exten':_0x2a347e,'type':_0x573c('0x7'),'isApp':![],'UserId':_0x5d15a['id']};var _0x10912e=[{'app':_0x573c('0x1c'),'appdata':_0x573c('0x1d')}];if(_0x5d15a['extensionMonitor']&&_[_0x573c('0x1e')](_0x4c408a,_0x5d15a[_0x573c('0x1f')])){_0x10912e[_0x573c('0xc')]({'app':_0x573c('0x1c'),'appdata':'_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}'});_0x10912e[_0x573c('0xc')]({'app':_0x573c('0x1c'),'appdata':util['format'](_0x573c('0x20'),_0x5d15a[_0x573c('0x1f')])});_0x10912e[_0x573c('0xc')]({'app':_0x573c('0x21'),'appdata':util[_0x573c('0x9')]('SIP/%s,,tTxXU(xcally-mixmonitor-context)',_0x5d15a[_0x573c('0x17')])});}else{_0x10912e[_0x573c('0xc')]({'app':'Dial','appdata':util[_0x573c('0x9')](_0x573c('0x22'),_0x5d15a['name'])});}_0x10912e[_0x573c('0xc')]({'app':_0x573c('0x23')});for(var _0x1e3947=0x0;_0x1e3947<_0x10912e[_0x573c('0x8')];_0x1e3947++){_0x10912e[_0x1e3947][_0x573c('0x24')]=_0x1e3947+0x1;_0x10912e[_0x1e3947]=_[_0x573c('0x25')](_0x10912e[_0x1e3947],_0x45213f);}return _0x10912e;};var getChanSpyPromise=function(_0x431877,_0x204231){return new BPromise(function(_0x265c4b,_0x29b5ed){return _0x431877[_0x573c('0x26')][_0x573c('0x27')]()['then'](function(_0x32ae78){if(_0x32ae78){var _0x122321=getChanSpyApplications(_0x204231,_0x204231[_0x573c('0x28')],_0x32ae78);if(_0x204231[_0x573c('0x28')]!=_0x204231['name']){_0x122321=_0x122321[_0x573c('0x29')](getChanSpyApplications(_0x204231,_0x204231[_0x573c('0x17')],_0x32ae78));}return _0x265c4b(_0x122321);}return _0x265c4b([]);})[_0x573c('0x2a')](function(_0x21c1b8){_0x29b5ed(_0x21c1b8);});});};module['exports']=function(_0x2c1bb3,_0x3ffb1e){var _0x8d2263=_0x2c1bb3[_0x573c('0x2b')];return _0x2c1bb3['define']('User',attributes,{'tableName':_0x573c('0x2c'),'charset':_0x573c('0x2d'),'collate':_0x573c('0x2e'),'hooks':{'beforeCreate':function(_0x28a8a9,_0x40369d,_0x47b761){var _0x1d8461=typeof _0x28a8a9[_0x573c('0x2f')]===_0x573c('0x30')?BPromise['resolve'](null):_0x8d2263[_0x573c('0x31')][_0x573c('0x32')]({'where':{'id':_0x28a8a9[_0x573c('0x2f')]}});_0x1d8461['then'](function(_0x5515a1){if(_0x5515a1){_0x28a8a9[_0x573c('0x33')]=_0x5515a1[_0x573c('0x33')];}})['then'](function(){_0x28a8a9['updateMd5Password']();_0x28a8a9[_0x573c('0x34')](_0x47b761);})[_0x573c('0x2a')](function(_0x34d5ac){_0x47b761(_0x34d5ac);});},'beforeUpdate':function(_0x3b1502,_0x5cc0f3,_0x1115d1){Promise['resolve']()['then'](function(){if(_0x3b1502[_0x573c('0x35')](_0x573c('0x2f'))===![])return;return _0x8d2263[_0x573c('0x31')][_0x573c('0x32')]({'where':{'id':_0x3b1502[_0x573c('0x2f')]}})[_0x573c('0x36')](function(_0x3ece4d){if(_0x3ece4d)_0x3b1502[_0x573c('0x33')]=_0x3ece4d[_0x573c('0x33')];return;});})['then'](function(){if(_0x3b1502[_0x573c('0x35')](_0x573c('0x14'))){_0x3b1502['updateMd5Password']();return _0x3b1502[_0x573c('0x34')](_0x1115d1);}_0x1115d1();})[_0x573c('0x2a')](function(_0xc53dad){_0x1115d1(_0xc53dad);});},'afterCreate':function(_0x33a25c,_0x5a96f6,_0x569f7d){var _0x234995=getUserApplications(_0x33a25c,_0x33a25c['internal']);if(_0x33a25c[_0x573c('0x28')]!=_0x33a25c[_0x573c('0x17')]){_0x234995=_0x234995[_0x573c('0x29')](getUserApplications(_0x33a25c,_0x33a25c[_0x573c('0x17')]));}return _0x8d2263[_0x573c('0x37')][_0x573c('0x38')](_0x234995)[_0x573c('0x36')](function(){if(_0x33a25c[_0x573c('0xf')]){return getChanSpyPromise(_0x8d2263,_0x33a25c);}})[_0x573c('0x36')](function(_0xb451cb){if(_0xb451cb&&_0xb451cb['length']){return _0x8d2263[_0x573c('0x37')]['bulkCreate'](_0xb451cb);}})[_0x573c('0x36')](function(){_0x569f7d();})['catch'](function(_0xdb1808){_0x569f7d(_0xdb1808);});},'afterUpdate':function(_0x5e9e7e,_0x43deed,_0x39bfc7){return BPromise[_0x573c('0x39')]()[_0x573c('0x36')](function(){if(_0x5e9e7e[_0x573c('0x35')](_0x573c('0xf'))||_0x5e9e7e[_0x573c('0x35')](_0x573c('0x6'))){return _0x8d2263[_0x573c('0x37')][_0x573c('0x3a')]({'where':{'UserId':_0x5e9e7e['id'],'type':_0x573c('0x7'),'ChanSpyId':{'$ne':null}}})[_0x573c('0x36')](function(){if(_0x5e9e7e['chanspy']){return getChanSpyPromise(_0x8d2263,_0x5e9e7e)[_0x573c('0x36')](function(_0x527077){if(_0x527077&&_0x527077[_0x573c('0x8')]){return _0x8d2263[_0x573c('0x37')][_0x573c('0x38')](_0x527077);}});}});}})['then'](function(){if(_0x5e9e7e[_0x573c('0x35')](_0x573c('0x1f'))||_0x5e9e7e['changed'](_0x573c('0x6'))){return _0x8d2263[_0x573c('0x37')][_0x573c('0x3a')]({'where':{'UserId':_0x5e9e7e['id'],'type':_0x573c('0x7'),'ChanSpyId':null}})[_0x573c('0x36')](function(){var _0x1f813d=getUserApplications(_0x5e9e7e,_0x5e9e7e['internal']);if(_0x5e9e7e[_0x573c('0x28')]!=_0x5e9e7e[_0x573c('0x17')]){_0x1f813d=_0x1f813d[_0x573c('0x29')](getUserApplications(_0x5e9e7e,_0x5e9e7e[_0x573c('0x17')]));}return _0x8d2263[_0x573c('0x37')][_0x573c('0x38')](_0x1f813d);});}})[_0x573c('0x36')](function(){_0x39bfc7();})[_0x573c('0x2a')](function(_0x920442){_0x39bfc7(_0x920442);});},'afterDestroy':function(_0x2738a6,_0x1f207f){return _0x8d2263[_0x573c('0x37')]['destroy']({'where':{'UserId':_0x2738a6['id'],'type':'system'}});}},'instanceMethods':{'authenticate':function(_0x858d53,_0x36a851){if(!_0x36a851){return this[_0x573c('0x14')]===this[_0x573c('0x3b')](_0x858d53);}var _0x4b5538=this;this[_0x573c('0x3b')](_0x858d53,function(_0x20bbf7,_0x3f86d3){if(_0x20bbf7){_0x36a851(_0x20bbf7);}if(_0x4b5538[_0x573c('0x14')]===_0x3f86d3){_0x36a851(null,!![]);}else{_0x36a851(null,![]);}});},'makeSalt':function(_0x1effb1,_0x318b9c){var _0x23c64f=0x10;if(typeof arguments[0x0]===_0x573c('0x3c')){_0x318b9c=arguments[0x0];_0x1effb1=_0x23c64f;}else if(typeof arguments[0x1]===_0x573c('0x3c')){_0x318b9c=arguments[0x1];}else{throw new Error(_0x573c('0x3d'));}if(!_0x1effb1){_0x1effb1=_0x23c64f;}return crypto['randomBytes'](_0x1effb1,function(_0x38283d,_0x26fcac){if(_0x38283d){_0x318b9c(_0x38283d);}return _0x318b9c(null,_0x26fcac['toString'](_0x573c('0x3e')));});},'encryptPassword':function(_0x6f893f,_0x495443){if(!_0x6f893f||!this['salt']){return _0x495443?_0x495443(null):null;}var _0x55f6f5=0x2710;var _0x45cc8f=0x40;var _0x1a34e4=new Buffer(this[_0x573c('0x3f')],_0x573c('0x3e'));if(!_0x495443){return crypto[_0x573c('0x40')](_0x6f893f,_0x1a34e4,_0x55f6f5,_0x45cc8f,_0x573c('0x41'))[_0x573c('0x42')](_0x573c('0x3e'));}return crypto[_0x573c('0x43')](_0x6f893f,_0x1a34e4,_0x55f6f5,_0x45cc8f,_0x573c('0x41'),function(_0x518c16,_0x388170){if(_0x518c16){_0x495443(_0x518c16);}return _0x495443(null,_0x388170[_0x573c('0x42')](_0x573c('0x3e')));});},'updatePassword':function(_0x47178a){if(!this[_0x573c('0x14')]){return _0x47178a(null);}if(!validatePresenceOf(this['password'])&&authTypes[_0x573c('0x44')](this[_0x573c('0x45')])===-0x1){_0x47178a(new Error(_0x573c('0x46')));}var _0x267fbe=this;this[_0x573c('0x47')](function(_0x56ec2b,_0x5e5044){if(_0x56ec2b){return _0x47178a(_0x56ec2b);}_0x267fbe['salt']=_0x5e5044;_0x267fbe[_0x573c('0x3b')](_0x267fbe[_0x573c('0x14')],function(_0x4476dd,_0x1b7be2){if(_0x4476dd){_0x47178a(_0x4476dd);}_0x267fbe[_0x573c('0x14')]=_0x1b7be2;_0x47178a(null);});});},'updateMd5Password':function(){this['md5secret']=md5(this['name']+_0x573c('0x48')+this[_0x573c('0x14')]);return this['md5secret'];}}});};