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