Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit 4b70c09

Browse files
author
Ilya Radchenko
committed
Remove force, for and option to update the user
1 parent 493bd27 commit 4b70c09

File tree

5 files changed

+70
-60
lines changed

5 files changed

+70
-60
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ Options:
3737
-l User's email address
3838
-p User's password
3939
-a Specify if this is an admin (flag) (default: false)
40-
-f Force create user, overwrites previous user with the same email address (flag) (default: false)
4140
```
4241

42+
If a user exists with the given email address, you will have an option to update
43+
that user, or cancel the process.
44+
4345
### restart
4446

4547
Restart strider (touch .strider)

add-user.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,15 @@ module.exports = function(deps) {
1818
output: process.stdout
1919
});
2020

21-
Step(
22-
function getEmail() {
21+
Step(function getEmail() {
2322
var next = this;
2423

2524
if (email) {
2625
next();
2726
} else {
2827
rl.question('Enter email []: ', function (em) {
2928
email = em;
30-
31-
User.findByEmail(email, function (err, users) {
32-
if (users.length) {
33-
if (force) {
34-
User.remove({ email: email }, function (error) {
35-
if (error) {
36-
console.error('Unable to remove existing user with email \'%s\'', email);
37-
process.exit(1);
38-
}
39-
40-
next();
41-
});
42-
} else {
43-
console.error('User already exists with the \'%s\' email address. Please enter a unique email address.', email);
44-
email = undefined;
45-
getEmail.call(next);
46-
}
47-
} else {
48-
next();
49-
}
50-
});
29+
next();
5130
});
5231
}
5332
},
@@ -92,24 +71,23 @@ module.exports = function(deps) {
9271

9372
process.stdout.write('\nEmail:\t\t' + email + '\n');
9473
process.stdout.write('Password:\t' + password.replace(/.*/, '****') + '\n');
95-
process.stdout.write('isAdmin:\t' + (level ? 'y' : 'n') + '\n');
96-
97-
rl.question('OK? (y/n) [y]', function (ok) {
98-
if (ok === 'y' || ok === '') {
99-
next();
100-
} else {
101-
console.log('Goodbye!');
102-
process.exit();
103-
}
104-
})
74+
process.stdout.write('isAdmin:\t' + (level ? 'y' : 'n') + '\n');
75+
76+
rl.question('OK? (y/n) [y]', function (ok) {
77+
if (ok === 'y' || ok === '') {
78+
next();
79+
} else {
80+
console.log('Goodbye!');
81+
process.exit();
82+
}
83+
})
10584
},
10685

10786
function save() {
108-
saveUser(email, password, level, force);
109-
}
110-
);
87+
saveUser(email, password, level);
88+
});
11189
} else {
112-
saveUser(email, password, level, force);
90+
saveUser(email, password, level);
11391
}
11492
}
11593

commands/addUser.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ module.exports = function(deps, parser) {
1515
'default': false,
1616
flag: true
1717
})
18-
.option('force', {
19-
abbr: 'f',
20-
help: 'Force create user, overwrites previous user with the same email address',
21-
'default': false,
22-
flag: true
23-
})
2418
.callback(function(opts){
2519
deps.connect(function(err) {
2620
if (err) {

create-user.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
11
'use strict';
22

3+
var readline = require('readline');
4+
35
module.exports = function(deps) {
46
var User = deps.models().User;
57

6-
function createUser(email, password, admin, force) {
7-
var u = new User();
8-
9-
u.email = email;
10-
u.created = new Date();
11-
u.set('password', password);
12-
u.account_level = admin;
13-
14-
u.save(function(err) {
8+
function createUser(email, password, admin) {
9+
User.findByEmail(email, function (err, users) {
1510
if (err) {
16-
console.log('Error adding user:', err);
11+
console.error('Failed to lookup users, please let us know at https://github.com/Strider-CD/strider-cli/issues: ', err);
1712
process.exit(1);
1813
}
1914

20-
console.log('\nUser %s successfully! Enjoy.', force ? 'overwritten' : 'added');
21-
process.exit();
15+
if (users.length) {
16+
var rl = readline.createInterface({
17+
input: process.stdin,
18+
output: process.stdout
19+
});
20+
21+
rl.question('User already exists, overwrite? (y/n) [n]: ', function (overwrite) {
22+
if (overwrite) {
23+
User.update({ email: email }, {
24+
password: password,
25+
account_level: admin
26+
}, function (err) {
27+
if (err) {
28+
console.log('Error updating user:', err);
29+
process.exit(1);
30+
}
31+
32+
console.log('User updated successfully! Enjoy.');
33+
process.exit();
34+
});
35+
} else {
36+
console.log('addUser cancelled');
37+
process.exit();
38+
}
39+
});
40+
} else {
41+
var u = new User();
42+
43+
u.email = email;
44+
u.created = new Date();
45+
u.set('password', password);
46+
u.account_level = admin;
47+
48+
u.save(function(err) {
49+
if (err) {
50+
console.log('Error adding user:', err);
51+
process.exit(1);
52+
}
53+
54+
console.log('User created successfully! Enjoy.');
55+
process.exit();
56+
});
57+
}
2258
});
2359
}
2460

save-user.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = function(deps) {
55
var Upgrade = deps.upgrade()
66
, Config = deps.models().Config
77

8-
function saveUser(email, password, admin, force) {
8+
function saveUser(email, password, admin) {
99
Upgrade.isFreshDb(function (err, isFresh) {
1010
if (isFresh) {
1111
Upgrade.needConfigObj(function (err, needsConfig) {
@@ -15,15 +15,15 @@ module.exports = function(deps) {
1515
c.version = Config.SCHEMA_VERSION;
1616

1717
c.save(function () {
18-
createUser(email, password, admin, force);
18+
createUser(email, password, admin);
1919
});
2020
} else {
21-
createUser(email, password, admin, force);
21+
createUser(email, password, admin);
2222
}
2323
});
2424
} else {
2525
Upgrade.ensure(Config.SCHEMA_VERSION, function () {
26-
createUser(email, password, admin, force);
26+
createUser(email, password, admin);
2727
});
2828
}
2929
});

0 commit comments

Comments
 (0)