0.0.11 | Built motion from commit e8dda05.
[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
7 module.exports = function(sequelize, DataTypes) {
8   var User = sequelize.define('User', {
9     name: {
10       type: DataTypes.STRING,
11       unique: true,
12       validate: {
13         notEmpty: true
14       },
15       set: function(name) {
16         this.setDataValue('name', name);
17         this.setDataValue('defaultuser', name);
18       }
19     },
20     email: {
21       type: DataTypes.STRING,
22       unique: true,
23       set: function(email) {
24         if (email) {
25           this.setDataValue('email', email.toLowerCase());
26         }
27
28       },
29       defaultValue: null
30     },
31     role: {
32       type: DataTypes.ENUM('admin', 'user', 'agent', 'telephone')
33     },
34     password: {
35       type: DataTypes.STRING,
36       allowNull: false,
37       validate: {
38         notEmpty: true
39       },
40       set: function(password) {
41         this.salt = this.makeSalt();
42         this.setDataValue('password', this.encryptPassword(password));
43         this.setDataValue('md5secret', this.md5Password(this.name + ':asterisk:' + password));
44       }
45     },
46     provider: {
47       type: DataTypes.STRING,
48       defaultValue: 'local'
49     },
50     internal: {
51       type: DataTypes.INTEGER(11),
52       unique: true,
53       set: function(internal) {
54         this.setDataValue('internal', internal);
55         this.setDataValue('accountcode', internal);
56       }
57     },
58     salt: {
59       type: DataTypes.STRING
60     },
61     phone: {
62       type: DataTypes.STRING
63     },
64     mobile: {
65       type: DataTypes.STRING
66     },
67     address: {
68       type: DataTypes.STRING
69     },
70     zipcode: {
71       type: DataTypes.STRING
72     },
73     userpic: {
74       type: DataTypes.STRING
75     },
76     city: {
77       type: DataTypes.STRING
78     },
79     country: {
80       type: DataTypes.STRING
81     },
82     facebookUserId: {
83       type: DataTypes.INTEGER
84     },
85     twitterUserId: {
86       type: DataTypes.INTEGER
87     },
88     twitterKey: {
89       type: DataTypes.STRING
90     },
91     twitterSecret: {
92       type: DataTypes.STRING
93     },
94     github: {
95       type: DataTypes.STRING
96     },
97     openId: {
98       type: DataTypes.STRING
99     },
100     online: {
101       type: DataTypes.BOOLEAN,
102       defaultValue: false
103     },
104     lastLoginAt: {
105       type: DataTypes.DATE
106     },
107     ipaddr: {
108       type: DataTypes.STRING,
109       allowNull: true,
110     },
111     port: {
112       type: DataTypes.INTEGER(5),
113       allowNull: true,
114     },
115     regseconds: {
116       type: DataTypes.INTEGER(11),
117       allowNull: true,
118     },
119     defaultuser: {
120       type: DataTypes.STRING,
121       allowNull: true,
122     },
123     fullcontact: {
124       type: DataTypes.STRING,
125       allowNull: true,
126     },
127     regserver: {
128       type: DataTypes.STRING,
129       allowNull: true,
130     },
131     useragent: {
132       type: DataTypes.STRING,
133       allowNull: true,
134     },
135     lastms: {
136       type: DataTypes.INTEGER(11),
137       allowNull: true,
138     },
139     host: {
140       type: DataTypes.STRING,
141       allowNull: true,
142       defaultValue: 'dynamic'
143     },
144     type: {
145       type: DataTypes.ENUM('friend', 'user', 'peer'),
146       allowNull: true,
147       defaultValue: 'friend'
148     },
149     context: {
150       type: DataTypes.STRING,
151       allowNull: true,
152       defaultValue: 'from-sip'
153     },
154     permit: {
155       type: DataTypes.STRING,
156       allowNull: true,
157     },
158     deny: {
159       type: DataTypes.STRING,
160       allowNull: true,
161     },
162     secret: {
163       type: DataTypes.STRING,
164       allowNull: true,
165     },
166     md5secret: {
167       type: DataTypes.STRING,
168       allowNull: true
169     },
170     remotesecret: {
171       type: DataTypes.STRING,
172       allowNull: true,
173     },
174     transport: {
175       type: DataTypes.STRING,
176       allowNull: true,
177       defaultValue: 'udp'
178     },
179     dtmfmode: {
180       type: DataTypes.ENUM('rfc2833', 'info', 'shortinfo', 'inband',
181         'auto'),
182       allowNull: true,
183       defaultValue: 'rfc2833'
184     },
185     directmedia: {
186       type: DataTypes.ENUM('yes', 'no', 'nonat', 'update'),
187       allowNull: true,
188       defaultValue: 'no'
189     },
190     nat: {
191       type: DataTypes.STRING,
192       allowNull: true,
193       defaultValue: 'force_rport,comedia'
194     },
195     callgroup: {
196       type: DataTypes.STRING,
197       allowNull: true,
198     },
199     pickupgroup: {
200       type: DataTypes.STRING,
201       allowNull: true,
202     },
203     language: {
204       type: DataTypes.STRING,
205       allowNull: true,
206       defaultValue: 'en'
207     },
208     disallow: {
209       type: DataTypes.STRING,
210       allowNull: true,
211       defaultValue: 'all'
212     },
213     allow: {
214       type: DataTypes.STRING,
215       allowNull: true,
216       defaultValue: 'alaw;ulaw;gsm'
217     },
218     insecure: {
219       type: DataTypes.STRING,
220       allowNull: true,
221       defaultValue: 'port,invite'
222     },
223     trustrpid: {
224       type: DataTypes.ENUM('yes', 'no'),
225       allowNull: true,
226       defaultValue: 'no'
227     },
228     progressinband: {
229       type: DataTypes.ENUM('yes', 'no', 'never'),
230       allowNull: true,
231     },
232     promiscredir: {
233       type: DataTypes.ENUM('yes', 'no'),
234       allowNull: true,
235     },
236     useclientcode: {
237       type: DataTypes.ENUM('yes', 'no'),
238       allowNull: true,
239     },
240     accountcode: {
241       type: DataTypes.INTEGER(11),
242       allowNull: true,
243     },
244     setvar: {
245       type: DataTypes.STRING,
246       allowNull: true,
247     },
248     callerid: {
249       type: DataTypes.STRING,
250       allowNull: true,
251       defaultValue: '"" <>'
252     },
253     amaflags: {
254       type: DataTypes.STRING,
255       allowNull: true,
256     },
257     callcounter: {
258       type: DataTypes.ENUM('yes', 'no'),
259       allowNull: true,
260       defaultValue: 'yes'
261     },
262     busylevel: {
263       type: DataTypes.INTEGER(11),
264       allowNull: true,
265     },
266     allowoverlap: {
267       type: DataTypes.ENUM('yes', 'no'),
268       allowNull: true,
269     },
270     allowsubscribe: {
271       type: DataTypes.ENUM('yes', 'no'),
272       allowNull: true,
273     },
274     videosupport: {
275       type: DataTypes.ENUM('yes', 'no'),
276       allowNull: true,
277     },
278     maxcallbitrate: {
279       type: DataTypes.INTEGER(11),
280       allowNull: true,
281     },
282     rfc2833compensate: {
283       type: DataTypes.ENUM('yes', 'no'),
284       allowNull: true,
285     },
286     mailbox: {
287       type: DataTypes.STRING,
288       allowNull: true,
289     },
290     "session-timers": {
291       type: DataTypes.ENUM('accept', 'refuse', 'originate'),
292       allowNull: true,
293     },
294     "session-expires": {
295       type: DataTypes.INTEGER(11),
296       allowNull: true,
297     },
298     "session-minse": {
299       type: DataTypes.INTEGER(11),
300       allowNull: true,
301     },
302     "session-refresher": {
303       type: DataTypes.ENUM('uac', 'uas'),
304       allowNull: true,
305     },
306     t38pt_usertpsource: {
307       type: DataTypes.STRING,
308       allowNull: true,
309     },
310     regexten: {
311       type: DataTypes.STRING,
312       allowNull: true,
313     },
314     fromdomain: {
315       type: DataTypes.STRING,
316       allowNull: true,
317     },
318     fromuser: {
319       type: DataTypes.STRING,
320       allowNull: true,
321     },
322     qualify: {
323       type: DataTypes.ENUM('yes', 'no'),
324       allowNull: true,
325       defaultValue: 'yes'
326     },
327     defaultip: {
328       type: DataTypes.STRING,
329       allowNull: true,
330     },
331     rtptimeout: {
332       type: DataTypes.INTEGER(11),
333       allowNull: true,
334     },
335     rtpholdtimeout: {
336       type: DataTypes.INTEGER(11),
337       allowNull: true,
338     },
339     sendrpid: {
340       type: DataTypes.ENUM('yes', 'no'),
341       allowNull: true,
342       defaultValue: 'no'
343     },
344     outboundproxy: {
345       type: DataTypes.STRING,
346       allowNull: true,
347     },
348     callbackextension: {
349       type: DataTypes.STRING,
350       allowNull: true,
351     },
352     timert1: {
353       type: DataTypes.INTEGER(11),
354       allowNull: true,
355     },
356     timerb: {
357       type: DataTypes.INTEGER(11),
358       allowNull: true,
359     },
360     qualifyfreq: {
361       type: DataTypes.INTEGER(11),
362       allowNull: true,
363     },
364     constantssrc: {
365       type: DataTypes.ENUM('yes', 'no'),
366       allowNull: true,
367     },
368     contactpermit: {
369       type: DataTypes.STRING,
370       allowNull: true,
371     },
372     contactdeny: {
373       type: DataTypes.STRING,
374       allowNull: true,
375     },
376     usereqphone: {
377       type: DataTypes.ENUM('yes', 'no'),
378       allowNull: true,
379       defaultValue: 'no'
380     },
381     textsupport: {
382       type: DataTypes.ENUM('yes', 'no'),
383       allowNull: true,
384     },
385     faxdetect: {
386       type: DataTypes.ENUM('yes', 'no'),
387       allowNull: true,
388     },
389     buggymwi: {
390       type: DataTypes.ENUM('yes', 'no'),
391       allowNull: true,
392     },
393     auth: {
394       type: DataTypes.STRING,
395       allowNull: true,
396     },
397     fullname: {
398       type: DataTypes.STRING,
399       allowNull: true,
400     },
401     trunkname: {
402       type: DataTypes.STRING,
403       allowNull: true,
404     },
405     cid_number: {
406       type: DataTypes.STRING,
407       allowNull: true,
408     },
409     callingpres: {
410       type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
411         'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
412         'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
413         'PROHIB_FAILED_SCREEN', 'PROHIB'),
414       allowNull: true,
415     },
416     mohinterpret: {
417       type: DataTypes.STRING,
418       allowNull: true,
419     },
420     mohsuggest: {
421       type: DataTypes.STRING,
422       allowNull: true,
423     },
424     parkinglot: {
425       type: DataTypes.STRING,
426       allowNull: true,
427     },
428     hasvoicemail: {
429       type: DataTypes.ENUM('yes', 'no'),
430       allowNull: true,
431     },
432     subscribemwi: {
433       type: DataTypes.ENUM('yes', 'no'),
434       allowNull: true,
435     },
436     vmexten: {
437       type: DataTypes.STRING,
438       allowNull: true,
439     },
440     description: {
441       type: DataTypes.STRING,
442       allowNull: true,
443     },
444     autoframing: {
445       type: DataTypes.ENUM('yes', 'no'),
446       allowNull: true,
447     },
448     limitonpeers: {
449       type: DataTypes.ENUM('yes', 'no'),
450       allowNull: true,
451       defaultValue: 'yes'
452     },
453     rtpkeepalive: {
454       type: DataTypes.INTEGER(11),
455       allowNull: true,
456     },
457     "call-limit": {
458       type: DataTypes.INTEGER(11),
459       allowNull: true,
460       defaultValue: null
461     },
462     g726nonstandard: {
463       type: DataTypes.ENUM('yes', 'no'),
464       allowNull: true,
465     },
466     ignoresdpversion: {
467       type: DataTypes.ENUM('yes', 'no'),
468       allowNull: true,
469     },
470     allowtransfer: {
471       type: DataTypes.ENUM('yes', 'no'),
472       allowNull: true,
473     },
474     dynamic: {
475       type: DataTypes.ENUM('yes', 'no'),
476       allowNull: true,
477     },
478     encryption: {
479       type: DataTypes.ENUM('yes', 'no'),
480       allowNull: true,
481       defaultValue: 'no'
482     },
483     registry: {
484       type: DataTypes.STRING,
485       allowNull: true,
486     }
487
488   }, {
489     tableName: 'users',
490     getterMethods: {
491       // profile: function() {
492       //   return {
493       //     name: this.name,
494       //     role: this.role
495       //   }
496       // }
497     },
498     instanceMethods: {
499       /**
500        * Authenticate - check if the passwords are the same
501        *
502        * @param {String} plainText
503        *        {function} callBack
504        * @api public
505        */
506       authenticate: function(plainText) {
507         return this.encryptPassword(plainText) === this.password;
508       },
509       /**
510        * Make salt
511        *
512        * @return {String}
513        * @api public
514        */
515       makeSalt: function() {
516         return crypto.randomBytes(16).toString('base64');
517       },
518       /**
519        * Encrypt password
520        *
521        * @param {String} password
522        * @return {String}
523        * @api public
524        */
525       encryptPassword: function(password) {
526         if (!password || !this.salt) return '';
527         var salt = new Buffer(this.salt, 'base64');
528         return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
529           'base64');
530       },
531       /**
532        * md5 password
533        *
534        * @param {String} password
535        * @return {String}
536        * @api public
537        */
538       md5Password: function(password) {
539         if (!password) return '';
540         return md5(password);
541       }
542     },
543     associate: function(models) {
544       // BELOGNS TO MANY
545       User.hasMany(models.ChatMessage);
546       User.hasMany(models.Contact);
547       User.belongsToMany(models.Module, {
548         through: 'user_has_modules'
549       });
550       User.belongsToMany(models.Channel, {
551         through: 'user_has_channels'
552       });
553       User.belongsToMany(models.Team, {
554         through: models.UserHasTeam
555       });
556       User.belongsToMany(models.ChatRoom, {
557         through: models.UserHasChatRoom
558       });
559       User.belongsToMany(models.MailQueue, {
560         through: models.UserHasMailQueue
561       });
562       User.belongsToMany(models.ChatQueue, {
563         through: models.UserHasChatQueue
564       });
565       User.belongsToMany(models.VoiceQueue, {
566         through: models.UserHasVoiceQueue
567       });
568       User.hasMany(models.VoiceExtension, {
569         foreignKey: 'UserId',
570         as: 'UserExtensions',
571         onDelete: 'cascade'
572       });
573
574
575     }
576   });
577
578   return User;
579 }