Skip to content

Commit

Permalink
pantsel#168 combining hard coded usernames and passwords into single …
Browse files Browse the repository at this point in the history
…location
  • Loading branch information
rmetcalf9 committed Feb 5, 2018
1 parent 8cf04ba commit 5518c35
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
29 changes: 20 additions & 9 deletions api/hooks/load-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var async = require('async');
var _ = require('lodash')
var defUserSeedData = require('../../config/default-user-seed-data.js');

/**
* load-db.js
Expand Down Expand Up @@ -29,15 +30,25 @@ module.exports = function hook(sails) {
var passportsFns = []
users.forEach(function(user){
passportsFns.push(function(_cb){
sails.models.passport
.create({
protocol: "local",
password : user.username == 'admin' ? 'adminadminadmin' : 'demodemodemo',
user : user.id
}).exec(function(err,passport){
if(err) return _cb(err)
return _cb(null)
})
var passwordToSetArr = defUserSeedData.seedData.filter( function (orig) {
return (orig.username == user.username)
});
var passwordToSet = undefined;
if (passwordToSetArr.length == [1]) {
passwordToSet = passwordToSetArr[0].password;
}
// Only set the password if we have one
if (typeof(passwordToSet) != 'undefined') {
sails.models.passport
.create({
protocol: "local",
password : passwordToSet,
user : user.id
}).exec(function(err,passport){
if(err) return _cb(err)
return _cb(null)
})
};
})
})

Expand Down
35 changes: 15 additions & 20 deletions api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var _ = require('lodash');
var async = require('async');
var uuid = require('node-uuid');
var defUserSeedData = require('../../config/default-user-seed-data.js');

/**
* User.js
Expand Down Expand Up @@ -100,26 +101,20 @@ var defaultModel = _.merge(_.cloneDeep(require('../base/Model')), {
}
},

seedData: [
{
"username": "admin",
"email": "admin@some.domain",
"firstName": "Arnold",
"lastName": "Administrator",
"node_id": "http://kong:8001",
"admin": true,
"active" : true
},
{
"username": "demo",
"email": "demo@some.domain",
"firstName": "John",
"lastName": "Doe",
"node_id": "http://kong:8001",
"admin": false,
"active" : true
}
]
//seedData object should now come from a file
// the new object has had the password field added
// we need to remove it
seedData: defUserSeedData.seedData.map( function (orig) {
return {
"username": orig.username,
"email": orig.email,
"firstName": orig.firstName,
"lastName": orig.lastName,
"node_id": orig.node_id,
"admin": orig.admin,
"active" : orig.active
}
})
});

var mongoModel = function () {
Expand Down
30 changes: 30 additions & 0 deletions config/default-user-seed-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Created by rmetcalf9 on 5/2/2018.
*/
'use strict';

module.exports = {
seedData: [
{
"username": "admin",
"email": "admin@some.domain",
"firstName": "Arnold",
"lastName": "Administrator",
"node_id": "http://kong:8001",
"admin": true,
"active" : true,
"password": "adminadminadmin"
},
{
"username": "demo",
"email": "demo@some.domain",
"firstName": "John",
"lastName": "Doe",
"node_id": "http://kong:8001",
"admin": false,
"active" : true,
"password": "demodemodemo"
}
]
}

0 comments on commit 5518c35

Please sign in to comment.