Built motion from commit 712a57186.|1.0.37
[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     },
423     trustrpid: {
424       type: DataTypes.ENUM('yes', 'no'),
425       allowNull: true,
426       defaultValue: 'no'
427     },
428     trust_id_outbound: {
429       type: DataTypes.ENUM('yes', 'no'),
430       allowNull: true,
431       defaultValue: 'no'
432     },
433     progressinband: {
434       type: DataTypes.ENUM('yes', 'no', 'never'),
435       allowNull: true,
436     },
437     promiscredir: {
438       type: DataTypes.ENUM('yes', 'no'),
439       allowNull: true,
440     },
441     useclientcode: {
442       type: DataTypes.ENUM('yes', 'no'),
443       allowNull: true,
444     },
445     accountcode: {
446       type: DataTypes.INTEGER(11),
447       allowNull: true,
448     },
449     setvar: {
450       type: DataTypes.STRING,
451       allowNull: true,
452     },
453     callerid: {
454       type: DataTypes.STRING,
455       allowNull: true,
456       defaultValue: '"" <>'
457     },
458     amaflags: {
459       type: DataTypes.STRING,
460       allowNull: true,
461     },
462     callcounter: {
463       type: DataTypes.ENUM('yes', 'no'),
464       allowNull: true,
465       defaultValue: 'yes'
466     },
467     busylevel: {
468       type: DataTypes.INTEGER(11),
469       allowNull: true,
470     },
471     allowoverlap: {
472       type: DataTypes.ENUM('yes', 'no'),
473       allowNull: true,
474     },
475     allowsubscribe: {
476       type: DataTypes.ENUM('yes', 'no'),
477       allowNull: true,
478     },
479     allowtransfer: {
480       type: DataTypes.ENUM('yes', 'no'),
481       allowNull: true,
482     },
483     ignoresdpversion: {
484       type: DataTypes.ENUM('yes', 'no'),
485       allowNull: true,
486     },
487     subscribecontext: {
488       type: DataTypes.STRING,
489       allowNull: true,
490     },
491     template: {
492       type: DataTypes.STRING,
493       allowNull: true,
494     },
495     videosupport: {
496       type: DataTypes.ENUM('yes', 'no', 'always'),
497       allowNull: true,
498       defaultValue: 'no'
499     },
500     maxcallbitrate: {
501       type: DataTypes.INTEGER(11),
502       allowNull: true,
503     },
504     rfc2833compensate: {
505       type: DataTypes.ENUM('yes', 'no'),
506       allowNull: true,
507     },
508     mailbox: {
509       type: DataTypes.STRING,
510       allowNull: true,
511     },
512     session_timers: {
513       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
514       allowNull: true,
515     },
516     session_expires: {
517       type: DataTypes.INTEGER(11),
518       allowNull: true,
519     },
520     session_minse: {
521       type: DataTypes.INTEGER(11),
522       allowNull: true,
523     },
524     session_refresher: {
525       type: DataTypes.ENUM('uac', 'uas'),
526       allowNull: true,
527       defaultValue: 'uas'
528     },
529     t38pt_usertpsource: {
530       type: DataTypes.STRING,
531       allowNull: true,
532     },
533     regexten: {
534       type: DataTypes.STRING,
535       allowNull: true,
536     },
537     fromdomain: {
538       type: DataTypes.STRING,
539       allowNull: true,
540     },
541     fromuser: {
542       type: DataTypes.STRING,
543       allowNull: true,
544     },
545     qualify: {
546       type: DataTypes.ENUM('yes', 'no'),
547       allowNull: true,
548       defaultValue: 'yes'
549     },
550     keepalive: {
551       type: DataTypes.INTEGER(11),
552       allowNull: true,
553     },
554     defaultip: {
555       type: DataTypes.STRING,
556       allowNull: true,
557     },
558     defaultuser: {
559       type: DataTypes.STRING,
560       allowNull: true,
561     },
562     rtptimeout: { // Terminate call if 60 seconds of no RTP or RTCP activity on the audio channel  when we're not on hold.
563       type: DataTypes.INTEGER(11),
564       allowNull: true,
565     },
566     rtpholdtimeout: { // Terminate call if 300 seconds of no RTP or RTCP activity on the audio channel when we're on hold (must be > rtptimeout)
567       type: DataTypes.INTEGER(11),
568       allowNull: true,
569     },
570     rtpkeepalive: { // Send keepalives in the RTP stream to keep NAT open (default is off - zero)
571       type: DataTypes.INTEGER(11),
572       allowNull: true,
573     },
574     sendrpid: {
575       type: DataTypes.ENUM('yes', 'no'),
576       allowNull: true,
577       defaultValue: 'no'
578     },
579     outboundproxy: {
580       type: DataTypes.STRING,
581       allowNull: true,
582     },
583     callbackextension: {
584       type: DataTypes.STRING,
585       allowNull: true,
586     },
587     timert1: {
588       type: DataTypes.INTEGER(11),
589       allowNull: true,
590     },
591     timerb: {
592       type: DataTypes.INTEGER(11),
593       allowNull: true,
594     },
595     qualifyfreq: {
596       type: DataTypes.INTEGER(11),
597       allowNull: true,
598     },
599     contactpermit: {
600       type: DataTypes.STRING,
601       allowNull: true,
602     },
603     contactdeny: {
604       type: DataTypes.STRING,
605       allowNull: true,
606     },
607     contactacl: {
608       type: DataTypes.STRING,
609       allowNull: true,
610     },
611     unsolicited_mailbox: {
612       type: DataTypes.STRING,
613       allowNull: true,
614     },
615     use_q850_reason: {
616       type: DataTypes.STRING,
617       allowNull: true,
618     },
619     maxforwards: {
620       type: DataTypes.INTEGER(11),
621       allowNull: true,
622     },
623     encryption: {
624       type: DataTypes.ENUM('yes', 'no'),
625       allowNull: true,
626       defaultValue: 'no'
627     },
628     avpf: {
629       type: DataTypes.ENUM('yes', 'no'),
630       allowNull: true
631     },
632     force_avp: {
633       type: DataTypes.ENUM('yes', 'no'),
634       allowNull: true
635     },
636     icesupport: {
637       type: DataTypes.ENUM('yes', 'no'),
638       allowNull: true
639     },
640     dtlsenable: {
641       type: DataTypes.ENUM('yes', 'no'),
642       allowNull: true
643     },
644     dtlsverify: {
645       type: DataTypes.ENUM('yes', 'no', 'fingerprint', 'certificate'),
646       allowNull: true
647     },
648     dtlsrekey: {
649       type: DataTypes.INTEGER(11),
650       allowNull: true,
651     },
652     dtlscertfile: {
653       type: DataTypes.STRING,
654       allowNull: true,
655     },
656     dtlsprivatekey: {
657       type: DataTypes.STRING,
658       allowNull: true,
659     },
660     dtlscipher: {
661       type: DataTypes.STRING,
662       allowNull: true,
663     },
664     dtlscafile: {
665       type: DataTypes.STRING,
666       allowNull: true,
667     },
668     dtlscapath: {
669       type: DataTypes.STRING,
670       allowNull: true,
671     },
672     dtlssetup: {
673       type: DataTypes.ENUM('active', 'passive', 'actpass'),
674       allowNull: true
675     },
676     dtlsfingerprint: {
677       type: DataTypes.STRING,
678       allowNull: true,
679     },
680     usereqphone: { //This provider requires ";user=phone" on URI
681       type: DataTypes.ENUM('yes', 'no'),
682       allowNull: true,
683       defaultValue: 'no'
684     },
685     recordonfeature: { //Feature to use when INFO with Record: on is received.
686       type: DataTypes.STRING,
687       allowNull: true,
688     },
689     recordofffeature: { //Feature to use when INFO with Record: off is received.
690       type: DataTypes.STRING,
691       allowNull: true,
692     },
693     call_limit: {
694       type: DataTypes.INTEGER(11),
695       allowNull: true,
696       defaultValue: 10
697     },
698     registertrying: { //Send a 100 Trying when the device registers.
699       type: DataTypes.ENUM('yes', 'no'),
700       allowNull: true,
701     },
702     subscribemwi: { //Only send notifications if this phone subscribes for mailbox notification
703       type: DataTypes.ENUM('yes', 'no'),
704       allowNull: true,
705     },
706     vmexten: { // dialplan extension to reach mailbox. defaults to global vmexten which defaults to "asterisk"
707       type: DataTypes.STRING,
708       allowNull: true,
709     },
710     mohinterpret: { // This option specifies a preference for which music on hold class this channel should listen to when put on hold
711       type: DataTypes.STRING,
712       allowNull: true,
713     },
714     mohsuggest: { //  This option specifies which music on hold class to suggest to the peer channel when this channel places the peer on hold.
715       type: DataTypes.STRING,
716       allowNull: true,
717     },
718     parkinglot: {
719       type: DataTypes.STRING,
720       allowNull: true,
721     },
722     canreinvite: {
723       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'update,nonat'),
724       allowNull: true,
725       defaultValue: 'no'
726     },
727     loginInPause: {
728       type: DataTypes.BOOLEAN,
729       defaultValue: false
730     },
731     resetPasswordToken: {
732       type: DataTypes.STRING
733     },
734     resetPasswordExpires: {
735       type: DataTypes.DATE
736     },
737     phoneBarEnableRecording: {
738       type: DataTypes.BOOLEAN,
739       defaultValue: true
740     },
741     showWebBar: {
742       type: DataTypes.BOOLEAN,
743       defaultValue: false
744     },
745     phoneBarShowOmniDesktop: {
746       type: DataTypes.BOOLEAN,
747       defaultValue: true
748     },
749     phoneBarRingInUse: {
750       type: DataTypes.BOOLEAN,
751       defaultValue: false
752     },
753     phoneBarDnd: {
754       type: DataTypes.BOOLEAN,
755       defaultValue: true
756     },
757     phoneBarUnansweredCallBadge: {
758       type: DataTypes.BOOLEAN,
759       defaultValue: true
760     },
761     phonebarEnableDtmfTone: {
762       type: DataTypes.BOOLEAN,
763       defaultValue: false
764     },
765     phonebarAutoAnswerDelay: {
766       type: DataTypes.INTEGER,
767       defaultValue: 0
768     }
769   }, {
770     tableName: 'users',
771     charset: 'utf8',
772     collate: 'utf8_bin',
773     instanceMethods: {
774       /**
775        * Authenticate - check if the passwords are the same
776        *
777        * @param {String} plainText
778        *        {function} callBack
779        * @api public
780        */
781       authenticate: function(plainText) {
782         return this.encryptPassword(plainText) === this.password;
783       },
784       /**
785        * Make salt
786        *
787        * @return {String}
788        * @api public
789        */
790       makeSalt: function() {
791         return crypto.randomBytes(16).toString('base64');
792       },
793       /**
794        * Encrypt password
795        *
796        * @param {String} password
797        * @return {String}
798        * @api public
799        */
800       encryptPassword: function(password) {
801         if (!password || !this.salt) return '';
802         var salt = new Buffer(this.salt, 'base64');
803         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
804           'base64');
805       },
806       /**
807        * md5 password
808        *
809        * @param {String} password
810        * @return {String}
811        * @api public
812        */
813       md5Password: function(password) {
814         if (!password) return '';
815         return md5(password);
816       }
817     },
818     associate: function(models) {
819       // BELOGNS TO MANY
820       User.hasMany(models.ChatMessage);
821       User.hasMany(models.MailMessage);
822       User.hasMany(models.Contact);
823       User.hasMany(models.Action);
824       User.belongsToMany(models.Module, {
825         through: models.UserHasModule,
826         required: false
827       });
828       User.belongsToMany(models.MailRoom, {
829         through: models.UserHasMailRoom,
830       });
831       User.belongsToMany(models.Channel, {
832         through: 'user_has_channels'
833       });
834       User.belongsToMany(models.SmsRoom, {
835         through: 'user_has_sms_rooms'
836       });
837       User.belongsToMany(models.OpenchannelRoom, {
838         through: 'user_has_openchannel_rooms'
839       });
840       User.belongsToMany(models.FaxRoom, {
841         through: 'user_has_fax_rooms'
842       });
843       User.belongsToMany(models.Team, {
844         through: models.UserHasTeam
845       });
846       User.belongsToMany(models.VoiceQueue, {
847         through: models.UserHasVoiceQueue,
848         required: false
849       });
850       User.belongsToMany(models.VoiceQueue, {
851         through: models.UserHasVoiceQueuePermit,
852         as: 'PVoiceQueues'
853       });
854       User.belongsToMany(models.MailQueue, {
855         through: models.UserHasMailQueue,
856         required: false
857       });
858       User.belongsToMany(models.SmsQueue, {
859         through: models.UserHasSmsQueue,
860         required: false
861       });
862       User.belongsToMany(models.SmsQueue, {
863         through: models.UserHasSmsQueuePermit,
864         as: 'PSmsQueues'
865       });
866       User.belongsToMany(models.OpenchannelQueue, {
867         through: models.UserHasOpenchannelQueue,
868         required: false
869       });
870       User.belongsToMany(models.OpenchannelQueue, {
871         through: models.UserHasOpenchannelQueuePermit,
872         as: 'POpenchannelQueues'
873       });
874       User.belongsToMany(models.MailQueue, {
875         through: models.UserHasMailQueuePermit,
876         as: 'PMailQueues'
877       });
878       User.belongsToMany(models.FaxQueue, {
879         through: models.UserHasFaxQueue,
880         required: false
881       });
882       User.belongsToMany(models.FaxQueue, {
883         through: models.UserHasFaxQueuePermit,
884         as: 'PFaxQueues'
885       });
886       User.belongsToMany(models.ChatQueue, {
887         through: models.UserHasChatQueue,
888         required: false
889       });
890       User.belongsToMany(models.ChatQueue, {
891         through: models.UserHasChatQueuePermit,
892         as: 'PChatQueues'
893       });
894       User.belongsToMany(models.ChatRoom, {
895         through: models.UserHasChatRoom
896       });
897       User.belongsToMany(models.List, {
898         through: models.UserHasList
899       });
900       User.hasMany(models.VoiceExtension, {
901         foreignKey: 'UserId',
902         as: 'UserExtensions',
903         onDelete: 'cascade',
904         hooks: true
905       });
906
907       // SCOPES MANAGEMENT
908       User.addScope('all', {
909         attributes: ['id',
910           'name',
911           'email',
912           'internal',
913           'fullname',
914           'role',
915           'online',
916           'userpic',
917           'accountcode',
918           'transport',
919           'host',
920           'role',
921           'nat',
922           'type',
923           'allow',
924           'lastLoginAt',
925           'ipaddr',
926           'fullcontact',
927           'port',
928           'lastms',
929           'description',
930           'callgroup',
931           'pickupgroup'
932         ]
933       });
934
935       // SCOPES MANAGEMENT
936       User.addScope('user', {
937         where: {
938           role: {
939             $in: ['admin', 'user']
940           },
941         },
942         attributes: ['id',
943           'name',
944           'email',
945           'internal',
946           'fullname',
947           'role',
948           'userpic',
949           'accountcode',
950           'transport',
951           'host',
952           'role',
953           'nat',
954           'type',
955           'allow',
956           'lastLoginAt',
957           'ipaddr',
958           'fullcontact',
959           'port',
960           'lastms',
961           'description',
962           'callgroup',
963           'pickupgroup'
964         ]
965       });
966       User.addScope('agent', {
967         where: {
968           role: 'agent'
969         },
970         attributes: ['id',
971           'name',
972           'email',
973           'internal',
974           'fullname',
975           'role',
976           'userpic',
977           'accountcode',
978           'transport',
979           'host',
980           'nat',
981           'type',
982           'allow',
983           'chatCapacity',
984           'mailCapacity',
985           'faxCapacity',
986           'smsCapacity',
987           'openchannelCapacity',
988           'online',
989           'lastLoginAt',
990           'phoneBarAutoAnswer',
991           'phoneBarEnableSettings',
992           'phoneBarUnconditional',
993           'phoneBarNoReply',
994           'phoneBarBusy',
995           'phoneBarUnconditionalNumber',
996           'phoneBarNoReplyNumber',
997           'phoneBarBusyNumber',
998           'phoneBarListenPort',
999           'phoneBarExpires',
1000           'phoneBarRemoteControl',
1001           'phoneBarRemoteControlPort',
1002           'phoneBarEnableRecording',
1003           'chanspy',
1004           'voicePause',
1005           'mailPause',
1006           'faxPause',
1007           'chatPause',
1008           'smsPause',
1009           'openchannelPause',
1010           'pauseType',
1011           'lastPauseAt',
1012           'status',
1013           'statusAt',
1014           'queueStatus',
1015           'queueStatusAt',
1016           'lastQueue',
1017           'useragent',
1018           'ipaddr',
1019           'fullcontact',
1020           'port',
1021           'lastms',
1022           'description',
1023           'loginInPause',
1024           'showWebBar',
1025           'callgroup',
1026           'pickupgroup',
1027           'phoneBarShowOmniDesktop',
1028           'phoneBarRingInUse',
1029           'phoneBarDnd',
1030           'phoneBarUnansweredCallBadge',
1031           'phone',
1032           'mobile',
1033           'phonebarEnableDtmfTone',
1034           'phonebarAutoAnswerDelay'
1035         ]
1036       });
1037       User.addScope('telephone', {
1038         where: {
1039           role: 'telephone'
1040         }
1041       });
1042       User.addScope('queues', {
1043         include: [{
1044           model: models.VoiceQueue,
1045           required: false
1046         }, {
1047           model: models.ChatQueue,
1048           required: false
1049         }, {
1050           model: models.MailQueue,
1051           required: false
1052         }, {
1053           model: models.FaxQueue,
1054           required: false
1055         }, {
1056           model: models.SmsQueue,
1057           required: false
1058         }, {
1059           model: models.OpenchannelQueue,
1060           required: false
1061         }]
1062       });
1063
1064       User.addScope('checkPauseStatus', function(query) {
1065         var scope = {
1066           where: {}
1067         };
1068         if (query.voicePause) {
1069           scope.where.voicePause = (query.voicePause === 'true') ?
1070             true : false;
1071           delete query.voicePause;
1072         } else if (query.faxPause) {
1073           scope.where.faxPause = (query.faxPause === 'true') ? true :
1074             false;
1075           delete query.faxPause;
1076         } else if (query.chatPause) {
1077           scope.where.chatPause = (query.chatPause === 'true') ?
1078             true : false;
1079           delete query.chatPause;
1080         } else if (query.mailPause) {
1081           scope.where.mailPause = (query.mailPause === 'true') ?
1082             true : false;
1083           delete query.mailPause;
1084         } else if (query.smsPause) {
1085           scope.where.smsPause = (query.smsPause === 'true') ?
1086             true : false;
1087           delete query.smsPause;
1088         } else if (query.openchannelPause) {
1089           scope.where.openchannelPause = (query.openchannelPause === 'true') ?
1090             true : false;
1091           delete query.openchannelPause;
1092         }
1093         return scope;
1094       });
1095       User.addScope('checkOnlineStatus', function(query) {
1096         var scope = {
1097           where: {}
1098         };
1099         if (query.online) {
1100           scope.where.online = (query.online === 'true') ? true :
1101             false;
1102           delete query.online;
1103         }
1104         return scope;
1105       });
1106       User.addScope('checkSipStatus', function(query) {
1107         var scope = {
1108           where: {}
1109         };
1110         if (query.status) {
1111           scope.where.status = query.status;
1112           delete query.status;
1113         }
1114         return scope;
1115       });
1116       User.addScope('checkQueueStatus', function(query) { //to be executed before checkOnlineStatus
1117         var scope = {
1118           where: {}
1119         };
1120         if (query.queueStatus) {
1121           if (query.queueStatus === 'paused') {
1122             scope.where.voicePause = true;
1123           } else {
1124             scope.where.queueStatus = query.queueStatus;
1125             scope.where.voicePause = false;
1126             query.online = 'true';
1127           }
1128           delete query.queueStatus;
1129         }
1130         return scope;
1131       });
1132
1133       User.addScope('me', function(userId) {
1134         var scope = {
1135           where: {
1136             id: userId
1137           },
1138           attributes: ['id', 'fullname', 'name', 'email', 'role', 'userpic', 'lastLoginAt', 'voicePause',
1139             'mailPause',
1140             'chatPause',
1141             'faxPause',
1142             'smsPause',
1143             'openchannelPause',
1144             'lastPauseAt',
1145             'pauseType',
1146             'phoneBarRemoteControl',
1147             'phoneBarRemoteControlPort',
1148             'loginInPause',
1149             'showWebBar',
1150             'phonebarEnableDtmfTone',
1151             'phonebarAutoAnswerDelay'
1152           ],
1153           include: [{
1154             model: models.Module,
1155             attributes: ['id', 'path'],
1156             include: [{
1157               model: models.Module,
1158               attributes: ['id', 'path'],
1159               as: 'SubModules',
1160               required: false,
1161               include: [{
1162                 model: models.Module,
1163                 attributes: ['id', 'path'],
1164                 as: 'SubModules'
1165               }]
1166             }]
1167           }]
1168         };
1169         return scope;
1170       });
1171     }
1172   });
1173
1174   return User;
1175 }