3 var crypto = require('crypto');
4 var md5 = require('md5');
5 var _ = require('lodash');
7 module.exports = function(sequelize, DataTypes) {
8 var Telephone = sequelize.define('Telephone', {
10 type: DataTypes.STRING,
14 is: /^[A-Za-z0-9\.\_]+$/i
17 this.setDataValue('name', name);
18 this.setDataValue('defaultuser', name);
22 type: DataTypes.STRING,
27 set: function(password) {
28 this.salt = this.makeSalt();
29 this.setDataValue('password', this.encryptPassword(password));
30 this.setDataValue('md5secret', this.md5Password(this.name + ':asterisk:' + password));
34 type: DataTypes.INTEGER(11),
36 set: function(internal) {
37 this.setDataValue('internal', internal);
38 this.setDataValue('accountcode', internal);
42 type: DataTypes.STRING,
46 type: DataTypes.INTEGER(5),
50 type: DataTypes.INTEGER(11),
54 type: DataTypes.STRING,
58 type: DataTypes.STRING,
62 type: DataTypes.STRING,
66 type: DataTypes.STRING,
70 type: DataTypes.INTEGER(11),
74 type: DataTypes.STRING,
76 defaultValue: 'dynamic'
79 type: DataTypes.ENUM('friend', 'user', 'peer'),
81 defaultValue: 'friend'
84 type: DataTypes.STRING,
86 defaultValue: 'from-sip'
89 type: DataTypes.STRING,
93 type: DataTypes.STRING,
97 type: DataTypes.STRING,
101 type: DataTypes.STRING,
105 type: DataTypes.STRING,
109 type: DataTypes.STRING,
114 type: DataTypes.ENUM('rfc2833', 'info', 'shortinfo', 'inband',
117 defaultValue: 'rfc2833'
120 type: DataTypes.ENUM('yes', 'no', 'nonat', 'update'),
125 type: DataTypes.STRING,
127 defaultValue: 'force_rport,comedia'
130 type: DataTypes.STRING,
134 type: DataTypes.STRING,
138 type: DataTypes.STRING,
143 type: DataTypes.STRING,
148 type: DataTypes.STRING,
150 defaultValue: 'alaw;ulaw;gsm'
153 type: DataTypes.STRING,
155 defaultValue: 'port,invite'
158 type: DataTypes.ENUM('yes', 'no'),
163 type: DataTypes.ENUM('yes', 'no', 'never'),
167 type: DataTypes.ENUM('yes', 'no'),
171 type: DataTypes.ENUM('yes', 'no'),
175 type: DataTypes.INTEGER(11),
179 type: DataTypes.STRING,
183 type: DataTypes.STRING,
185 defaultValue: '"" <>'
188 type: DataTypes.STRING,
192 type: DataTypes.ENUM('yes', 'no'),
197 type: DataTypes.INTEGER(11),
201 type: DataTypes.ENUM('yes', 'no'),
205 type: DataTypes.ENUM('yes', 'no'),
209 type: DataTypes.ENUM('yes', 'no'),
213 type: DataTypes.INTEGER(11),
217 type: DataTypes.ENUM('yes', 'no'),
221 type: DataTypes.STRING,
225 type: DataTypes.ENUM('accept', 'refuse', 'originate'),
229 type: DataTypes.INTEGER(11),
233 type: DataTypes.INTEGER(11),
236 "session-refresher": {
237 type: DataTypes.ENUM('uac', 'uas'),
240 t38pt_usertpsource: {
241 type: DataTypes.STRING,
245 type: DataTypes.STRING,
249 type: DataTypes.STRING,
253 type: DataTypes.STRING,
257 type: DataTypes.ENUM('yes', 'no'),
262 type: DataTypes.STRING,
266 type: DataTypes.INTEGER(11),
270 type: DataTypes.INTEGER(11),
274 type: DataTypes.ENUM('yes', 'no'),
279 type: DataTypes.STRING,
283 type: DataTypes.STRING,
287 type: DataTypes.INTEGER(11),
291 type: DataTypes.INTEGER(11),
295 type: DataTypes.INTEGER(11),
299 type: DataTypes.ENUM('yes', 'no'),
303 type: DataTypes.STRING,
307 type: DataTypes.STRING,
311 type: DataTypes.ENUM('yes', 'no'),
316 type: DataTypes.ENUM('yes', 'no'),
320 type: DataTypes.ENUM('yes', 'no'),
324 type: DataTypes.ENUM('yes', 'no'),
328 type: DataTypes.STRING,
332 type: DataTypes.STRING,
336 type: DataTypes.STRING,
340 type: DataTypes.STRING,
344 type: DataTypes.ENUM('ALLOWED_NOT_SCREENED',
345 'ALLOWED_PASSED_SCREEN', 'ALLOWED_FAILED_SCREEN', 'ALLOWED',
346 'PROHIB_NOT_SCREENED', 'PROHIB_PASSED_SCREEN',
347 'PROHIB_FAILED_SCREEN', 'PROHIB'),
351 type: DataTypes.STRING,
355 type: DataTypes.STRING,
359 type: DataTypes.STRING,
363 type: DataTypes.ENUM('yes', 'no'),
367 type: DataTypes.ENUM('yes', 'no'),
371 type: DataTypes.STRING,
375 type: DataTypes.STRING,
379 type: DataTypes.ENUM('yes', 'no'),
383 type: DataTypes.ENUM('yes', 'no'),
388 type: DataTypes.INTEGER(11),
392 type: DataTypes.INTEGER(11),
397 type: DataTypes.ENUM('yes', 'no'),
401 type: DataTypes.ENUM('yes', 'no'),
405 type: DataTypes.ENUM('yes', 'no'),
409 type: DataTypes.ENUM('yes', 'no'),
413 type: DataTypes.ENUM('yes', 'no'),
418 type: DataTypes.STRING,
430 * Authenticate - check if the passwords are the same
432 * @param {String} plainText
433 * {function} callBack
436 authenticate: function(plainText) {
437 return this.encryptPassword(plainText) === this.password;
445 makeSalt: function() {
446 return crypto.randomBytes(16).toString('base64');
451 * @param {String} password
455 encryptPassword: function(password) {
456 if (!password || !this.salt) return '';
457 var salt = new Buffer(this.salt, 'base64');
458 return crypto.pbkdf2Sync(password, salt, 10000, 64).toString(
464 * @param {String} password
468 md5Password: function(password) {
469 if (!password) return '';
470 return md5(password);
473 associate: function(models) {
475 // Telephone.hasMany(models.ChatMessage);
476 // Telephone.hasMany(models.Contact);
477 // Telephone.belongsToMany(models.Module, {
478 // through: 'user_has_modules'
480 // Telephone.belongsToMany(models.Channel, {
481 // through: 'user_has_channels'
483 // Telephone.belongsToMany(models.Team, {
484 // through: models.UserHasTeam
486 // Telephone.belongsToMany(models.ChatRoom, {
487 // through: models.UserHasChatRoom
489 // Telephone.belongsToMany(models.MailQueue, {
490 // through: models.UserHasMailQueue
492 // Telephone.belongsToMany(models.ChatQueue, {
493 // through: models.UserHasChatQueue
495 // Telephone.belongsToMany(models.VoiceQueue, {
496 // through: models.UserHasVoiceQueue
498 // Telephone.hasMany(models.VoiceExtension, {
499 // foreignKey: 'UserId',
500 // as: 'UserExtensions',
501 // onDelete: 'cascade'