Built motion from commit 2e74e5e.|0.0.138
[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     ipaddr: { //REALTIME ASTERISK
279       type: DataTypes.STRING,
280       allowNull: true,
281     },
282     port: { //REALTIME ASTERISK
283       type: DataTypes.INTEGER(5),
284       allowNull: true,
285     },
286     regseconds: { //REALTIME ASTERISK
287       type: DataTypes.INTEGER(11),
288       allowNull: true,
289     },
290     fullcontact: { //REALTIME ASTERISK
291       type: DataTypes.STRING,
292       allowNull: true,
293     },
294     regserver: { //REALTIME ASTERISK
295       type: DataTypes.STRING,
296       allowNull: true,
297     },
298     useragent: { //REALTIME ASTERISK
299       type: DataTypes.STRING,
300       allowNull: true,
301     },
302     lastms: { //REALTIME ASTERISK
303       type: DataTypes.INTEGER(11),
304       allowNull: true,
305     },
306     type: {
307       type: DataTypes.ENUM('friend', 'user', 'peer'),
308       allowNull: true,
309       defaultValue: 'friend'
310     },
311     context: {
312       type: DataTypes.STRING,
313       allowNull: true,
314       defaultValue: 'from-sip'
315     },
316     callingpres: {
317       type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
318         'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
319         'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
320         'PROHIB_FAILED_SCREEN', 'PROHIB'),
321       allowNull: true,
322     },
323     permit: {
324       type: DataTypes.STRING,
325       allowNull: true,
326     },
327     deny: {
328       type: DataTypes.STRING,
329       allowNull: true,
330     },
331     secret: {
332       type: DataTypes.STRING,
333       allowNull: true,
334     },
335     md5secret: {
336       type: DataTypes.STRING,
337       allowNull: true,
338     },
339     remotesecret: {
340       type: DataTypes.STRING,
341       allowNull: true,
342     },
343     transport: {
344       type: DataTypes.STRING,
345       allowNull: true,
346       defaultValue: 'udp'
347     },
348     dtmfmode: {
349       type: DataTypes.ENUM('rfc2833', 'info', 'shortinfo', 'inband',
350         'auto'),
351       allowNull: true,
352       defaultValue: 'rfc2833'
353     },
354     directmedia: {
355       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update', 'outgoing'),
356       allowNull: true,
357       defaultValue: 'no'
358     },
359     directrtpsetup: {
360       type: DataTypes.ENUM('yes', 'no'),
361       allowNull: true,
362       defaultValue: 'no'
363     },
364     directmediapermit: {
365       type: DataTypes.STRING,
366       allowNull: true,
367     },
368     directmediadeny: {
369       type: DataTypes.STRING,
370       allowNull: true,
371     },
372     nat: {
373       type: DataTypes.STRING,
374       allowNull: true,
375       defaultValue: 'force_rport,comedia'
376     },
377     callgroup: {
378       type: DataTypes.STRING,
379       allowNull: true,
380     },
381     namedcallgroup: { //We are in named call groups engineering,sales,netgroup,protgroup
382       type: DataTypes.STRING,
383       allowNull: true,
384     },
385     pickupgroup: {
386       type: DataTypes.STRING,
387       allowNull: true,
388     },
389     namedpickupgroup: { //We can do call pick-p for named call group sales
390       type: DataTypes.STRING,
391       allowNull: true,
392     },
393     language: {
394       type: DataTypes.STRING,
395       allowNull: true,
396       defaultValue: 'en'
397     },
398     tonezone: {
399       type: DataTypes.STRING,
400       allowNull: true
401     },
402     allow: {
403       type: DataTypes.STRING,
404       allowNull: true,
405       defaultValue: 'alaw;ulaw;gsm'
406     },
407     disallow: {
408       type: DataTypes.STRING,
409       allowNull: true,
410       defaultValue: null
411     },
412     autoframing: {
413       type: DataTypes.ENUM('yes', 'no'),
414       allowNull: true,
415     },
416     insecure: {
417       type: DataTypes.STRING,
418       allowNull: true,
419       defaultValue: 'port,invite'
420     },
421     trustrpid: {
422       type: DataTypes.ENUM('yes', 'no'),
423       allowNull: true,
424       defaultValue: 'no'
425     },
426     trust_id_outbound: {
427       type: DataTypes.ENUM('yes', 'no'),
428       allowNull: true,
429       defaultValue: 'no'
430     },
431     progressinband: {
432       type: DataTypes.ENUM('yes', 'no', 'never'),
433       allowNull: true,
434     },
435     promiscredir: {
436       type: DataTypes.ENUM('yes', 'no'),
437       allowNull: true,
438     },
439     useclientcode: {
440       type: DataTypes.ENUM('yes', 'no'),
441       allowNull: true,
442     },
443     accountcode: {
444       type: DataTypes.INTEGER(11),
445       allowNull: true,
446     },
447     setvar: {
448       type: DataTypes.STRING,
449       allowNull: true,
450     },
451     callerid: {
452       type: DataTypes.STRING,
453       allowNull: true,
454       defaultValue: '"" <>'
455     },
456     amaflags: {
457       type: DataTypes.STRING,
458       allowNull: true,
459     },
460     callcounter: {
461       type: DataTypes.ENUM('yes', 'no'),
462       allowNull: true,
463       defaultValue: 'yes'
464     },
465     busylevel: {
466       type: DataTypes.INTEGER(11),
467       allowNull: true,
468     },
469     allowoverlap: {
470       type: DataTypes.ENUM('yes', 'no'),
471       allowNull: true,
472     },
473     allowsubscribe: {
474       type: DataTypes.ENUM('yes', 'no'),
475       allowNull: true,
476     },
477     allowtransfer: {
478       type: DataTypes.ENUM('yes', 'no'),
479       allowNull: true,
480     },
481     ignoresdpversion: {
482       type: DataTypes.ENUM('yes', 'no'),
483       allowNull: true,
484     },
485     subscribecontext: {
486       type: DataTypes.STRING,
487       allowNull: true,
488     },
489     template: {
490       type: DataTypes.STRING,
491       allowNull: true,
492     },
493     videosupport: {
494       type: DataTypes.ENUM('yes', 'no', 'always'),
495       allowNull: true,
496       defaultValue: 'no'
497     },
498     maxcallbitrate: {
499       type: DataTypes.INTEGER(11),
500       allowNull: true,
501     },
502     rfc2833compensate: {
503       type: DataTypes.ENUM('yes', 'no'),
504       allowNull: true,
505     },
506     mailbox: {
507       type: DataTypes.STRING,
508       allowNull: true,
509     },
510     session_timers: {
511       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
512       allowNull: true,
513     },
514     session_expires: {
515       type: DataTypes.INTEGER(11),
516       allowNull: true,
517     },
518     session_minse: {
519       type: DataTypes.INTEGER(11),
520       allowNull: true,
521     },
522     session_refresher: {
523       type: DataTypes.ENUM('uac', 'uas'),
524       allowNull: true,
525       defaultValue: 'uas'
526     },
527     t38pt_usertpsource: {
528       type: DataTypes.STRING,
529       allowNull: true,
530     },
531     regexten: {
532       type: DataTypes.STRING,
533       allowNull: true,
534     },
535     fromdomain: {
536       type: DataTypes.STRING,
537       allowNull: true,
538     },
539     fromuser: {
540       type: DataTypes.STRING,
541       allowNull: true,
542     },
543     host: {
544       type: DataTypes.STRING,
545       allowNull: true,
546       defaultValue: 'dynamic'
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   }, {
741     tableName: 'users',
742     instanceMethods: {
743       /**
744        * Authenticate - check if the passwords are the same
745        *
746        * @param {String} plainText
747        *        {function} callBack
748        * @api public
749        */
750       authenticate: function(plainText) {
751         return this.encryptPassword(plainText) === this.password;
752       },
753       /**
754        * Make salt
755        *
756        * @return {String}
757        * @api public
758        */
759       makeSalt: function() {
760         return crypto.randomBytes(16).toString('base64');
761       },
762       /**
763        * Encrypt password
764        *
765        * @param {String} password
766        * @return {String}
767        * @api public
768        */
769       encryptPassword: function(password) {
770         if (!password || !this.salt) return '';
771         var salt = new Buffer(this.salt, 'base64');
772         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
773           'base64');
774       },
775       /**
776        * md5 password
777        *
778        * @param {String} password
779        * @return {String}
780        * @api public
781        */
782       md5Password: function(password) {
783         if (!password) return '';
784         return md5(password);
785       }
786     },
787     associate: function(models) {
788       // BELOGNS TO MANY
789       User.hasMany(models.ChatMessage);
790       User.hasMany(models.MailMessage);
791       User.hasMany(models.Contact);
792       User.hasMany(models.Action);
793       User.belongsToMany(models.Module, {
794         through: models.UserHasModule,
795         required: false
796       });
797       User.belongsToMany(models.Channel, {
798         through: 'user_has_channels'
799       });
800       User.belongsToMany(models.MailRoom, {
801         through: 'user_has_mail_rooms'
802       });
803       User.belongsToMany(models.SmsRoom, {
804         through: 'user_has_sms_rooms'
805       });
806       User.belongsToMany(models.OpenchannelRoom, {
807         through: 'user_has_openchannel_rooms'
808       });
809       User.belongsToMany(models.FaxRoom, {
810         through: 'user_has_fax_rooms'
811       });
812       User.belongsToMany(models.Team, {
813         through: models.UserHasTeam
814       });
815       User.belongsToMany(models.VoiceQueue, {
816         through: models.UserHasVoiceQueue,
817         required: false
818       });
819       User.belongsToMany(models.VoiceQueue, {
820         through: models.UserHasVoiceQueuePermit,
821         as: 'PVoiceQueues'
822       });
823       User.belongsToMany(models.MailQueue, {
824         through: models.UserHasMailQueue,
825         required: false
826       });
827       User.belongsToMany(models.SmsQueue, {
828         through: models.UserHasSmsQueue,
829         required: false
830       });
831       User.belongsToMany(models.SmsQueue, {
832         through: models.UserHasSmsQueuePermit,
833         as: 'PSmsQueues'
834       });
835       User.belongsToMany(models.OpenchannelQueue, {
836         through: models.UserHasOpenchannelQueue,
837         required: false
838       });
839       User.belongsToMany(models.OpenchannelQueue, {
840         through: models.UserHasOpenchannelQueuePermit,
841         as: 'POpenchannelQueues'
842       });
843       User.belongsToMany(models.MailQueue, {
844         through: models.UserHasMailQueuePermit,
845         as: 'PMailQueues'
846       });
847       User.belongsToMany(models.FaxQueue, {
848         through: models.UserHasFaxQueue,
849         required: false
850       });
851       User.belongsToMany(models.FaxQueue, {
852         through: models.UserHasFaxQueuePermit,
853         as: 'PFaxQueues'
854       });
855       User.belongsToMany(models.ChatQueue, {
856         through: models.UserHasChatQueue,
857         required: false
858       });
859       User.belongsToMany(models.ChatQueue, {
860         through: models.UserHasChatQueuePermit,
861         as: 'PChatQueues'
862       });
863       User.belongsToMany(models.ChatRoom, {
864         through: models.UserHasChatRoom
865       });
866       User.belongsToMany(models.List, {
867         through: models.UserHasList
868       });
869       User.hasMany(models.VoiceExtension, {
870         foreignKey: 'UserId',
871         as: 'UserExtensions',
872         onDelete: 'cascade',
873         hooks: true
874       });
875
876       // SCOPES MANAGEMENT
877       User.addScope('all', {
878         attributes: ['id',
879           'name',
880           'email',
881           'internal',
882           'fullname',
883           'role',
884           'online',
885           'userpic',
886           'accountcode',
887           'transport',
888           'host',
889           'role',
890           'nat',
891           'type',
892           'allow',
893           'lastLoginAt',
894           'ipaddr',
895           'fullcontact',
896           'port',
897           'lastms',
898           'description'
899         ]
900       });
901
902       // SCOPES MANAGEMENT
903       User.addScope('user', {
904         where: {
905           role: {
906             $in: ['admin', 'user']
907           },
908         },
909         attributes: ['id',
910           'name',
911           'email',
912           'internal',
913           'fullname',
914           'role',
915           'userpic',
916           'accountcode',
917           'transport',
918           'host',
919           'role',
920           'nat',
921           'type',
922           'allow',
923           'lastLoginAt',
924           'ipaddr',
925           'fullcontact',
926           'port',
927           'lastms',
928           'description',
929         ]
930       });
931       User.addScope('agent', {
932         where: {
933           role: 'agent'
934         },
935         attributes: ['id',
936           'name',
937           'email',
938           'internal',
939           'fullname',
940           'role',
941           'userpic',
942           'accountcode',
943           'transport',
944           'host',
945           'nat',
946           'type',
947           'allow',
948           'chatCapacity',
949           'mailCapacity',
950           'faxCapacity',
951           'smsCapacity',
952           'openchannelCapacity',
953           'online',
954           'lastLoginAt',
955           'phoneBarAutoAnswer',
956           'phoneBarEnableSettings',
957           'phoneBarUnconditional',
958           'phoneBarNoReply',
959           'phoneBarBusy',
960           'phoneBarUnconditionalNumber',
961           'phoneBarNoReplyNumber',
962           'phoneBarBusyNumber',
963           'phoneBarListenPort',
964           'phoneBarExpires',
965           'phoneBarRemoteControl',
966           'phoneBarRemoteControlPort',
967           'chanspy',
968           'voicePause',
969           'mailPause',
970           'faxPause',
971           'chatPause',
972           'smsPause',
973           'openchannelPause',
974           'pauseType',
975           'lastPauseAt',
976           'status',
977           'statusAt',
978           'queueStatus',
979           'queueStatusAt',
980           'lastQueue',
981           'useragent',
982           'ipaddr',
983           'fullcontact',
984           'port',
985           'lastms',
986           'description',
987           'loginInPause'
988         ]
989       });
990       User.addScope('telephone', {
991         where: {
992           role: 'telephone'
993         }
994       });
995       User.addScope('queues', {
996         include: [models.VoiceQueue, models.ChatQueue, models.MailQueue,
997           models.FaxQueue, models.SmsQueue, models.OpenchannelQueue
998         ]
999       });
1000
1001       User.addScope('checkPauseStatus', function(query) {
1002         var scope = {
1003           where: {}
1004         };
1005         if (query.voicePause) {
1006           scope.where.voicePause = (query.voicePause === 'true') ?
1007             true : false;
1008           delete query.voicePause;
1009         } else if (query.faxPause) {
1010           scope.where.faxPause = (query.faxPause === 'true') ? true :
1011             false;
1012           delete query.faxPause;
1013         } else if (query.chatPause) {
1014           scope.where.chatPause = (query.chatPause === 'true') ?
1015             true : false;
1016           delete query.chatPause;
1017         } else if (query.mailPause) {
1018           scope.where.mailPause = (query.mailPause === 'true') ?
1019             true : false;
1020           delete query.mailPause;
1021         } else if (query.smsPause) {
1022           scope.where.smsPause = (query.smsPause === 'true') ?
1023             true : false;
1024           delete query.smsPause;
1025         } else if (query.openchannelPause) {
1026           scope.where.openchannelPause = (query.openchannelPause === 'true') ?
1027             true : false;
1028           delete query.openchannelPause;
1029         }
1030         return scope;
1031       });
1032       User.addScope('checkOnlineStatus', function(query) {
1033         var scope = {
1034           where: {}
1035         };
1036         if (query.online) {
1037           scope.where.online = (query.online === 'true') ? true :
1038             false;
1039           delete query.online;
1040         }
1041         return scope;
1042       });
1043       User.addScope('checkSipStatus', function(query) {
1044         var scope = {
1045           where: {}
1046         };
1047         if (query.status) {
1048           scope.where.status = query.status;
1049           delete query.status;
1050         }
1051         return scope;
1052       });
1053       User.addScope('checkQueueStatus', function(query) {
1054         var scope = {
1055           where: {}
1056         };
1057         if (query.queueStatus) {
1058           scope.where.queueStatus = query.queueStatus;
1059           delete query.queueStatus;
1060         }
1061         return scope;
1062       });
1063     }
1064   });
1065
1066   return User;
1067 }