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