forked from akoshek/cloud-recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
73 lines (65 loc) · 1.77 KB
/
db.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
var Mongo = require('mongodb').MongoClient;
var bcrypt = require('bcrypt-nodejs');
var db;
module.exports.init = function (callback) {
Mongo.connect("mongodb://root:admin@ds111124.mlab.com:11124/cloud_users", function(err, database) {
if(!err) {
callback();
db = database.collection('users');
}
else callback(err);
});
}
module.exports.createUser= function (username, id, token, callback) {
db.insert({username: username, id:id, token:token},{w:1}, function (err, result) {
if(err)
callback(err,null)
else {
res = result.ops[0];
callback(null,res);
}
});
}
module.exports.findUser= function (id, callback) {
db.findOne({id:id}, function (err, item) {
if(err)
callback(err,null);
else callback(null,item);
});
}
module.exports.setKeys= function (username,key,id, callback) {
var hash = bcrypt.hashSync(key);
db.update({id:id},{$set: {api_user: username, key: key}}, {w:1}, function (err,result) {
if(err)
callback(err);
else callback();
});
}
module.exports.setRecoverySettings= function (options,id, callback) {
db.update({id:id},{$push: {ids: options.id,
imageNames: options.imagename,
hostnames: options.hostname,
domains: options.domain,
hasRecovery: options.hasRecovery}}, {w:1}, function (err,result) {
if(err)
callback(err);
else callback();
})
}
module.exports.setRecoveredMachine = function (user_id,machine_id,callback) {
db.findOne({id:user_id}, function (err,user) {
if(err)
callback(err);
else {
let machines = user.ids;
let index = machines.indexOf(machine_id);
let recovery = user.hasRecovery;
recovery[index] = true;
db.update({id:user_id},{$set: {hasRecovery:recovery}},{w:1},function (err,result) {
if(err)
callback(err);
else callback();
})
}
})
}