-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmongoose_admin_user.js
More file actions
98 lines (84 loc) · 3.24 KB
/
Copy pathmongoose_admin_user.js
File metadata and controls
98 lines (84 loc) · 3.24 KB
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
var mongoose = require('mongoose');
bcrypt = require('./crypt');
exports.bcrypt = bcrypt;
var AdminUserData = new mongoose.Schema({
username:{type:String, required:true, unique:true},
passwordHash:{type:String, editable:false},
is_superuser :{type:Boolean,'default':false},
permissions:[{type:mongoose.Schema.ObjectId, ref:'_MongooseAdminPermission'}]
},{strict:true});
mongoose.model('_MongooseAdminUser', AdminUserData);
function MongooseAdminUser() {
this.fields = {};
};
MongooseAdminUser.prototype.toSessionStore = function() {
var serialized = {};
for (var i in this) {
if (typeof i !== 'function' || typeof i !== 'object') {
serialized[i] = this[i];
}
}
return JSON.stringify(serialized);
};
MongooseAdminUser.fromSessionStore = function(sessionStore) {
var sessionObject = JSON.parse(sessionStore);
var adminUser = new MongooseAdminUser();
for (var i in sessionObject) {
if (sessionObject.hasOwnProperty(i)) {
adminUser[i] = sessionObject[i];
}
}
return adminUser;
};
MongooseAdminUser.ensureExists = function(username, password, onReady) {
var adminUser = new MongooseAdminUser();
var adminUserModel = mongoose.model('_MongooseAdminUser');
adminUserModel.findOne({'username': username}, function(err, adminUserData) {
if (err) {
console.log('Unable to check if admin user exists because: ' + err);
oReady('Unable to check if user exist', null);
} else {
if (adminUserData) {
var salt = bcrypt.gen_salt_sync(10);
adminUserData.passwordHash = bcrypt.encrypt_sync(password, salt);
} else {
adminUserData = new adminUserModel();
adminUserData.username = username;
var salt = bcrypt.gen_salt_sync(10);
adminUserData.passwordHash = bcrypt.encrypt_sync(password, salt);
}
adminUserData.is_superuser = true;
adminUserData.save(function(err) {
if (err) {
console.log('Unable to create or update admin user because: ' + err);
onReady('Unable to create or update admin user', null);
} else {
adminUser.fields = adminUserData;
onReady(null, adminUser);
}
});
}
});
};
MongooseAdminUser.getByUsernamePassword = function(username, password, onReady) {
var adminUser = new MongooseAdminUser();
var adminUserModel = mongoose.model('_MongooseAdminUser');
adminUserModel.findOne({'username': username}, function(err, adminUserData) {
if (err) {
console.log('Unable to get admin user because: ' + err);
onReady('Unable to get admin user', null);
} else {
if (adminUserData) {
if (bcrypt.compare_sync(password, adminUserData.passwordHash)) {
adminUser.fields = adminUserData;
onReady(null, adminUser);
} else {
onReady(null, null);
}
} else {
onReady(null, null);
}
}
});
};
exports.MongooseAdminUser = MongooseAdminUser;