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