summaryrefslogtreecommitdiff
path: root/server/lib/schemas/User.js
blob: 180a14053037efc0c72db98cd81c04ba729535dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* jshint node: true */

var mongoose = require('mongoose'),
	uniqueValidator = require('mongoose-unique-validator'),
	_ = require('lodash'),
	util = require('../util'),
	crypto = require('crypto'),
	config = require('../../../config.json');

var UserSchema = new mongoose.Schema({
	twitter_id: String,
	facebook_id: String,
	
	displayName: { type: String, default: "" },
	username: {
		type: String,
		required: true,
		validate: [function (val) {
			val = util.slugify(val)
			this.username = val.toLowerCase()
			switch (val) {
				case 'login':
				case 'logout':
				case 'profile':
				case 'auth':
				case 'upload':
				case 'about':
				case 'settings':
				case 'assets':
				case 'staff':
				case 'admin':
				case 'terms':
				case 'api':
				case 'vvalls':
				case 'assets':
				case '':
					return false
			}
			if (! this.displayName) { this.displayName = val; }
			return true
		}, "{PATH} is not an acceptable name"]
	},
	email: { type: String, default: "" },
	emailVerified: { type: Boolean, default: false, },
	emailOptout: { type: Boolean, default: false, },
	password: {
		type: String,
		validate: [function (val) {
			if (! val.length) return true
			return true
		}, "{PATH} is not an acceptable password"]
	},
	passwordNonce: {
		type: String,
		default: "",
	},

	location: { type: String, default: "" },
	photo: { type: String, default: "" },
	bio: { type: String, default: "" },
	website: { type: String, default: "" },
	twitterName: { type: String, default: "" },
	facebookUrl: { type: String, default: "" },
	isStaff: { type: Boolean, default: false },
	created_at: { type: Date },
	updated_at: { type: Date },
	last_seen: { type: Date },
	created_ip: { type: Number },
	last_ip: { type: Number },	
});

UserSchema.plugin(uniqueValidator, { message: '{PATH} is already taken.' })

UserSchema.methods.validPassword = function (pw) {
	var shasum = crypto.createHash('sha1')
	shasum.update(pw)
	return this.password === shasum.digest('hex');
}
UserSchema.statics.findByUsername = function (username, cb) {
	this.findOne({ username: new RegExp("^" + username + "$", "i") }, cb)
}
UserSchema.statics.findByEmail = function (email, cb) {
	email = util.escapeRegExp(email)
	this.findOne({ email: new RegExp("^" + email + "$", "i") }, cb)
}
UserSchema.statics.findByIP = function (ip, cb) {
	ip = util.ip2num(ip)
	this.findOne({ $or: [{ created_ip: ip }, { last_ip: ip }] }, cb)
}
UserSchema.statics.resetPasswordNonce = function(user, cb){
	crypto.pseudoRandomBytes(256, function (err, buf){
		var shasum = crypto.createHash('sha1')
		shasum.update(buf)
		user.passwordNonce = shasum.digest('hex')
		user.save(function(err, doc){
			cb()
		})
	})
}
module.exports = exports = mongoose.model('user', UserSchema);
exports.schema = UserSchema;