1d5ee5becbe3eb213cd4e925918720bc9852c003
[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     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 }