d18d8e2ce4cad0d044c34545f70cee65a349f2aa
[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: 'alaw;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   }, {
751     tableName: 'users',
752     instanceMethods: {
753       /**
754        * Authenticate - check if the passwords are the same
755        *
756        * @param {String} plainText
757        *        {function} callBack
758        * @api public
759        */
760       authenticate: function(plainText) {
761         return this.encryptPassword(plainText) === this.password;
762       },
763       /**
764        * Make salt
765        *
766        * @return {String}
767        * @api public
768        */
769       makeSalt: function() {
770         return crypto.randomBytes(16).toString('base64');
771       },
772       /**
773        * Encrypt password
774        *
775        * @param {String} password
776        * @return {String}
777        * @api public
778        */
779       encryptPassword: function(password) {
780         if (!password || !this.salt) return '';
781         var salt = new Buffer(this.salt, 'base64');
782         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
783           'base64');
784       },
785       /**
786        * md5 password
787        *
788        * @param {String} password
789        * @return {String}
790        * @api public
791        */
792       md5Password: function(password) {
793         if (!password) return '';
794         return md5(password);
795       }
796     },
797     associate: function(models) {
798       // BELOGNS TO MANY
799       User.hasMany(models.ChatMessage);
800       User.hasMany(models.MailMessage);
801       User.hasMany(models.Contact);
802       User.hasMany(models.Action);
803       User.belongsToMany(models.Module, {
804         through: models.UserHasModule,
805         required: false
806       });
807       User.belongsToMany(models.MailRoom, {
808         through: models.UserHasMailRoom,
809       });
810       User.belongsToMany(models.Channel, {
811         through: 'user_has_channels'
812       });
813       User.belongsToMany(models.SmsRoom, {
814         through: 'user_has_sms_rooms'
815       });
816       User.belongsToMany(models.OpenchannelRoom, {
817         through: 'user_has_openchannel_rooms'
818       });
819       User.belongsToMany(models.FaxRoom, {
820         through: 'user_has_fax_rooms'
821       });
822       User.belongsToMany(models.Team, {
823         through: models.UserHasTeam
824       });
825       User.belongsToMany(models.VoiceQueue, {
826         through: models.UserHasVoiceQueue,
827         required: false
828       });
829       User.belongsToMany(models.VoiceQueue, {
830         through: models.UserHasVoiceQueuePermit,
831         as: 'PVoiceQueues'
832       });
833       User.belongsToMany(models.MailQueue, {
834         through: models.UserHasMailQueue,
835         required: false
836       });
837       User.belongsToMany(models.SmsQueue, {
838         through: models.UserHasSmsQueue,
839         required: false
840       });
841       User.belongsToMany(models.SmsQueue, {
842         through: models.UserHasSmsQueuePermit,
843         as: 'PSmsQueues'
844       });
845       User.belongsToMany(models.OpenchannelQueue, {
846         through: models.UserHasOpenchannelQueue,
847         required: false
848       });
849       User.belongsToMany(models.OpenchannelQueue, {
850         through: models.UserHasOpenchannelQueuePermit,
851         as: 'POpenchannelQueues'
852       });
853       User.belongsToMany(models.MailQueue, {
854         through: models.UserHasMailQueuePermit,
855         as: 'PMailQueues'
856       });
857       User.belongsToMany(models.FaxQueue, {
858         through: models.UserHasFaxQueue,
859         required: false
860       });
861       User.belongsToMany(models.FaxQueue, {
862         through: models.UserHasFaxQueuePermit,
863         as: 'PFaxQueues'
864       });
865       User.belongsToMany(models.ChatQueue, {
866         through: models.UserHasChatQueue,
867         required: false
868       });
869       User.belongsToMany(models.ChatQueue, {
870         through: models.UserHasChatQueuePermit,
871         as: 'PChatQueues'
872       });
873       User.belongsToMany(models.ChatRoom, {
874         through: models.UserHasChatRoom
875       });
876       User.belongsToMany(models.List, {
877         through: models.UserHasList
878       });
879       User.hasMany(models.VoiceExtension, {
880         foreignKey: 'UserId',
881         as: 'UserExtensions',
882         onDelete: 'cascade',
883         hooks: true
884       });
885
886       // SCOPES MANAGEMENT
887       User.addScope('all', {
888         attributes: ['id',
889           'name',
890           'email',
891           'internal',
892           'fullname',
893           'role',
894           'online',
895           'userpic',
896           'accountcode',
897           'transport',
898           'host',
899           'role',
900           'nat',
901           'type',
902           'allow',
903           'lastLoginAt',
904           'ipaddr',
905           'fullcontact',
906           'port',
907           'lastms',
908           'description',
909           'callgroup',
910           'pickupgroup'
911         ]
912       });
913
914       // SCOPES MANAGEMENT
915       User.addScope('user', {
916         where: {
917           role: {
918             $in: ['admin', 'user']
919           },
920         },
921         attributes: ['id',
922           'name',
923           'email',
924           'internal',
925           'fullname',
926           'role',
927           'userpic',
928           'accountcode',
929           'transport',
930           'host',
931           'role',
932           'nat',
933           'type',
934           'allow',
935           'lastLoginAt',
936           'ipaddr',
937           'fullcontact',
938           'port',
939           'lastms',
940           'description',
941           'callgroup',
942           'pickupgroup'
943         ]
944       });
945       User.addScope('agent', {
946         where: {
947           role: 'agent'
948         },
949         attributes: ['id',
950           'name',
951           'email',
952           'internal',
953           'fullname',
954           'role',
955           'userpic',
956           'accountcode',
957           'transport',
958           'host',
959           'nat',
960           'type',
961           'allow',
962           'chatCapacity',
963           'mailCapacity',
964           'faxCapacity',
965           'smsCapacity',
966           'openchannelCapacity',
967           'online',
968           'lastLoginAt',
969           'phoneBarAutoAnswer',
970           'phoneBarEnableSettings',
971           'phoneBarUnconditional',
972           'phoneBarNoReply',
973           'phoneBarBusy',
974           'phoneBarUnconditionalNumber',
975           'phoneBarNoReplyNumber',
976           'phoneBarBusyNumber',
977           'phoneBarListenPort',
978           'phoneBarExpires',
979           'phoneBarRemoteControl',
980           'phoneBarRemoteControlPort',
981           'phoneBarEnableRecording',
982           'chanspy',
983           'voicePause',
984           'mailPause',
985           'faxPause',
986           'chatPause',
987           'smsPause',
988           'openchannelPause',
989           'pauseType',
990           'lastPauseAt',
991           'status',
992           'statusAt',
993           'queueStatus',
994           'queueStatusAt',
995           'lastQueue',
996           'useragent',
997           'ipaddr',
998           'fullcontact',
999           'port',
1000           'lastms',
1001           'description',
1002           'loginInPause',
1003           'showWebBar',
1004           'callgroup',
1005           'pickupgroup',
1006           'phoneBarShowOmniDesktop'
1007         ]
1008       });
1009       User.addScope('telephone', {
1010         where: {
1011           role: 'telephone'
1012         }
1013       });
1014       User.addScope('queues', {
1015         include: [{
1016           model: models.VoiceQueue,
1017           required: false
1018         }, {
1019           model: models.ChatQueue,
1020           required: false
1021         }, {
1022           model: models.MailQueue,
1023           required: false
1024         }, {
1025           model: models.FaxQueue,
1026           required: false
1027         }, {
1028           model: models.SmsQueue,
1029           required: false
1030         }, {
1031           model: models.OpenchannelQueue,
1032           required: false
1033         }]
1034       });
1035
1036       User.addScope('checkPauseStatus', function(query) {
1037         var scope = {
1038           where: {}
1039         };
1040         if (query.voicePause) {
1041           scope.where.voicePause = (query.voicePause === 'true') ?
1042             true : false;
1043           delete query.voicePause;
1044         } else if (query.faxPause) {
1045           scope.where.faxPause = (query.faxPause === 'true') ? true :
1046             false;
1047           delete query.faxPause;
1048         } else if (query.chatPause) {
1049           scope.where.chatPause = (query.chatPause === 'true') ?
1050             true : false;
1051           delete query.chatPause;
1052         } else if (query.mailPause) {
1053           scope.where.mailPause = (query.mailPause === 'true') ?
1054             true : false;
1055           delete query.mailPause;
1056         } else if (query.smsPause) {
1057           scope.where.smsPause = (query.smsPause === 'true') ?
1058             true : false;
1059           delete query.smsPause;
1060         } else if (query.openchannelPause) {
1061           scope.where.openchannelPause = (query.openchannelPause === 'true') ?
1062             true : false;
1063           delete query.openchannelPause;
1064         }
1065         return scope;
1066       });
1067       User.addScope('checkOnlineStatus', function(query) {
1068         var scope = {
1069           where: {}
1070         };
1071         if (query.online) {
1072           scope.where.online = (query.online === 'true') ? true :
1073             false;
1074           delete query.online;
1075         }
1076         return scope;
1077       });
1078       User.addScope('checkSipStatus', function(query) {
1079         var scope = {
1080           where: {}
1081         };
1082         if (query.status) {
1083           scope.where.status = query.status;
1084           delete query.status;
1085         }
1086         return scope;
1087       });
1088       User.addScope('checkQueueStatus', function(query) { //to be executed before checkOnlineStatus
1089         var scope = {
1090           where: {}
1091         };
1092         if (query.queueStatus) {
1093           if (query.queueStatus === 'paused') {
1094             scope.where.voicePause = true;
1095           } else {
1096             scope.where.queueStatus = query.queueStatus;
1097             scope.where.voicePause = false;
1098             query.online = 'true';
1099           }
1100           delete query.queueStatus;
1101         }
1102         return scope;
1103       });
1104
1105       User.addScope('me', function(userId) {
1106         var scope = {
1107           where: {
1108             id: userId
1109           },
1110           attributes: ['id', 'fullname', 'name', 'email', 'role', 'userpic', 'lastLoginAt', 'voicePause',
1111             'mailPause',
1112             'chatPause',
1113             'faxPause',
1114             'smsPause',
1115             'openchannelPause',
1116             'lastPauseAt',
1117             'pauseType',
1118             'phoneBarRemoteControl',
1119             'phoneBarRemoteControlPort',
1120             'loginInPause',
1121             'showWebBar'
1122           ],
1123           include: [{
1124             model: models.Module,
1125             include: [{
1126               model: models.Module,
1127               as: 'SubModules',
1128               required: false,
1129               include: [{
1130                 all: true
1131               }]
1132             }]
1133           }, {
1134             model: models.VoiceQueue,
1135             as: 'PVoiceQueues'
1136           }, {
1137             model: models.ChatQueue,
1138             as: 'PChatQueues'
1139           }, {
1140             model: models.MailQueue,
1141             as: 'PMailQueues'
1142           }, {
1143             model: models.FaxQueue,
1144             as: 'PFaxQueues'
1145           }, {
1146             model: models.SmsQueue,
1147             as: 'PSmsQueues'
1148           }]
1149         };
1150         return scope;
1151       });
1152     }
1153   });
1154
1155   return User;
1156 }