This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring the seeddb logic to work with promises all over due to al…
…l the async behavior
- Loading branch information
Showing
4 changed files
with
135 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,93 +2,146 @@ | |
|
||
var mongoose = require('mongoose'), | ||
chalk = require('chalk'), | ||
crypto = require('crypto'), | ||
User = mongoose.model('User'); | ||
crypto = require('crypto'); | ||
|
||
console.log(chalk.bold.red('Warning: Database seeding is turned on')); | ||
|
||
var seedUser = { | ||
username: 'user', | ||
password: 'User_Password1!', | ||
provider: 'local', | ||
email: '[email protected]', | ||
firstName: 'User', | ||
lastName: 'Local', | ||
displayName: 'User Local', | ||
roles: ['user'] | ||
}; | ||
|
||
var seedAdmin = { | ||
username: 'admin', | ||
password: 'Admin_Password1!', | ||
provider: 'local', | ||
email: '[email protected]', | ||
firstName: 'Admin', | ||
lastName: 'Local', | ||
displayName: 'Admin Local', | ||
roles: ['user', 'admin'] | ||
}; | ||
|
||
//If production only seed admin if it does not exist | ||
if (process.env.NODE_ENV === 'production') { | ||
//Add Local Admin | ||
User.find({username: seedAdmin.username}, function (err, users) { | ||
if (users.length === 0) { | ||
var user = new User(seedAdmin); | ||
|
||
// generate a random password and save | ||
User.generateRandomPassphrase() | ||
.then(saveUser(user)) | ||
.catch(reportError); | ||
|
||
} else { | ||
console.log(seedAdmin.username + ' user exists'); | ||
} | ||
function removeUser (user) { | ||
return new Promise(function (resolve, reject) { | ||
var User = mongoose.model('User'); | ||
User.find({username: user.username}).remove(function (err) { | ||
if (err) { | ||
reject(new Error('Database Seeding:\t\t\tFailed to remove local ' + user.username)); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
} else { | ||
|
||
//Add Local User | ||
User.find({username: seedUser.username}).remove(function () { | ||
var user = new User(seedUser); | ||
} | ||
|
||
// generate a random password and save | ||
User.generateRandomPassphrase() | ||
.then(saveUser(user)) | ||
.catch(reportError); | ||
}); | ||
function saveUser (user) { | ||
return function() { | ||
return new Promise(function (resolve, reject) { | ||
// Then save the user | ||
user.save(function (err, theuser) { | ||
if (err) { | ||
reject(new Error('Database Seeding:\t\t\tFailed to add local ' + user.username)); | ||
} else { | ||
resolve(theuser); | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
//Add Local Admin | ||
User.find({username: seedAdmin.username}).remove(function () { | ||
var user = new User(seedAdmin); | ||
function checkUserNotExists (user) { | ||
return new Promise(function (resolve, reject) { | ||
var User = mongoose.model('User'); | ||
User.find({username: user.username}, function (err, users) { | ||
if (err) { | ||
reject(new Error('Database Seeding:\t\t\tFailed to find local account ' + user.username)); | ||
} | ||
|
||
// generate a random password and save | ||
User.generateRandomPassphrase() | ||
.then(saveUser(user)) | ||
.catch(reportError); | ||
if (users.length === 0) { | ||
resolve(); | ||
} else { | ||
reject(new Error('Database Seeding:\t\t\tFailed due to local account already exists: ' + user.username)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function reportSuccess (password) { | ||
return function (user) { | ||
return new Promise(function (resolve, reject) { | ||
console.log(chalk.bold.red('Database Seeding:\t\t\tLocal ' + user.username + ' added with password set to ' + password)); | ||
resolve(); | ||
}); | ||
}; | ||
} | ||
|
||
// save the specified user with the password provided from the resolved promise | ||
function saveUser(user) { | ||
function seedTheUser (user) { | ||
return function (password) { | ||
// set the new password | ||
user.password = password; | ||
return new Promise(function (resolve, reject) { | ||
|
||
var User = mongoose.model('User'); | ||
// set the new password | ||
user.password = password; | ||
|
||
// Then save the user | ||
user.save(function (err) { | ||
if (err) { | ||
console.log('Database Seeding:\t\t\tFailed to add local ' + user.username); | ||
if (user.username === 'admin' && process.env.NODE_ENV === 'production') { | ||
checkUserNotExists(user) | ||
.then(saveUser(user)) | ||
.then(reportSuccess(password)) | ||
.then(function () { | ||
resolve(); | ||
}) | ||
.catch(reportError); | ||
} else { | ||
console.log(chalk.bold.red('Database Seeding:\t\t\tLocal ' + user.username + ' added with password set to ' + password)); | ||
removeUser(user) | ||
.then(saveUser(user)) | ||
.then(reportSuccess(password)) | ||
.then(function () { | ||
resolve(); | ||
}) | ||
.catch(reportError); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
// report the error | ||
function reportError(err) { | ||
function reportError (err) { | ||
console.log(); | ||
console.log('Database Seeding:\t\t\t Failed to generate random password'); | ||
console.log(err); | ||
console.log(); | ||
} | ||
|
||
module.exports.start = function start() { | ||
var User = mongoose.model('User'); | ||
return new Promise(function (resolve, reject) { | ||
var seedUser = { | ||
username: 'user', | ||
password: 'User_Password1!', | ||
provider: 'local', | ||
email: '[email protected]', | ||
firstName: 'User', | ||
lastName: 'Local', | ||
displayName: 'User Local', | ||
roles: ['user'] | ||
}; | ||
|
||
var seedAdmin = { | ||
username: 'admin', | ||
password: 'Admin_Password1!', | ||
provider: 'local', | ||
email: '[email protected]', | ||
firstName: 'Admin', | ||
lastName: 'Local', | ||
displayName: 'Admin Local', | ||
roles: ['user', 'admin'] | ||
}; | ||
|
||
var user = null; | ||
var adminAccount = new User(seedAdmin); | ||
var userAccount = new User(seedUser); | ||
|
||
//If production only seed admin if it does not exist | ||
if (process.env.NODE_ENV === 'production') { | ||
User.generateRandomPassphrase() | ||
.then(seedTheUser(adminAccount)) | ||
.then(function () { | ||
resolve(); | ||
}) | ||
.catch(reportError); | ||
} else { | ||
// Add both Admin and User account | ||
|
||
User.generateRandomPassphrase() | ||
.then(seedTheUser(userAccount)) | ||
.then(User.generateRandomPassphrase) | ||
.then(seedTheUser(adminAccount)) | ||
.then(function () { | ||
resolve(); | ||
}) | ||
.catch(reportError); | ||
} | ||
}); | ||
}; |
This file contains 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