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