Files
meanTorrent/modules/users/server/models/user.server.model.js

151 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-12-25 16:36:33 +02:00
'use strict';
2013-05-22 17:03:50 +03:00
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
2014-02-10 13:09:25 +02:00
Schema = mongoose.Schema,
crypto = require('crypto');
2013-05-22 17:03:50 +03:00
2014-03-26 01:11:24 +02:00
/**
* A Validation function for local strategy properties
*/
var validateLocalStrategyProperty = function(property) {
return ((this.provider !== 'local' && !this.updated) || property.length);
};
/**
* A Validation function for local strategy password
*/
var validateLocalStrategyPassword = function(password) {
return (this.provider !== 'local' || (password && password.length > 6));
};
2013-05-22 17:03:50 +03:00
/**
* User Schema
*/
var UserSchema = new Schema({
2014-02-10 13:09:25 +02:00
firstName: {
type: String,
2014-03-26 01:11:24 +02:00
trim: true,
2014-02-10 13:09:25 +02:00
default: '',
2014-03-26 01:11:24 +02:00
validate: [validateLocalStrategyProperty, 'Please fill in your first name']
2014-02-10 13:09:25 +02:00
},
lastName: {
type: String,
2014-03-26 01:11:24 +02:00
trim: true,
2014-02-10 13:09:25 +02:00
default: '',
2014-03-26 01:11:24 +02:00
validate: [validateLocalStrategyProperty, 'Please fill in your last name']
2014-02-10 13:09:25 +02:00
},
displayName: {
type: String,
2014-02-10 13:24:01 +02:00
trim: true
2014-02-10 13:09:25 +02:00
},
email: {
type: String,
2014-02-10 13:24:01 +02:00
trim: true,
2014-03-26 01:11:24 +02:00
default: '',
validate: [validateLocalStrategyProperty, 'Please fill in your email'],
match: [/.+\@.+\..+/, 'Please fill a valid email address']
2014-02-10 13:09:25 +02:00
},
username: {
2014-03-26 01:11:24 +02:00
type: String,
unique: 'Username already exists',
2014-03-26 01:11:24 +02:00
required: 'Please fill in a username',
trim: true
},
password: {
2014-02-10 13:09:25 +02:00
type: String,
default: '',
2014-03-26 01:11:24 +02:00
validate: [validateLocalStrategyPassword, 'Password should be longer']
},
salt: {
type: String
2014-02-10 13:09:25 +02:00
},
2014-11-10 23:12:33 +02:00
profileImageURL: {
type: String,
default: 'modules/users/img/profile/default.png'
},
2014-02-10 13:09:25 +02:00
provider: {
type: String,
2014-03-26 01:11:24 +02:00
required: 'Provider is required'
2014-02-10 13:09:25 +02:00
},
2014-03-26 01:11:24 +02:00
providerData: {},
2014-04-16 02:00:23 +03:00
additionalProvidersData: {},
roles: {
type: [{
type: String,
enum: ['user', 'admin']
}],
default: ['user']
},
2014-03-26 01:11:24 +02:00
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
/* For reset password */
resetPasswordToken: {
type: String
},
2014-11-10 23:12:33 +02:00
resetPasswordExpires: {
type: Date
}
});
2013-05-22 17:03:50 +03:00
/**
2014-03-26 01:11:24 +02:00
* Hook a pre save method to hash the password
2013-05-22 17:03:50 +03:00
*/
2014-03-26 01:11:24 +02:00
UserSchema.pre('save', function(next) {
if (this.password && this.password.length > 6) {
this.salt = crypto.randomBytes(16).toString('base64');
2014-03-26 01:11:24 +02:00
this.password = this.hashPassword(this.password);
}
next();
2013-08-17 01:06:17 +03:00
});
2013-05-22 17:03:50 +03:00
/**
2014-03-26 01:11:24 +02:00
* Create instance method for hashing a password
2013-05-22 17:03:50 +03:00
*/
2014-03-26 01:11:24 +02:00
UserSchema.methods.hashPassword = function(password) {
if (this.salt && password) {
return crypto.pbkdf2Sync(password, new Buffer(this.salt, 'base64'), 10000, 64).toString('base64');
2014-03-31 02:39:07 +03:00
} else {
return password;
}
};
2013-05-22 17:03:50 +03:00
/**
2014-03-26 01:11:24 +02:00
* Create instance method for authenticating user
2013-05-22 17:03:50 +03:00
*/
2014-03-26 01:11:24 +02:00
UserSchema.methods.authenticate = function(password) {
return this.password === this.hashPassword(password);
};
2013-05-22 17:03:50 +03:00
/**
2014-03-26 01:11:24 +02:00
* Find possible not used username
2013-05-22 17:03:50 +03:00
*/
2014-03-26 01:11:24 +02:00
UserSchema.statics.findUniqueUsername = function(username, suffix, callback) {
var _this = this;
var possibleUsername = username + (suffix || '');
2013-08-17 01:06:17 +03:00
_this.findOne({
username: possibleUsername
}, function(err, user) {
if (!err) {
2014-03-26 01:11:24 +02:00
if (!user) {
callback(possibleUsername);
} else {
return _this.findUniqueUsername(username, (suffix || 0) + 1, callback);
}
} else {
callback(null);
}
});
};
2013-05-22 17:03:50 +03:00
2015-02-07 12:31:07 -08:00
mongoose.model('User', UserSchema);