This repository has been archived by the owner on Mar 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
/
first-time-setup.js
172 lines (140 loc) · 5.32 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const Async = require('async');
const MongoModels = require('mongo-models');
const Mongodb = require('mongodb');
const Promptly = require('promptly');
Async.auto({
mongodbUri: (done) => {
const options = {
default: 'mongodb://localhost:27017/aqua'
};
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);
}],
setupRootUser: ['rootPassword', (results, done) => {
const Account = require('./server/models/account');
const AdminGroup = require('./server/models/admin-group');
const Admin = require('./server/models/admin');
const AuthAttempt = require('./server/models/auth-attempt');
const Session = require('./server/models/session');
const Status = require('./server/models/status');
const User = require('./server/models/user');
Async.auto({
connect: function (done) {
MongoModels.connect(results.mongodbUri, {}, done);
},
clean: ['connect', (dbResults, done) => {
Async.parallel([
Account.deleteMany.bind(Account, {}),
AdminGroup.deleteMany.bind(AdminGroup, {}),
Admin.deleteMany.bind(Admin, {}),
AuthAttempt.deleteMany.bind(AuthAttempt, {}),
Session.deleteMany.bind(Session, {}),
Status.deleteMany.bind(Status, {}),
User.deleteMany.bind(User, {})
], done);
}],
adminGroup: ['clean', function (dbResults, done) {
AdminGroup.create('Root', done);
}],
admin: ['clean', function (dbResults, done) {
const document = {
_id: Admin.ObjectId('111111111111111111111111'),
name: {
first: 'Root',
middle: '',
last: 'Admin'
},
timeCreated: new Date()
};
Admin.insertOne(document, (err, docs) => {
done(err, docs && docs[0]);
});
}],
user: ['clean', function (dbResults, done) {
Async.auto({
passwordHash: User.generatePasswordHash.bind(this, results.rootPassword)
}, (err, passResults) => {
if (err) {
return done(err);
}
const document = {
_id: Admin.ObjectId('000000000000000000000000'),
isActive: true,
username: 'root',
password: passResults.passwordHash.hash,
email: results.rootEmail.toLowerCase(),
timeCreated: new Date()
};
User.insertOne(document, (err, docs) => {
done(err, docs && docs[0]);
});
});
}],
adminMembership: ['admin', function (dbResults, done) {
const id = dbResults.admin._id.toString();
const update = {
$set: {
groups: {
root: 'Root'
}
}
};
Admin.findByIdAndUpdate(id, update, done);
}],
linkUser: ['admin', 'user', function (dbResults, done) {
const id = dbResults.user._id.toString();
const update = {
$set: {
'roles.admin': {
id: dbResults.admin._id.toString(),
name: 'Root Admin'
}
}
};
User.findByIdAndUpdate(id, update, done);
}],
linkAdmin: ['admin', 'user', function (dbResults, done) {
const id = dbResults.admin._id.toString();
const update = {
$set: {
user: {
id: dbResults.user._id.toString(),
name: 'root'
}
}
};
Admin.findByIdAndUpdate(id, update, done);
}]
}, (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);
});