Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #793 from trainerbill/SeedDB2
Browse files Browse the repository at this point in the history
Database seeding
  • Loading branch information
lirantal committed Aug 13, 2015
2 parents 820355c + ea97f2e commit d41490c
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
3 changes: 2 additions & 1 deletion config/env/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ module.exports = {
}
}
},
livereload: true
livereload: true,
seedDB: process.env.MONGO_SEED || false
};
3 changes: 2 additions & 1 deletion config/env/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ module.exports = {
pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD'
}
}
}
},
seedDB: process.env.MONGO_SEED || false
};
3 changes: 2 additions & 1 deletion config/env/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ module.exports = {
pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD'
}
}
}
},
seedDB: process.env.MONGO_SEED || false
};
5 changes: 5 additions & 0 deletions config/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var config = require('../config'),
// Initialize Models
mongoose.loadModels();

//SeedDB
if (config.seedDB) {
require('./seed');
}

module.exports.loadModels = function loadModels() {
mongoose.loadModels();
};
Expand Down
85 changes: 85 additions & 0 deletions config/lib/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

var mongoose = require('mongoose'),
chalk = require('chalk'),
crypto = require('crypto'),
User = mongoose.model('User');

console.log(chalk.bold.red('Warning: Database seeding is turned on'));

//If production only seed admin if it does not exist
if (process.env.NODE_ENV === 'production') {
//Add Local Admin
User.find({username: 'admin'}, function (err, users) {
if (users.length === 0) {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'admin',
password: password,
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
});
// Then save the user
user.save(function (err) {
if (err) {
console.log('Failed to add local admin');
} else {
console.log(chalk.bold.red('Local admin added with password set to ' + password));
}
});
} else {
console.log('Admin user exists');
}
});
} else {
//Add Local User
User.find({username: 'user'}).remove(function () {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'user',
password: password,
provider: 'local',
email: 'user@localhost.com',
firstName: 'User',
lastName: 'Local',
displayName: 'User Local',
roles: ['user']
});
// Then save the user
user.save(function (err) {
if (err) {
console.log('Failed to add local user');
} else {
console.log(chalk.bold.red('Local user added with password set to ' + password));
}
});
});


//Add Local Admin
User.find({username: 'admin'}).remove(function () {
var password = crypto.randomBytes(64).toString('hex').slice(1, 20);
var user = new User({
username: 'admin',
password: password,
provider: 'local',
email: 'admin@localhost.com',
firstName: 'Admin',
lastName: 'Local',
displayName: 'Admin Local',
roles: ['user', 'admin']
});
// Then save the user
user.save(function (err) {
if (err) {
console.log('Failed to add local admin');
} else {
console.log(chalk.bold.red('Local admin added with password set to ' + password));
}
});
});
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
"async": "^1.3.0",
"body-parser": "^1.13.1",
"bower": "^1.4.1",
"cfenv": "~1.0.0",
"chalk": "^1.1.0",
"compression": "^1.5.0",
"connect-flash": "~0.1.1",
"connect-mongo": "~0.8.1",
"consolidate": "~0.13.1",
"cookie-parser": "^1.3.2",
"crypto": "0.0.3",
"express": "^4.13.1",
"express-session": "^1.11.3",
"forever": "~0.14.2",
"glob": "^5.0.13",
"grunt-cli": "~0.1.13",
"grunt": "0.4.5",
"grunt-cli": "~0.1.13",
"helmet": "~0.9.1",
"jasmine-core": "^2.3.4",
"lodash": "^3.10.0",
Expand All @@ -57,8 +59,7 @@
"serve-favicon": "^2.3.0",
"socket.io": "^1.3.5",
"swig": "^1.4.2",
"validator": "^3.41.2",
"cfenv": "~1.0.0"
"validator": "^3.41.2"
},
"devDependencies": {
"grunt-concurrent": "^2.0.0",
Expand Down

0 comments on commit d41490c

Please sign in to comment.