Built motion from commit (unavailable).|2.2.1
[motion2.git] / server / api / authLocal / authLocal.controller.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 _0x757b=['This\x20name\x20is\x20not\x20registered','authenticate','This\x20password\x20is\x20not\x20correct','MailAccount','name','MailServerOut','Unable\x20to\x20find\x20a\x20configured\x20SMTP\x20service!','Smtp','service','host','port','secure','authentication','auth','pass','merge','\x22%s\x22\x20<%s>','email','request','SendMail','format','SendMail\x20error:\x20%s.\x20Please\x20contact\x20your\x20Administrator!','message','stringify','status','send','login','local','Something\x20went\x20wrong,\x20please\x20try\x20again.','online','body','trim','toLowerCase','This\x20email\x20is\x20not\x20registered.','save','xCally\x20Motion\x20Password\x20Reset','You\x20are\x20receiving\x20this\x20because\x20you\x20(or\x20someone\x20else)\x20have\x20requested\x20the\x20reset\x20of\x20the\x20password\x20for\x20your\x20account.\x0a\x0a','Please\x20click\x20on\x20the\x20following\x20link,\x20or\x20paste\x20this\x20into\x20your\x20browser\x20to\x20complete\x20the\x20process:\x0a\x0a','headers','Reset\x20Email\x20sent!','reset','token','now','Password\x20reset\x20token\x20is\x20invalid\x20or\x20has\x20expired.','password','resetPasswordToken','resetPasswordExpires','Password\x20reset!','use','util','passport-local','Strategy','bluebird','lodash','api','../../mysqldb','../../components/auth/service','signToken','client','http','role','agent','User','count','then','../../config/license/util','getLicense','agents','Maximum\x20login\x20has\x20been\x20reached','catch','find','admin','user'];(function(_0x5e36d1,_0x471178){var _0x950b9e=function(_0x1872f3){while(--_0x1872f3){_0x5e36d1['push'](_0x5e36d1['shift']());}};_0x950b9e(++_0x471178);}(_0x757b,0x7a));var _0xb757=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x757b[_0x3dd15e];return _0x231fd0;};'use strict';var util=require(_0xb757('0x0'));var passport=require('passport');var LocalStrategy=require(_0xb757('0x1'))[_0xb757('0x2')];var BPromise=require(_0xb757('0x3'));var jayson=require('jayson/promise');var _=require(_0xb757('0x4'));var logger=require('../../config/logger')(_0xb757('0x5'));var rs=require('randomstring');var db=require(_0xb757('0x6'))['db'];var signToken=require(_0xb757('0x7'))[_0xb757('0x8')];var client=jayson[_0xb757('0x9')][_0xb757('0xa')]({'port':0x232b});var getLicense=function(_0x21a519){return new BPromise(function(_0x2d9994,_0x318c3b){var _0x4ee3db=0x0;_0x21a519[_0xb757('0xb')]!==_0xb757('0xc')&&_0x2d9994(_0x21a519);return db[_0xb757('0xd')][_0xb757('0xe')]({'where':{'role':'agent','online':!![],'id':{'$ne':_0x21a519['id']}}})[_0xb757('0xf')](function(_0xd84a92){_0x4ee3db=_0xd84a92;return require(_0xb757('0x10'))[_0xb757('0x11')]();})['then'](function(_0x31747b){if(_0x31747b&&_0x31747b['agents']&&_0x4ee3db>=_0x31747b[_0xb757('0x12')]){return _0x318c3b({'message':_0xb757('0x13')});}return _0x2d9994(_0x21a519);})[_0xb757('0x14')](function(_0xb56382){return _0x318c3b(_0xb56382);});});};function localAuthenticate(_0x6d3db2,_0x19173d,_0x4227b6,_0x1d1d1b){_0x6d3db2[_0xb757('0x15')]({'where':{'name':_0x19173d,'role':{'$or':[_0xb757('0x16'),_0xb757('0x17'),_0xb757('0xc')]}}})[_0xb757('0xf')](function(_0x53efa1){if(!_0x53efa1||_0x53efa1&&_0x53efa1['name']!==_0x19173d){return _0x1d1d1b(null,![],{'message':_0xb757('0x18')});}return getLicense(_0x53efa1);})[_0xb757('0xf')](function(_0x137ae3){_0x137ae3[_0xb757('0x19')](_0x4227b6,function(_0xa08286,_0x351b9a){if(_0xa08286){return _0x1d1d1b(_0xa08286);}if(!_0x351b9a){return _0x1d1d1b(null,![],{'message':_0xb757('0x1a')});}else{return _0x1d1d1b(null,_0x137ae3);}});})[_0xb757('0x14')](function(_0x3550e8){_0x1d1d1b(_0x3550e8);});}function sendServiceMail(_0x42d9ad){return db[_0xb757('0x1b')][_0xb757('0x15')]({'where':{'service':!![]},'attributes':['id',_0xb757('0x1c'),'email'],'include':[{'model':db[_0xb757('0x1d')],'as':'Smtp'}]})[_0xb757('0xf')](function(_0x1c686a){if(!_0x1c686a){throw new Error(_0xb757('0x1e'));}var _0x1f0f6c={'tls':{'rejectUnauthorized':![]}};if(_0x1c686a[_0xb757('0x1f')][_0xb757('0x20')]){_0x1f0f6c[_0xb757('0x20')]=_0x1c686a[_0xb757('0x1f')][_0xb757('0x20')];}else{_0x1f0f6c[_0xb757('0x21')]=_0x1c686a[_0xb757('0x1f')][_0xb757('0x21')];_0x1f0f6c['port']=_0x1c686a[_0xb757('0x1f')][_0xb757('0x22')];_0x1f0f6c[_0xb757('0x23')]=_0x1c686a[_0xb757('0x1f')][_0xb757('0x23')];}if(_0x1c686a[_0xb757('0x1f')][_0xb757('0x24')]){_0x1f0f6c[_0xb757('0x25')]={'user':_0x1c686a[_0xb757('0x1f')][_0xb757('0x17')],'pass':_0x1c686a[_0xb757('0x1f')][_0xb757('0x26')]};}var _0x3f9e6e={'account':_0x1f0f6c,'message':_[_0xb757('0x27')]({'from':util['format'](_0xb757('0x28'),_0x1c686a['name'],_0x1c686a[_0xb757('0x29')]||_0x1c686a[_0xb757('0x1f')][_0xb757('0x17')])},_0x42d9ad)};return client[_0xb757('0x2a')](_0xb757('0x2b'),_0x3f9e6e);})['then'](function(_0x345659){if(_0x345659['error']){throw new Error(util[_0xb757('0x2c')](_0xb757('0x2d'),_0x345659['error'][_0xb757('0x2e')]||JSON[_0xb757('0x2f')](_0x345659['error'])));}return!![];});}function handleError(_0xdad4d2,_0x211a53,_0x142429){return _0xdad4d2[_0xb757('0x30')](_0x211a53||0x1f4)[_0xb757('0x31')]({'message':_0x142429?_0x142429[_0xb757('0x2e')]||_0x142429:''});}exports[_0xb757('0x32')]=function(_0x112b07,_0x2d88d6,_0x9586b5){passport['authenticate'](_0xb757('0x33'),function(_0x557f0c,_0x2a12b4,_0x3ce118){var _0x3a69b0=_0x557f0c||_0x3ce118;if(_0x3a69b0){return handleError(_0x2d88d6,0x191,_0x3a69b0);}if(!_0x2a12b4){return handleError(_0x2d88d6,0x194,{'message':_0xb757('0x34')});}var _0x5105ed=signToken(_0x2a12b4['id'],_0x2a12b4[_0xb757('0xb')]);_0x2d88d6['json']({'id':_0x2a12b4['id'],'token':_0x5105ed,'statusCall':'','isAlreadyLogged':_0x2a12b4[_0xb757('0x35')]});})(_0x112b07,_0x2d88d6,_0x9586b5);};exports['forgot']=function(_0x8becd9,_0x533261,_0x144fca){if(!_0x8becd9['body'][_0xb757('0x29')]){return handleError(_0x533261,0x1f4,{'message':'Email\x20parameter\x20is\x20missing'});}var _0x5ebc94=rs['generate']();_0x8becd9[_0xb757('0x36')][_0xb757('0x29')]=_0x8becd9[_0xb757('0x36')][_0xb757('0x29')][_0xb757('0x37')]()[_0xb757('0x38')]();return db[_0xb757('0xd')][_0xb757('0x15')]({'where':{'email':_0x8becd9[_0xb757('0x36')][_0xb757('0x29')]}})['then'](function(_0x42744b){if(!_0x42744b){throw new Error(_0xb757('0x39'));}_0x42744b['resetPasswordToken']=_0x5ebc94;_0x42744b['resetPasswordExpires']=Date['now']()+0x36ee80;return _0x42744b[_0xb757('0x3a')]();})[_0xb757('0xf')](function(){return sendServiceMail({'to':_0x8becd9[_0xb757('0x36')][_0xb757('0x29')],'subject':_0xb757('0x3b'),'text':_0xb757('0x3c')+_0xb757('0x3d')+'http://'+_0x8becd9[_0xb757('0x3e')][_0xb757('0x21')]+'/reset/'+_0x5ebc94+'\x0a\x0a'+'If\x20you\x20did\x20not\x20request\x20this,\x20please\x20ignore\x20this\x20email\x20and\x20your\x20password\x20will\x20remain\x20unchanged.\x0a'});})[_0xb757('0xf')](function(){_0x533261[_0xb757('0x30')](0xc8)[_0xb757('0x31')]({'message':_0xb757('0x3f')});})['catch'](function(_0x255751){return handleError(_0x533261,0x1f4,_0x255751);});};exports[_0xb757('0x40')]=function(_0x559b99,_0x3515d2,_0x540874){return db[_0xb757('0xd')][_0xb757('0x15')]({'where':{'resetPasswordToken':_0x559b99['params'][_0xb757('0x41')],'resetPasswordExpires':{'$gt':Date[_0xb757('0x42')]()}}})[_0xb757('0xf')](function(_0x539898){if(!_0x539898){throw new Error(_0xb757('0x43'));}_0x539898[_0xb757('0x44')]=_0x559b99[_0xb757('0x36')][_0xb757('0x44')];_0x539898[_0xb757('0x45')]=null;_0x539898[_0xb757('0x46')]=null;return _0x539898[_0xb757('0x3a')]();})[_0xb757('0xf')](function(){_0x3515d2['status'](0xc8)[_0xb757('0x31')]({'message':_0xb757('0x47')});})[_0xb757('0x14')](function(_0x36daaa){return handleError(_0x3515d2,0x1f4,_0x36daaa);});};passport[_0xb757('0x48')](new LocalStrategy({'usernameField':_0xb757('0x1c'),'passwordField':_0xb757('0x44')},function(_0x5b212d,_0xa4b19a,_0x5d3084){return localAuthenticate(db[_0xb757('0xd')],_0x5b212d,_0xa4b19a,_0x5d3084);}));