-
Notifications
You must be signed in to change notification settings - Fork 4
/
first-time-setup.js
executable file
·130 lines (105 loc) · 3.21 KB
/
first-time-setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const Async = require('async');
const Config = require('./config');
const Joi = require('joi');
const MongoModels = require('hicsail-mongo-models');
const Mongodb = require('mongodb');
const Promptly = require('promptly');
const User = require('./server/models/user');
const PasswordComplexity = require('joi-password-complexity');
Async.auto({
mongodbUri: (done) => {
const options = {
default: 'mongodb://localhost:27017/anchor'
};
Promptly.prompt(`MongoDB URI: (${options.default})`, options, done);
},
testMongo: ['mongodbUri', (results, done) => {
Mongodb.MongoClient.connect(results.mongodbUri, {}, (err, db) => {
if (err) {
console.error('Failed to connect to Mongodb.');
return done(err);
}
db.close();
done(null, true);
});
}],
rootEmail: ['testMongo', (results, done) => {
Promptly.prompt('Root user email:', done);
}],
rootPassword: ['rootEmail', (results, done) => {
Promptly.password('Root user password:', done);
}],
rootPasswordCheck: ['rootPassword', (results, done) => {
const complexityOptions = Config.get('/passwordComplexity');
Joi.validate(results.rootPassword, new PasswordComplexity(complexityOptions),done);
}],
setupRootUser: ['rootPassword', (results, done) => {
Async.auto({
connect: function (done) {
MongoModels.connect(results.mongodbUri, {}, done);
},
rootUser: ['connect', (dbResults, done) => {
User.findOne({ username: 'root' }, done);
}],
rootUserCheck: ['rootUser', (dbResults, done) => {
if (results.rootUser) {
return done(Error('Root User already exists'));
}
done();
}],
userEmail:['rootUserCheck', function (dbResults, done) {
User.findOne({ email: results.rootEmail }, done);
}],
emailCheck:['userEmail', function (dbResults, done) {
console.log(dbResults.userEmail);
if (dbResults.userEmail) {
done(Error('Email is in use'));
}
else {
done();
}
}],
user: ['emailCheck', function (dbResults, done) {
Async.auto({
passwordHash: function (done) {
User.generatePasswordHash(results.rootPassword,done);
}
}, (err, passResults) => {
if (err) {
return done(err);
}
const document = {
_id: User.ObjectId('000000000000000000000000'),
isActive: true,
username: 'root',
name: 'Root',
password: passResults.passwordHash.hash,
email: results.rootEmail.toLowerCase(),
roles: {
root: true
},
timeCreated: new Date()
};
User.insertOne(document, (err, docs) => {
done(err, docs && docs[0]);
});
});
}]
}, (err, dbResults) => {
if (err) {
console.error('Failed to setup root user.');
return done(err);
}
done(null, true);
});
}]
}, (err, results) => {
if (err) {
console.error('Setup failed.');
console.error(err);
return process.exit(1);
}
console.log('Setup complete.');
process.exit(0);
});