-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
92 lines (79 loc) · 2.69 KB
/
util.js
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
var crypto = require("crypto");
var bcrypt = require("bcrypt");
var models = require("./models/index");
var util = {
//Simple shorthand for model.save to allow error and success be passed as functions.
//This will also find the real db model if the one given is something liek a session var.
saveModel: function (model, error, success, modelName) {
if (model.save) {
model.save(function (err, model) {
if (err) {
console.log(err);
error(err);
} else {
success(model);
}
});
} else {
//Support for multiple models
if (!modelName)
modelName = "User";
var Model;
if (modelName == "User") {
Model = models.User;
} else if (modelName == "mineCraftServer") {
Model = models.MineCraftServer;
} else if (modelName == "Comment") {
Model = models.Comment;
} else {
console.error("Unknown model - " + modelName);
}
Model.findOne({
_id: model._id
}, function (err, found) {
Object.keys(model).forEach(function (key) {
if (key.charAt(0) == '_')
return;
var val = model[key];
found[key] = val;
});
found.save(function (err, model) {
if (err) {
console.log(err);
error(err);
} else {
success(model);
}
});
});
}
},
/**
* encrypts the password using becrpt
*/
bcrypt: function(password){
return bcrypt.hashSync(password, 8);
},
// Checks if there is at least one user matching params. If there is calls gtz (greaterThanZero).
// Otherwise calls zero. If an error occurs calls error with the error as a parameter.
moreThanZero: function (params, error, zero, gtz) {
models.User.count(params, function (err, count) {
if (err) {
console.error(err);
error(err);
} else if (count > 0) {
gtz();
} else {
zero();
}
});
},
//Calculates MD5 Hash of the given string. Returns in (lowercase) hex format
md5hash: function (str) {
var md5 = crypto.createHash('md5');
return md5.update(str).digest('hex');
},
verifyUser: function (usr, success, failure) {
}
}
module.exports = util;