Created
October 5, 2012 12:02
-
-
Save tommedema/3839429 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//find the superadmin user (force reading from master to ensure consistency) | |
users.findOne({name: config.admin.name}, {slaveOk: false}, function(err, doc) { | |
//when an error occured | |
if (err) { | |
var dbErr = new Error('failed to find super admin account'); | |
dbErr.origErr = err; | |
return mediator.emit('db.error', dbErr); | |
} | |
//when superadmin account does not yet exist | |
if (!doc || !doc.name || !doc.passhash) { | |
//use config hash if available, otherwise create one from raw password | |
if (config.admin.passhash) { | |
saveAdminUser(config.admin.passhash); | |
} | |
else { | |
//create a new hash | |
bcrypt.hash(config.admin.pass, 8, function(err, hash) { | |
if (err) { | |
var hashErr = new Error('failed to hash admin pass'); | |
hashErr.origErr = err; | |
return mediator.emit('db.error', hashErr); | |
} | |
saveAdminUser(hash); | |
}); | |
} | |
function saveAdminUser(hash) { | |
//create the document | |
var user = { | |
name: config.admin.name, | |
passhash: hash, | |
power: 100 | |
}; | |
//insert it | |
users.insert(user, {safe: true}, function(err, records) { | |
if (err || records.length === 0) { | |
var insErr = new Error('failed to insert admin user to users collection'); | |
insErr.origErr = err; | |
return mediator.emit('db.error', insErr); | |
} | |
//event: new user saved | |
mediator.emit('db.users.saved', user.name, user.passhash, user.power); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment