Built motion from commit 70c527640.|1.0.24
[motion.git] / server / models / user.js
1 'use strict';
2
3 var crypto = require('crypto');
4 var md5 = require('md5');
5 var _ = require('lodash');
6 var moment = require('moment');
7
8 module.exports = function(sequelize, DataTypes) {
9   var User = sequelize.define('User', {
10     name: {
11       type: DataTypes.STRING,
12       unique: 'name',
13       validate: {
14         notEmpty: true,
15         is: /^[A-Za-z0-9\.\_]+$/i
16       },
17       set: function(name) {
18         this.setDataValue('name', name);
19         this.setDataValue('defaultuser', name);
20       }
21     },
22     fullname: {
23       type: DataTypes.STRING,
24       allowNull: false,
25     },
26     email: {
27       type: DataTypes.STRING,
28       unique: 'email',
29       isEmail: true,
30       set: function(email) {
31         if (email) {
32           this.setDataValue('email', email.toLowerCase());
33         }
34       },
35       defaultValue: null
36     },
37     role: {
38       type: DataTypes.ENUM('admin', 'user', 'agent', 'telephone')
39     },
40     password: {
41       type: DataTypes.STRING,
42       allowNull: false,
43       validate: {
44         notEmpty: true
45       },
46       set: function(password) {
47         this.salt = this.makeSalt();
48         this.setDataValue('password', this.encryptPassword(password));
49         this.setDataValue('md5secret', this.md5Password(this.name + ':asterisk:' + password));
50       }
51     },
52     provider: {
53       type: DataTypes.STRING,
54       defaultValue: 'local'
55     },
56     internal: {
57       type: DataTypes.INTEGER(11),
58       unique: 'internal',
59       set: function(internal) {
60         this.setDataValue('internal', internal);
61         this.setDataValue('accountcode', internal);
62       }
63     },
64     salt: {
65       type: DataTypes.STRING
66     },
67     phone: {
68       type: DataTypes.STRING
69     },
70     mobile: {
71       type: DataTypes.STRING
72     },
73     address: {
74       type: DataTypes.STRING
75     },
76     zipcode: {
77       type: DataTypes.STRING
78     },
79     userpic: {
80       type: DataTypes.STRING
81     },
82     city: {
83       type: DataTypes.STRING
84     },
85     country: {
86       type: DataTypes.STRING
87     },
88     online: {
89       type: DataTypes.BOOLEAN,
90       defaultValue: false
91     },
92     lastLoginAt: {
93       type: DataTypes.DATE
94     },
95     status: {
96       type: DataTypes.STRING,
97       defaultValue: 'UNKNOWN'
98     },
99     statusAt: {
100       type: DataTypes.DATE
101     },
102     queueStatus: {
103       type: DataTypes.STRING,
104       defaultValue: 'complete'
105     },
106     queueStatusAt: {
107       type: DataTypes.DATE
108     },
109     lastQueue: {
110       type: DataTypes.STRING
111     },
112     voicePause: {
113       type: DataTypes.BOOLEAN,
114       defaultValue: false,
115       // set: function(voicePause) {
116       //   this.setDataValue('voicePause', voicePause);
117       //   if (voicePause) {
118       //     this.setDataValue('queueStatus', 'paused');
119       //     this.setDataValue('queueStatusAt', moment().format("YYYY-MM-DD HH:mm:ss"));
120       //   } else {
121       //     this.setDataValue('queueStatus', 'complete');
122       //     this.setDataValue('queueStatusAt', moment().format("YYYY-MM-DD HH:mm:ss"));
123       //   }
124       // }
125     },
126     chatPause: {
127       type: DataTypes.BOOLEAN,
128       defaultValue: false
129     },
130     mailPause: {
131       type: DataTypes.BOOLEAN,
132       defaultValue: false
133     },
134     faxPause: {
135       type: DataTypes.BOOLEAN,
136       defaultValue: false
137     },
138     smsPause: {
139       type: DataTypes.BOOLEAN,
140       defaultValue: false
141     },
142     openchannelPause: {
143       type: DataTypes.BOOLEAN,
144       defaultValue: false
145     },
146     pauseType: {
147       type: DataTypes.STRING,
148       defaultValue: 'Default Pause'
149     },
150     lastPauseAt: {
151       type: DataTypes.DATE
152     },
153     chatCapacity: {
154       type: DataTypes.INTEGER,
155       defaultValue: 0
156     },
157     mailCapacity: {
158       type: DataTypes.INTEGER,
159       defaultValue: 0
160     },
161     faxCapacity: {
162       type: DataTypes.INTEGER,
163       defaultValue: 0
164     },
165     smsCapacity: {
166       type: DataTypes.INTEGER,
167       defaultValue: 0
168     },
169     openchannelCapacity: {
170       type: DataTypes.INTEGER,
171       defaultValue: 0
172     },
173     phoneBarAutoAnswer: {
174       type: DataTypes.BOOLEAN,
175       defaultValue: false
176     },
177     phoneBarEnableSettings: {
178       type: DataTypes.BOOLEAN,
179       defaultValue: true
180     },
181     phoneBarUnconditionalNumber: {
182       type: DataTypes.STRING,
183       get: function() {
184         if (this.getDataValue('phoneBarUnconditional')) {
185           return this.getDataValue('phoneBarUnconditionalNumber');
186         }
187         return null;
188       }
189     },
190     phoneBarNoReplyNumber: {
191       type: DataTypes.STRING,
192       get: function() {
193         if (this.getDataValue('phoneBarNoReply')) {
194           return this.getDataValue('phoneBarNoReplyNumber');
195         }
196         return null;
197       }
198     },
199     phoneBarBusyNumber: {
200       type: DataTypes.STRING,
201       get: function() {
202         if (this.getDataValue('phoneBarBusy')) {
203           return this.getDataValue('phoneBarBusyNumber');
204         }
205         return null;
206       }
207     },
208     phoneBarUnconditional: {
209       type: DataTypes.BOOLEAN,
210       defaultValue: false
211     },
212     phoneBarNoReply: {
213       type: DataTypes.BOOLEAN,
214       defaultValue: false
215     },
216     phoneBarBusy: {
217       type: DataTypes.BOOLEAN,
218       defaultValue: false
219     },
220     phoneBarListenPort: {
221       type: DataTypes.INTEGER(5),
222       defaultValue: 5060
223     },
224     phoneBarECTail: {
225       type: DataTypes.INTEGER(5),
226       defaultValue: 200
227     },
228     phoneBarExpires: {
229       type: DataTypes.INTEGER(5),
230       defaultValue: 120
231     },
232     phoneBarNameServer: {
233       type: DataTypes.STRING,
234       allowNull: true
235     },
236     phoneBarStunServer: {
237       type: DataTypes.STRING,
238       allowNull: true
239     },
240     phoneBarVADEnabled: {
241       type: DataTypes.BOOLEAN,
242       defaultValue: false
243     },
244     phoneBarNoUDP: {
245       type: DataTypes.BOOLEAN,
246       defaultValue: false
247     },
248     phoneBarNoTCP: {
249       type: DataTypes.BOOLEAN,
250       defaultValue: true
251     },
252     phoneBarLogLevel: {
253       type: DataTypes.INTEGER(5),
254       defaultValue: 1
255     },
256     phoneBarPublishEnabled: {
257       type: DataTypes.BOOLEAN,
258       defaultValue: false
259     },
260     phoneBarRemoteControl: {
261       type: DataTypes.BOOLEAN,
262       defaultValue: false
263     },
264     phoneBarRemoteControlPort: {
265       type: DataTypes.INTEGER,
266       defaultValue: 9888
267     },
268     chanspy: {
269       type: DataTypes.BOOLEAN,
270       defaultValue: false
271     },
272     description: {
273       type: DataTypes.STRING,
274       allowNull: true,
275     },
276     host: {
277       type: DataTypes.STRING,
278       allowNull: true,
279       defaultValue: 'dynamic'
280     },
281     ipaddr: { //REALTIME ASTERISK
282       type: DataTypes.STRING,
283       allowNull: true,
284     },
285     port: { //REALTIME ASTERISK
286       type: DataTypes.INTEGER(5),
287       allowNull: true,
288     },
289     regseconds: { //REALTIME ASTERISK
290       type: DataTypes.INTEGER(11),
291       allowNull: true,
292     },
293     fullcontact: { //REALTIME ASTERISK
294       type: DataTypes.STRING,
295       allowNull: true,
296     },
297     regserver: { //REALTIME ASTERISK
298       type: DataTypes.STRING,
299       allowNull: true,
300     },
301     useragent: { //REALTIME ASTERISK
302       type: DataTypes.STRING,
303       allowNull: true,
304     },
305     lastms: { //REALTIME ASTERISK
306       type: DataTypes.INTEGER(11),
307       allowNull: true,
308     },
309     type: {
310       type: DataTypes.ENUM('friend', 'user', 'peer'),
311       allowNull: true,
312       defaultValue: 'friend'
313     },
314     context: {
315       type: DataTypes.STRING,
316       allowNull: true,
317       defaultValue: 'from-sip'
318     },
319     callingpres: {
320       type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
321         'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
322         'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
323         'PROHIB_FAILED_SCREEN', 'PROHIB'),
324       allowNull: true,
325     },
326     deny: {
327       type: DataTypes.STRING,
328       allowNull: true,
329     },
330     permit: {
331       type: DataTypes.STRING,
332       allowNull: true,
333     },
334     secret: {
335       type: DataTypes.STRING,
336       allowNull: true,
337     },
338     md5secret: {
339       type: DataTypes.STRING,
340       allowNull: true,
341     },
342     remotesecret: {
343       type: DataTypes.STRING,
344       allowNull: true,
345     },
346     transport: {
347       type: DataTypes.STRING,
348       allowNull: true,
349       defaultValue: 'udp'
350     },
351     dtmfmode: {
352       type: DataTypes.ENUM('rfc2833', 'info', 'shortinfo', 'inband',
353         'auto'),
354       allowNull: true,
355       defaultValue: 'rfc2833'
356     },
357     directmedia: {
358       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'outgoing'),
359       allowNull: true,
360       defaultValue: 'no'
361     },
362     directrtpsetup: {
363       type: DataTypes.ENUM('yes', 'no'),
364       allowNull: true,
365       defaultValue: 'no'
366     },
367     directmediapermit: {
368       type: DataTypes.STRING,
369       allowNull: true,
370     },
371     directmediadeny: {
372       type: DataTypes.STRING,
373       allowNull: true,
374     },
375     nat: {
376       type: DataTypes.STRING,
377       allowNull: true,
378       defaultValue: 'force_rport,comedia'
379     },
380     callgroup: {
381       type: DataTypes.STRING,
382       allowNull: true,
383     },
384     namedcallgroup: { //We are in named call groups engineering,sales,netgroup,protgroup
385       type: DataTypes.STRING,
386       allowNull: true,
387     },
388     pickupgroup: {
389       type: DataTypes.STRING,
390       allowNull: true,
391     },
392     namedpickupgroup: { //We can do call pick-p for named call group sales
393       type: DataTypes.STRING,
394       allowNull: true,
395     },
396     language: {
397       type: DataTypes.STRING,
398       allowNull: true,
399       defaultValue: 'en'
400     },
401     tonezone: {
402       type: DataTypes.STRING,
403       allowNull: true
404     },
405     disallow: {
406       type: DataTypes.STRING,
407       allowNull: true,
408       defaultValue: 'all'
409     },
410     allow: {
411       type: DataTypes.STRING,
412       allowNull: true,
413       defaultValue: 'ulaw;gsm'
414     },
415     autoframing: {
416       type: DataTypes.ENUM('yes', 'no'),
417       allowNull: true,
418     },
419     insecure: {
420       type: DataTypes.STRING,
421       allowNull: true,
422       defaultValue: 'port,invite'
423     },
424     trustrpid: {
425       type: DataTypes.ENUM('yes', 'no'),
426       allowNull: true,
427       defaultValue: 'no'
428     },
429     trust_id_outbound: {
430       type: DataTypes.ENUM('yes', 'no'),
431       allowNull: true,
432       defaultValue: 'no'
433     },
434     progressinband: {
435       type: DataTypes.ENUM('yes', 'no', 'never'),
436       allowNull: true,
437     },
438     promiscredir: {
439       type: DataTypes.ENUM('yes', 'no'),
440       allowNull: true,
441     },
442     useclientcode: {
443       type: DataTypes.ENUM('yes', 'no'),
444       allowNull: true,
445     },
446     accountcode: {
447       type: DataTypes.INTEGER(11),
448       allowNull: true,
449     },
450     setvar: {
451       type: DataTypes.STRING,
452       allowNull: true,
453     },
454     callerid: {
455       type: DataTypes.STRING,
456       allowNull: true,
457       defaultValue: '"" <>'
458     },
459     amaflags: {
460       type: DataTypes.STRING,
461       allowNull: true,
462     },
463     callcounter: {
464       type: DataTypes.ENUM('yes', 'no'),
465       allowNull: true,
466       defaultValue: 'yes'
467     },
468     busylevel: {
469       type: DataTypes.INTEGER(11),
470       allowNull: true,
471     },
472     allowoverlap: {
473       type: DataTypes.ENUM('yes', 'no'),
474       allowNull: true,
475     },
476     allowsubscribe: {
477       type: DataTypes.ENUM('yes', 'no'),
478       allowNull: true,
479     },
480     allowtransfer: {
481       type: DataTypes.ENUM('yes', 'no'),
482       allowNull: true,
483     },
484     ignoresdpversion: {
485       type: DataTypes.ENUM('yes', 'no'),
486       allowNull: true,
487     },
488     subscribecontext: {
489       type: DataTypes.STRING,
490       allowNull: true,
491     },
492     template: {
493       type: DataTypes.STRING,
494       allowNull: true,
495     },
496     videosupport: {
497       type: DataTypes.ENUM('yes', 'no', 'always'),
498       allowNull: true,
499       defaultValue: 'no'
500     },
501     maxcallbitrate: {
502       type: DataTypes.INTEGER(11),
503       allowNull: true,
504     },
505     rfc2833compensate: {
506       type: DataTypes.ENUM('yes', 'no'),
507       allowNull: true,
508     },
509     mailbox: {
510       type: DataTypes.STRING,
511       allowNull: true,
512     },
513     session_timers: {
514       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
515       allowNull: true,
516     },
517     session_expires: {
518       type: DataTypes.INTEGER(11),
519       allowNull: true,
520     },
521     session_minse: {
522       type: DataTypes.INTEGER(11),
523       allowNull: true,
524     },
525     session_refresher: {
526       type: DataTypes.ENUM('uac', 'uas'),
527       allowNull: true,
528       defaultValue: 'uas'
529     },
530     t38pt_usertpsource: {
531       type: DataTypes.STRING,
532       allowNull: true,
533     },
534     regexten: {
535       type: DataTypes.STRING,
536       allowNull: true,
537     },
538     fromdomain: {
539       type: DataTypes.STRING,
540       allowNull: true,
541     },
542     fromuser: {
543       type: DataTypes.STRING,
544       allowNull: true,
545     },
546     qualify: {
547       type: DataTypes.ENUM('yes', 'no'),
548       allowNull: true,
549       defaultValue: 'yes'
550     },
551     keepalive: {
552       type: DataTypes.INTEGER(11),
553       allowNull: true,
554     },
555     defaultip: {
556       type: DataTypes.STRING,
557       allowNull: true,
558     },
559     defaultuser: {
560       type: DataTypes.STRING,
561       allowNull: true,
562     },
563     rtptimeout: { // Terminate call if 60 seconds of no RTP or RTCP activity on the audio channel  when we're not on hold.
564       type: DataTypes.INTEGER(11),
565       allowNull: true,
566     },
567     rtpholdtimeout: { // Terminate call if 300 seconds of no RTP or RTCP activity on the audio channel when we're on hold (must be > rtptimeout)
568       type: DataTypes.INTEGER(11),
569       allowNull: true,
570     },
571     rtpkeepalive: { // Send keepalives in the RTP stream to keep NAT open (default is off - zero)
572       type: DataTypes.INTEGER(11),
573       allowNull: true,
574     },
575     sendrpid: {
576       type: DataTypes.ENUM('yes', 'no'),
577       allowNull: true,
578       defaultValue: 'no'
579     },
580     outboundproxy: {
581       type: DataTypes.STRING,
582       allowNull: true,
583     },
584     callbackextension: {
585       type: DataTypes.STRING,
586       allowNull: true,
587     },
588     timert1: {
589       type: DataTypes.INTEGER(11),
590       allowNull: true,
591     },
592     timerb: {
593       type: DataTypes.INTEGER(11),
594       allowNull: true,
595     },
596     qualifyfreq: {
597       type: DataTypes.INTEGER(11),
598       allowNull: true,
599     },
600     contactpermit: {
601       type: DataTypes.STRING,
602       allowNull: true,
603     },
604     contactdeny: {
605       type: DataTypes.STRING,
606       allowNull: true,
607     },
608     contactacl: {
609       type: DataTypes.STRING,
610       allowNull: true,
611     },
612     unsolicited_mailbox: {
613       type: DataTypes.STRING,
614       allowNull: true,
615     },
616     use_q850_reason: {
617       type: DataTypes.STRING,
618       allowNull: true,
619     },
620     maxforwards: {
621       type: DataTypes.INTEGER(11),
622       allowNull: true,
623     },
624     encryption: {
625       type: DataTypes.ENUM('yes', 'no'),
626       allowNull: true,
627       defaultValue: 'no'
628     },
629     avpf: {
630       type: DataTypes.ENUM('yes', 'no'),
631       allowNull: true
632     },
633     force_avp: {
634       type: DataTypes.ENUM('yes', 'no'),
635       allowNull: true
636     },
637     icesupport: {
638       type: DataTypes.ENUM('yes', 'no'),
639       allowNull: true
640     },
641     dtlsenable: {
642       type: DataTypes.ENUM('yes', 'no'),
643       allowNull: true
644     },
645     dtlsverify: {
646       type: DataTypes.ENUM('yes', 'no', 'fingerprint', 'certificate'),
647       allowNull: true
648     },
649     dtlsrekey: {
650       type: DataTypes.INTEGER(11),
651       allowNull: true,
652     },
653     dtlscertfile: {
654       type: DataTypes.STRING,
655       allowNull: true,
656     },
657     dtlsprivatekey: {
658       type: DataTypes.STRING,
659       allowNull: true,
660     },
661     dtlscipher: {
662       type: DataTypes.STRING,
663       allowNull: true,
664     },
665     dtlscafile: {
666       type: DataTypes.STRING,
667       allowNull: true,
668     },
669     dtlscapath: {
670       type: DataTypes.STRING,
671       allowNull: true,
672     },
673     dtlssetup: {
674       type: DataTypes.ENUM('active', 'passive', 'actpass'),
675       allowNull: true
676     },
677     dtlsfingerprint: {
678       type: DataTypes.STRING,
679       allowNull: true,
680     },
681     usereqphone: { //This provider requires ";user=phone" on URI
682       type: DataTypes.ENUM('yes', 'no'),
683       allowNull: true,
684       defaultValue: 'no'
685     },
686     recordonfeature: { //Feature to use when INFO with Record: on is received.
687       type: DataTypes.STRING,
688       allowNull: true,
689     },
690     recordofffeature: { //Feature to use when INFO with Record: off is received.
691       type: DataTypes.STRING,
692       allowNull: true,
693     },
694     call_limit: {
695       type: DataTypes.INTEGER(11),
696       allowNull: true,
697       defaultValue: 10
698     },
699     registertrying: { //Send a 100 Trying when the device registers.
700       type: DataTypes.ENUM('yes', 'no'),
701       allowNull: true,
702     },
703     subscribemwi: { //Only send notifications if this phone subscribes for mailbox notification
704       type: DataTypes.ENUM('yes', 'no'),
705       allowNull: true,
706     },
707     vmexten: { // dialplan extension to reach mailbox. defaults to global vmexten which defaults to "asterisk"
708       type: DataTypes.STRING,
709       allowNull: true,
710     },
711     mohinterpret: { // This option specifies a preference for which music on hold class this channel should listen to when put on hold
712       type: DataTypes.STRING,
713       allowNull: true,
714     },
715     mohsuggest: { //  This option specifies which music on hold class to suggest to the peer channel when this channel places the peer on hold.
716       type: DataTypes.STRING,
717       allowNull: true,
718     },
719     parkinglot: {
720       type: DataTypes.STRING,
721       allowNull: true,
722     },
723     canreinvite: {
724       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'update,nonat'),
725       allowNull: true,
726       defaultValue: 'no'
727     },
728     loginInPause: {
729       type: DataTypes.BOOLEAN,
730       defaultValue: false
731     },
732     resetPasswordToken: {
733       type: DataTypes.STRING
734     },
735     resetPasswordExpires: {
736       type: DataTypes.DATE
737     },
738     phoneBarEnableRecording: {
739       type: DataTypes.BOOLEAN,
740       defaultValue: true
741     },
742     showWebBar: {
743       type: DataTypes.BOOLEAN,
744       defaultValue: false
745     },
746     phoneBarShowOmniDesktop: {
747       type: DataTypes.BOOLEAN,
748       defaultValue: true
749     },
750     phoneBarRingInUse: {
751       type: DataTypes.BOOLEAN,
752       defaultValue: false
753     }
754   }, {
755     tableName: 'users',
756     instanceMethods: {
757       /**
758        * Authenticate - check if the passwords are the same
759        *
760        * @param {String} plainText
761        *        {function} callBack
762        * @api public
763        */
764       authenticate: function(plainText) {
765         return this.encryptPassword(plainText) === this.password;
766       },
767       /**
768        * Make salt
769        *
770        * @return {String}
771        * @api public
772        */
773       makeSalt: function() {
774         return crypto.randomBytes(16).toString('base64');
775       },
776       /**
777        * Encrypt password
778        *
779        * @param {String} password
780        * @return {String}
781        * @api public
782        */
783       encryptPassword: function(password) {
784         if (!password || !this.salt) return '';
785         var salt = new Buffer(this.salt, 'base64');
786         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
787           'base64');
788       },
789       /**
790        * md5 password
791        *
792        * @param {String} password
793        * @return {String}
794        * @api public
795        */
796       md5Password: function(password) {
797         if (!password) return '';
798         return md5(password);
799       }
800     },
801     associate: function(models) {
802       // BELOGNS TO MANY
803       User.hasMany(models.ChatMessage);
804       User.hasMany(models.MailMessage);
805       User.hasMany(models.Contact);
806       User.hasMany(models.Action);
807       User.belongsToMany(models.Module, {
808         through: models.UserHasModule,
809         required: false
810       });
811       User.belongsToMany(models.MailRoom, {
812         through: models.UserHasMailRoom,
813       });
814       User.belongsToMany(models.Channel, {
815         through: 'user_has_channels'
816       });
817       User.belongsToMany(models.SmsRoom, {
818         through: 'user_has_sms_rooms'
819       });
820       User.belongsToMany(models.OpenchannelRoom, {
821         through: 'user_has_openchannel_rooms'
822       });
823       User.belongsToMany(models.FaxRoom, {
824         through: 'user_has_fax_rooms'
825       });
826       User.belongsToMany(models.Team, {
827         through: models.UserHasTeam
828       });
829       User.belongsToMany(models.VoiceQueue, {
830         through: models.UserHasVoiceQueue,
831         required: false
832       });
833       User.belongsToMany(models.VoiceQueue, {
834         through: models.UserHasVoiceQueuePermit,
835         as: 'PVoiceQueues'
836       });
837       User.belongsToMany(models.MailQueue, {
838         through: models.UserHasMailQueue,
839         required: false
840       });
841       User.belongsToMany(models.SmsQueue, {
842         through: models.UserHasSmsQueue,
843         required: false
844       });
845       User.belongsToMany(models.SmsQueue, {
846         through: models.UserHasSmsQueuePermit,
847         as: 'PSmsQueues'
848       });
849       User.belongsToMany(models.OpenchannelQueue, {
850         through: models.UserHasOpenchannelQueue,
851         required: false
852       });
853       User.belongsToMany(models.OpenchannelQueue, {
854         through: models.UserHasOpenchannelQueuePermit,
855         as: 'POpenchannelQueues'
856       });
857       User.belongsToMany(models.MailQueue, {
858         through: models.UserHasMailQueuePermit,
859         as: 'PMailQueues'
860       });
861       User.belongsToMany(models.FaxQueue, {
862         through: models.UserHasFaxQueue,
863         required: false
864       });
865       User.belongsToMany(models.FaxQueue, {
866         through: models.UserHasFaxQueuePermit,
867         as: 'PFaxQueues'
868       });
869       User.belongsToMany(models.ChatQueue, {
870         through: models.UserHasChatQueue,
871         required: false
872       });
873       User.belongsToMany(models.ChatQueue, {
874         through: models.UserHasChatQueuePermit,
875         as: 'PChatQueues'
876       });
877       User.belongsToMany(models.ChatRoom, {
878         through: models.UserHasChatRoom
879       });
880       User.belongsToMany(models.List, {
881         through: models.UserHasList
882       });
883       User.hasMany(models.VoiceExtension, {
884         foreignKey: 'UserId',
885         as: 'UserExtensions',
886         onDelete: 'cascade',
887         hooks: true
888       });
889
890       // SCOPES MANAGEMENT
891       User.addScope('all', {
892         attributes: ['id',
893           'name',
894           'email',
895           'internal',
896           'fullname',
897           'role',
898           'online',
899           'userpic',
900           'accountcode',
901           'transport',
902           'host',
903           'role',
904           'nat',
905           'type',
906           'allow',
907           'lastLoginAt',
908           'ipaddr',
909           'fullcontact',
910           'port',
911           'lastms',
912           'description',
913           'callgroup',
914           'pickupgroup'
915         ]
916       });
917
918       // SCOPES MANAGEMENT
919       User.addScope('user', {
920         where: {
921           role: {
922             $in: ['admin', 'user']
923           },
924         },
925         attributes: ['id',
926           'name',
927           'email',
928           'internal',
929           'fullname',
930           'role',
931           'userpic',
932           'accountcode',
933           'transport',
934           'host',
935           'role',
936           'nat',
937           'type',
938           'allow',
939           'lastLoginAt',
940           'ipaddr',
941           'fullcontact',
942           'port',
943           'lastms',
944           'description',
945           'callgroup',
946           'pickupgroup'
947         ]
948       });
949       User.addScope('agent', {
950         where: {
951           role: 'agent'
952         },
953         attributes: ['id',
954           'name',
955           'email',
956           'internal',
957           'fullname',
958           'role',
959           'userpic',
960           'accountcode',
961           'transport',
962           'host',
963           'nat',
964           'type',
965           'allow',
966           'chatCapacity',
967           'mailCapacity',
968           'faxCapacity',
969           'smsCapacity',
970           'openchannelCapacity',
971           'online',
972           'lastLoginAt',
973           'phoneBarAutoAnswer',
974           'phoneBarEnableSettings',
975           'phoneBarUnconditional',
976           'phoneBarNoReply',
977           'phoneBarBusy',
978           'phoneBarUnconditionalNumber',
979           'phoneBarNoReplyNumber',
980           'phoneBarBusyNumber',
981           'phoneBarListenPort',
982           'phoneBarExpires',
983           'phoneBarRemoteControl',
984           'phoneBarRemoteControlPort',
985           'phoneBarEnableRecording',
986           'chanspy',
987           'voicePause',
988           'mailPause',
989           'faxPause',
990           'chatPause',
991           'smsPause',
992           'openchannelPause',
993           'pauseType',
994           'lastPauseAt',
995           'status',
996           'statusAt',
997           'queueStatus',
998           'queueStatusAt',
999           'lastQueue',
1000           'useragent',
1001           'ipaddr',
1002           'fullcontact',
1003           'port',
1004           'lastms',
1005           'description',
1006           'loginInPause',
1007           'showWebBar',
1008           'callgroup',
1009           'pickupgroup',
1010           'phoneBarShowOmniDesktop',
1011           'phoneBarRingInUse',
1012           'phone',
1013           'mobile'
1014         ]
1015       });
1016       User.addScope('telephone', {
1017         where: {
1018           role: 'telephone'
1019         }
1020       });
1021       User.addScope('queues', {
1022         include: [{
1023           model: models.VoiceQueue,
1024           required: false
1025         }, {
1026           model: models.ChatQueue,
1027           required: false
1028         }, {
1029           model: models.MailQueue,
1030           required: false
1031         }, {
1032           model: models.FaxQueue,
1033           required: false
1034         }, {
1035           model: models.SmsQueue,
1036           required: false
1037         }, {
1038           model: models.OpenchannelQueue,
1039           required: false
1040         }]
1041       });
1042
1043       User.addScope('checkPauseStatus', function(query) {
1044         var scope = {
1045           where: {}
1046         };
1047         if (query.voicePause) {
1048           scope.where.voicePause = (query.voicePause === 'true') ?
1049             true : false;
1050           delete query.voicePause;
1051         } else if (query.faxPause) {
1052           scope.where.faxPause = (query.faxPause === 'true') ? true :
1053             false;
1054           delete query.faxPause;
1055         } else if (query.chatPause) {
1056           scope.where.chatPause = (query.chatPause === 'true') ?
1057             true : false;
1058           delete query.chatPause;
1059         } else if (query.mailPause) {
1060           scope.where.mailPause = (query.mailPause === 'true') ?
1061             true : false;
1062           delete query.mailPause;
1063         } else if (query.smsPause) {
1064           scope.where.smsPause = (query.smsPause === 'true') ?
1065             true : false;
1066           delete query.smsPause;
1067         } else if (query.openchannelPause) {
1068           scope.where.openchannelPause = (query.openchannelPause === 'true') ?
1069             true : false;
1070           delete query.openchannelPause;
1071         }
1072         return scope;
1073       });
1074       User.addScope('checkOnlineStatus', function(query) {
1075         var scope = {
1076           where: {}
1077         };
1078         if (query.online) {
1079           scope.where.online = (query.online === 'true') ? true :
1080             false;
1081           delete query.online;
1082         }
1083         return scope;
1084       });
1085       User.addScope('checkSipStatus', function(query) {
1086         var scope = {
1087           where: {}
1088         };
1089         if (query.status) {
1090           scope.where.status = query.status;
1091           delete query.status;
1092         }
1093         return scope;
1094       });
1095       User.addScope('checkQueueStatus', function(query) { //to be executed before checkOnlineStatus
1096         var scope = {
1097           where: {}
1098         };
1099         if (query.queueStatus) {
1100           if (query.queueStatus === 'paused') {
1101             scope.where.voicePause = true;
1102           } else {
1103             scope.where.queueStatus = query.queueStatus;
1104             scope.where.voicePause = false;
1105             query.online = 'true';
1106           }
1107           delete query.queueStatus;
1108         }
1109         return scope;
1110       });
1111
1112       User.addScope('me', function(userId) {
1113         var scope = {
1114           where: {
1115             id: userId
1116           },
1117           attributes: ['id', 'fullname', 'name', 'email', 'role', 'userpic', 'lastLoginAt', 'voicePause',
1118             'mailPause',
1119             'chatPause',
1120             'faxPause',
1121             'smsPause',
1122             'openchannelPause',
1123             'lastPauseAt',
1124             'pauseType',
1125             'phoneBarRemoteControl',
1126             'phoneBarRemoteControlPort',
1127             'loginInPause',
1128             'showWebBar'
1129           ],
1130           include: [{
1131             model: models.Module,
1132             include: [{
1133               model: models.Module,
1134               as: 'SubModules',
1135               required: false,
1136               include: [{
1137                 all: true
1138               }]
1139             }]
1140           }, {
1141             model: models.VoiceQueue,
1142             as: 'PVoiceQueues'
1143           }, {
1144             model: models.ChatQueue,
1145             as: 'PChatQueues'
1146           }, {
1147             model: models.MailQueue,
1148             as: 'PMailQueues'
1149           }, {
1150             model: models.FaxQueue,
1151             as: 'PFaxQueues'
1152           }, {
1153             model: models.SmsQueue,
1154             as: 'PSmsQueues'
1155           }]
1156         };
1157         return scope;
1158       });
1159     }
1160   });
1161
1162   return User;
1163 }