]>
Commit | Line | Data |
---|---|---|
75d612ce | 1 | import * as program from 'commander' |
91fea9fc | 2 | import { initDatabaseModels } from '../server/initializers' |
3fd3ab2d | 3 | import { UserModel } from '../server/models/account/user' |
e1fa0266 | 4 | import { isUserPasswordValid } from '../server/helpers/custom-validators/users' |
7df5e5e4 C |
5 | |
6 | program | |
7 | .option('-u, --user [user]', 'User') | |
7df5e5e4 C |
8 | .parse(process.argv) |
9 | ||
0e1dc3e7 | 10 | if (program['user'] === undefined) { |
7df5e5e4 C |
11 | console.error('All parameters are mandatory.') |
12 | process.exit(-1) | |
13 | } | |
14 | ||
91fea9fc | 15 | initDatabaseModels(true) |
6fcd19ba | 16 | .then(() => { |
3fd3ab2d | 17 | return UserModel.loadByUsername(program['user']) |
6fcd19ba C |
18 | }) |
19 | .then(user => { | |
7df5e5e4 C |
20 | if (!user) { |
21 | console.error('User unknown.') | |
fdbda9e3 | 22 | process.exit(-1) |
7df5e5e4 C |
23 | } |
24 | ||
c129e2a1 C |
25 | const readline = require('readline') |
26 | const Writable = require('stream').Writable | |
27 | const mutableStdout = new Writable({ | |
28 | write: function (chunk, encoding, callback) { | |
29 | callback() | |
7df5e5e4 | 30 | } |
c129e2a1 C |
31 | }) |
32 | const rl = readline.createInterface({ | |
33 | input: process.stdin, | |
34 | output: mutableStdout, | |
35 | terminal: true | |
36 | }) | |
37 | ||
38 | console.log('New password?') | |
39 | rl.on('line', function (password) { | |
e1fa0266 C |
40 | if (!isUserPasswordValid(password)) { |
41 | console.error('New password is invalid.') | |
42 | process.exit(-1) | |
43 | } | |
44 | ||
c129e2a1 C |
45 | user.password = password |
46 | ||
6fcd19ba C |
47 | user.save() |
48 | .then(() => console.log('User password updated.')) | |
49 | .catch(err => console.error(err)) | |
50 | .finally(() => process.exit(0)) | |
7df5e5e4 C |
51 | }) |
52 | }) |