From ea97f2e7d83281a5bf34cb294ce9f1483135dbf2 Mon Sep 17 00:00:00 2001 From: Andrew Throener Date: Thu, 13 Aug 2015 09:37:24 -0500 Subject: [PATCH] Database seeding --- config/env/development.js | 3 +- config/env/production.js | 3 +- config/env/test.js | 3 +- config/lib/app.js | 5 +++ config/lib/seed.js | 85 +++++++++++++++++++++++++++++++++++++++ package.json | 7 ++-- 6 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 config/lib/seed.js diff --git a/config/env/development.js b/config/env/development.js index 1f31ec53f6..39659d564d 100644 --- a/config/env/development.js +++ b/config/env/development.js @@ -65,5 +65,6 @@ module.exports = { } } }, - livereload: true + livereload: true, + seedDB: process.env.MONGO_SEED || false }; diff --git a/config/env/production.js b/config/env/production.js index 3bf482cc85..63c7f3dc45 100644 --- a/config/env/production.js +++ b/config/env/production.js @@ -61,5 +61,6 @@ module.exports = { pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' } } - } + }, + seedDB: process.env.MONGO_SEED || false }; diff --git a/config/env/test.js b/config/env/test.js index cbd9821795..2efb791e64 100644 --- a/config/env/test.js +++ b/config/env/test.js @@ -56,5 +56,6 @@ module.exports = { pass: process.env.MAILER_PASSWORD || 'MAILER_PASSWORD' } } - } + }, + seedDB: process.env.MONGO_SEED || false }; diff --git a/config/lib/app.js b/config/lib/app.js index 1feede39ed..76c0bf779c 100644 --- a/config/lib/app.js +++ b/config/lib/app.js @@ -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(); }; diff --git a/config/lib/seed.js b/config/lib/seed.js new file mode 100644 index 0000000000..f75db3a0bf --- /dev/null +++ b/config/lib/seed.js @@ -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)); + } + }); + }); +} diff --git a/package.json b/package.json index 62049a76d4..c9a92c38ca 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",