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 _0xe49d=['encryptPassword','function','randomBytes','toString','base64','salt','pbkdf2Sync','sha1','pbkdf2','indexOf','provider','makeSalt','md5secret','bluebird','util','md5','lodash','google','context','from-sip','system','length','%s%s','prefix','merge','Set','format','CDR(type)=%s','chanspy','record','push','MixMonitor','${UNIQUEID}.%s,a','password','ChanSpy','SIP/%s,%s','name','options','join','StopMixMonitor','wav','WAV','gsm','CDR(type)=internal','extensionMonitor','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=%s','Dial','SIP/%s,,tTxX','priority','defaults','VoiceChanSpy','findAll','then','internal','concat','exports','define','User','utf8','utf8_bin','updatePassword','updateMd5Password','VoiceExtension','bulkCreate','catch','resolve','changed','destroy'];(function(_0x316a21,_0x538f64){var _0x463431=function(_0x3892b9){while(--_0x3892b9){_0x316a21['push'](_0x316a21['shift']());}};_0x463431(++_0x538f64);}(_0xe49d,0xdc));var _0xde49=function(_0x321bb2,_0x202794){_0x321bb2=_0x321bb2-0x0;var _0x9c12ed=_0xe49d[_0x321bb2];return _0x9c12ed;};'use strict';var BPromise=require(_0xde49('0x0'));var crypto=require('crypto');var util=require(_0xde49('0x1'));var md5=require(_0xde49('0x2'));var _=require(_0xde49('0x3'));var attributes=require('./user.attributes');var authTypes=[_0xde49('0x4')];var validatePresenceOf=function(_0x307d4e){return _0x307d4e&&_0x307d4e['length'];};var getChanSpyApplications=function(_0x520b80,_0x3ab5c6,_0x18aa9c){var _0x4e3e43={'context':_0x520b80[_0xde49('0x5')]||_0xde49('0x6'),'type':_0xde49('0x7'),'isApp':![],'UserId':_0x520b80['id']};var _0x12931a=[];for(var _0x3a33d5=0x0,_0x897e84=0x1;_0x3a33d5<_0x18aa9c[_0xde49('0x8')];_0x3a33d5++,_0x897e84=0x1){var _0x4976e9={'exten':util['format'](_0xde49('0x9'),_0x18aa9c[_0x3a33d5][_0xde49('0xa')],_0x3ab5c6),'ChanSpyId':_0x18aa9c[_0x3a33d5]['id']};_0x12931a['push'](_[_0xde49('0xb')]({'app':_0xde49('0xc'),'appdata':util[_0xde49('0xd')](_0xde49('0xe'),_0xde49('0xf')),'priority':_0x897e84++},_0x4e3e43,_0x4976e9));if(_0x18aa9c[_0x3a33d5][_0xde49('0x10')]){_0x12931a[_0xde49('0x11')](_['merge']({'app':_0xde49('0x12'),'appdata':util[_0xde49('0xd')](_0xde49('0x13'),_0x18aa9c[_0x3a33d5]['recordingFormat']),'priority':_0x897e84++},_0x4e3e43,_0x4976e9));}if(_0x18aa9c[_0x3a33d5]['auth']){_0x12931a[_0xde49('0x11')](_['merge']({'app':'Authenticate','appdata':_0x18aa9c[_0x3a33d5][_0xde49('0x14')],'priority':_0x897e84++},_0x4e3e43,_0x4976e9));}_0x12931a[_0xde49('0x11')](_[_0xde49('0xb')]({'app':_0xde49('0x15'),'appdata':util[_0xde49('0xd')](_0xde49('0x16'),_0x520b80[_0xde49('0x17')],_0x18aa9c[_0x3a33d5][_0xde49('0x18')]?_0x18aa9c[_0x3a33d5]['options'][_0xde49('0x19')](''):''),'priority':_0x897e84++},_0x4e3e43,_0x4976e9));if(_0x18aa9c[_0x3a33d5][_0xde49('0x10')]){_0x12931a[_0xde49('0x11')](_['merge']({'app':_0xde49('0x1a'),'priority':_0x897e84++},_0x4e3e43,_0x4976e9));}}return _0x12931a;};var getUserApplications=function(_0x2f851a,_0xac9a84){var _0x356a6d=[_0xde49('0x1b'),_0xde49('0x1c'),_0xde49('0x1d')];var _0x2fe766={'context':_0x2f851a[_0xde49('0x5')]||_0xde49('0x6'),'exten':_0xac9a84,'type':_0xde49('0x7'),'isApp':![],'UserId':_0x2f851a['id']};var _0x1b38a7=[{'app':_0xde49('0xc'),'appdata':_0xde49('0x1e')}];if(_0x2f851a[_0xde49('0x1f')]&&_['includes'](_0x356a6d,_0x2f851a[_0xde49('0x1f')])){_0x1b38a7[_0xde49('0x11')]({'app':_0xde49('0xc'),'appdata':_0xde49('0x20')});_0x1b38a7[_0xde49('0x11')]({'app':_0xde49('0xc'),'appdata':util[_0xde49('0xd')](_0xde49('0x21'),_0x2f851a[_0xde49('0x1f')])});_0x1b38a7[_0xde49('0x11')]({'app':_0xde49('0x22'),'appdata':util[_0xde49('0xd')]('SIP/%s,,tTxXU(xcally-mixmonitor-context)',_0x2f851a['name'])});}else{_0x1b38a7['push']({'app':'Dial','appdata':util['format'](_0xde49('0x23'),_0x2f851a['name'])});}_0x1b38a7[_0xde49('0x11')]({'app':'Hangup'});for(var _0x1f8fa1=0x0;_0x1f8fa1<_0x1b38a7['length'];_0x1f8fa1++){_0x1b38a7[_0x1f8fa1][_0xde49('0x24')]=_0x1f8fa1+0x1;_0x1b38a7[_0x1f8fa1]=_[_0xde49('0x25')](_0x1b38a7[_0x1f8fa1],_0x2fe766);}return _0x1b38a7;};var getChanSpyPromise=function(_0x1ef8a7,_0x9cde6b){return new BPromise(function(_0x4170f8,_0x66c827){return _0x1ef8a7[_0xde49('0x26')][_0xde49('0x27')]()[_0xde49('0x28')](function(_0x1ec751){if(_0x1ec751){var _0x28aee3=getChanSpyApplications(_0x9cde6b,_0x9cde6b['internal'],_0x1ec751);if(_0x9cde6b[_0xde49('0x29')]!=_0x9cde6b[_0xde49('0x17')]){_0x28aee3=_0x28aee3[_0xde49('0x2a')](getChanSpyApplications(_0x9cde6b,_0x9cde6b['name'],_0x1ec751));}return _0x4170f8(_0x28aee3);}return _0x4170f8([]);})['catch'](function(_0x42892a){_0x66c827(_0x42892a);});});};module[_0xde49('0x2b')]=function(_0x195219,_0x4e657c){var _0x1da0db=_0x195219['models'];return _0x195219[_0xde49('0x2c')](_0xde49('0x2d'),attributes,{'tableName':'users','charset':_0xde49('0x2e'),'collate':_0xde49('0x2f'),'hooks':{'beforeCreate':function(_0x24a288,_0x4702fe,_0x1b52d3){_0x24a288['updateMd5Password']();_0x24a288[_0xde49('0x30')](_0x1b52d3);},'beforeUpdate':function(_0x4b534c,_0x55cba4,_0x5caa11){if(_0x4b534c['changed'](_0xde49('0x14'))){_0x4b534c[_0xde49('0x31')]();return _0x4b534c['updatePassword'](_0x5caa11);}_0x5caa11();},'afterCreate':function(_0x41a7ef,_0x2778b6,_0x4d010e){var _0x4b5b47=getUserApplications(_0x41a7ef,_0x41a7ef[_0xde49('0x29')]);if(_0x41a7ef[_0xde49('0x29')]!=_0x41a7ef['name']){_0x4b5b47=_0x4b5b47[_0xde49('0x2a')](getUserApplications(_0x41a7ef,_0x41a7ef[_0xde49('0x17')]));}return _0x1da0db[_0xde49('0x32')][_0xde49('0x33')](_0x4b5b47)[_0xde49('0x28')](function(){if(_0x41a7ef[_0xde49('0xf')]){return getChanSpyPromise(_0x1da0db,_0x41a7ef);}})['then'](function(_0x7d2fd6){if(_0x7d2fd6&&_0x7d2fd6[_0xde49('0x8')]){return _0x1da0db[_0xde49('0x32')]['bulkCreate'](_0x7d2fd6);}})['then'](function(){_0x4d010e();})[_0xde49('0x34')](function(_0x5a8dc9){_0x4d010e(_0x5a8dc9);});},'afterUpdate':function(_0x2fc32c,_0x2c8509,_0x2dfa4d){return BPromise[_0xde49('0x35')]()['then'](function(){if(_0x2fc32c['changed'](_0xde49('0xf'))||_0x2fc32c[_0xde49('0x36')]('context')){return _0x1da0db['VoiceExtension'][_0xde49('0x37')]({'where':{'UserId':_0x2fc32c['id'],'type':_0xde49('0x7'),'ChanSpyId':{'$ne':null}}})[_0xde49('0x28')](function(){if(_0x2fc32c['chanspy']){return getChanSpyPromise(_0x1da0db,_0x2fc32c)[_0xde49('0x28')](function(_0x1fc95a){if(_0x1fc95a&&_0x1fc95a[_0xde49('0x8')]){return _0x1da0db[_0xde49('0x32')][_0xde49('0x33')](_0x1fc95a);}});}});}})['then'](function(){if(_0x2fc32c[_0xde49('0x36')](_0xde49('0x1f'))||_0x2fc32c['changed']('context')){return _0x1da0db[_0xde49('0x32')][_0xde49('0x37')]({'where':{'UserId':_0x2fc32c['id'],'type':_0xde49('0x7'),'ChanSpyId':null}})[_0xde49('0x28')](function(){var _0x5a1ce3=getUserApplications(_0x2fc32c,_0x2fc32c[_0xde49('0x29')]);if(_0x2fc32c[_0xde49('0x29')]!=_0x2fc32c['name']){_0x5a1ce3=_0x5a1ce3['concat'](getUserApplications(_0x2fc32c,_0x2fc32c[_0xde49('0x17')]));}return _0x1da0db[_0xde49('0x32')][_0xde49('0x33')](_0x5a1ce3);});}})[_0xde49('0x28')](function(){_0x2dfa4d();})[_0xde49('0x34')](function(_0x290197){_0x2dfa4d(_0x290197);});},'afterDestroy':function(_0x50a307,_0x1c53aa){return _0x1da0db[_0xde49('0x32')][_0xde49('0x37')]({'where':{'UserId':_0x50a307['id'],'type':_0xde49('0x7')}});}},'instanceMethods':{'authenticate':function(_0x5cebd4,_0x368584){if(!_0x368584){return this[_0xde49('0x14')]===this[_0xde49('0x38')](_0x5cebd4);}var _0x46b6f7=this;this[_0xde49('0x38')](_0x5cebd4,function(_0x6ad841,_0x29c208){if(_0x6ad841){_0x368584(_0x6ad841);}if(_0x46b6f7[_0xde49('0x14')]===_0x29c208){_0x368584(null,!![]);}else{_0x368584(null,![]);}});},'makeSalt':function(_0x450c53,_0x19b510){var _0x238870=0x10;if(typeof arguments[0x0]===_0xde49('0x39')){_0x19b510=arguments[0x0];_0x450c53=_0x238870;}else if(typeof arguments[0x1]===_0xde49('0x39')){_0x19b510=arguments[0x1];}else{throw new Error('Missing\x20Callback');}if(!_0x450c53){_0x450c53=_0x238870;}return crypto[_0xde49('0x3a')](_0x450c53,function(_0x218b96,_0x490dcd){if(_0x218b96){_0x19b510(_0x218b96);}return _0x19b510(null,_0x490dcd[_0xde49('0x3b')](_0xde49('0x3c')));});},'encryptPassword':function(_0x49e457,_0x5570b8){if(!_0x49e457||!this[_0xde49('0x3d')]){return _0x5570b8?_0x5570b8(null):null;}var _0x3b5f18=0x2710;var _0x4e8cb1=0x40;var _0x5ee7f9=new Buffer(this[_0xde49('0x3d')],'base64');if(!_0x5570b8){return crypto[_0xde49('0x3e')](_0x49e457,_0x5ee7f9,_0x3b5f18,_0x4e8cb1,_0xde49('0x3f'))[_0xde49('0x3b')]('base64');}return crypto[_0xde49('0x40')](_0x49e457,_0x5ee7f9,_0x3b5f18,_0x4e8cb1,_0xde49('0x3f'),function(_0x2e2d00,_0xd89215){if(_0x2e2d00){_0x5570b8(_0x2e2d00);}return _0x5570b8(null,_0xd89215['toString'](_0xde49('0x3c')));});},'updatePassword':function(_0x5c20ff){if(!this[_0xde49('0x14')]){return _0x5c20ff(null);}if(!validatePresenceOf(this['password'])&&authTypes[_0xde49('0x41')](this[_0xde49('0x42')])===-0x1){_0x5c20ff(new Error('Invalid\x20password'));}var _0x579770=this;this[_0xde49('0x43')](function(_0x103e2b,_0x269ebd){if(_0x103e2b){return _0x5c20ff(_0x103e2b);}_0x579770[_0xde49('0x3d')]=_0x269ebd;_0x579770[_0xde49('0x38')](_0x579770[_0xde49('0x14')],function(_0x166d8e,_0x1d18c2){if(_0x166d8e){_0x5c20ff(_0x166d8e);}_0x579770[_0xde49('0x14')]=_0x1d18c2;_0x5c20ff(null);});});},'updateMd5Password':function(){this[_0xde49('0x44')]=md5(this[_0xde49('0x17')]+':asterisk:'+this[_0xde49('0x14')]);return this[_0xde49('0x44')];}}});};