-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user.js
executable file
·131 lines (124 loc) · 4.25 KB
/
create_user.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
#!/usr/bin/env node
/**
* @overview Create an user on elasticsearch.
*
* The script asks the name of the user to create and (twice) the password,
* which will be crypted using {@link https://www.npmjs.com/package/bcrypt-nodejs|bcrypt}.
*/
const common = require('../common');
const client = common.createClient();
const async = require('async');
const promptly = require('promptly');
const bcrypt = require('bcrypt-nodejs');
async.waterfall([
// Fetch the existing users
function(callback) {
client.get({
index: 'main',
type: 'users',
id: common.usersDocId
}, function(err, resp, _respcode) {
let res;
if (err) {
res = {users: []};
}
else {
res = {users: resp._source.users};
}
callback(null, res);
});
},
// Username
function(res, callback) {
const nameValidator = function(value) {
if (value.length < 5) {
throw new Error('The username must have a minimum length of 5');
}
return value;
};
const opt = {validator: nameValidator};
promptly.prompt('Username: ', opt, function(err, value) {
res.username = value;
callback(err, res);
});
},
// Verify if the username already exists and ask for confirmation
function(res, callback) {
function filterFn(item) {
return item.username !== res.username;
}
const filteredUsers = res.users.filter(filterFn);
if (filteredUsers.length !== res.users.length) {
promptly.confirm(
'You are going to overwrite an existing user. Are you sure?',
function(err, value) {
if (!value) {
process.exit(0);
}
else {
res.users = filteredUsers;
callback(null, res);
}
});
}
else {
callback(null, res);
}
},
// Password
function(res, callback) {
const pwdValidator = function(value) {
if (value === res.username) {
throw new Error('The password must be different from the username');
}
if (value.length < 5) {
throw new Error('The password must have a minimum length of 5');
}
return value;
};
const opt = {validator: pwdValidator};
promptly.password('Type a password: ', opt, function(err, value) {
res.password = value;
callback(err, res);
});
},
// Password confirmation
function(res, callback) {
const confirmPwdValidator = function(value) {
if (value !== res.password) {
throw new Error('The password does not match the one previously entered');
}
return value;
};
const opt = {validator: confirmPwdValidator};
promptly.password('Confirm the password: ', opt, function(err, _value) {
callback(err, res);
});
},
// Save the users on db
function(res, callback) {
res.users.push({
username: res.username,
password: bcrypt.hashSync(res.password, bcrypt.genSaltSync(10))
});
client.index({
index: 'main',
type: 'users',
id: common.usersDocId,
body: {
users: res.users
}
}, function(err, resp, _respcode) {
callback(err, res);
});
}
],
function(err, results) {
if (err) {
console.log(err, results);
}
else {
console.log("User successfully created!");
}
process.exit(err ? 1 : 0);
});