Skip to content

Commit 80950f8

Browse files
authored
Merge pull request #707 from Nebukadneza/add_cmdline_usermanager
Add simple user-management tool for emailsignin
2 parents be02aed + 31f1db4 commit 80950f8

4 files changed

Lines changed: 94 additions & 3 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- export PATH="$HOME/.yarn/bin:$PATH"
2323
- env: task=ShellCheck
2424
script:
25-
- shellcheck bin/*
25+
- shellcheck bin/heroku bin/setup
2626
language: generic
2727
- env: task=doctoc
2828
install: npm install doctoc

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ There are some configs you need to change in the files below
188188
| HMD_IMGUR_CLIENTID | no example | Imgur API client id |
189189
| HMD_EMAIL | `true` or `false` | set to allow email signin |
190190
| HMD_ALLOW_PDF_EXPORT | `true` or `false` | Enable or disable PDF exports |
191-
| HMD_ALLOW_EMAIL_REGISTER | `true` or `false` | set to allow email register (only applied when email is set, default is `true`) |
191+
| HMD_ALLOW_EMAIL_REGISTER | `true` or `false` | set to allow email register (only applied when email is set, default is `true`. Note `bin/manage_users` might help you if registration is `false`.) |
192192
| HMD_IMAGE_UPLOAD_TYPE | `imgur`, `s3`, `minio` or `filesystem` | Where to upload image. For S3, see our Image Upload Guides for [S3](docs/guides/s3-image-upload.md) or [Minio](docs/guides/minio-image-upload.md) |
193193
| HMD_S3_ACCESS_KEY_ID | no example | AWS access key id |
194194
| HMD_S3_SECRET_ACCESS_KEY | no example | AWS secret key |
@@ -246,7 +246,7 @@ There are some configs you need to change in the files below
246246
| heartbeattimeout | `10000` | socket.io heartbeat timeout |
247247
| documentmaxlength | `100000` | note max length |
248248
| email | `true` or `false` | set to allow email signin |
249-
| allowemailregister | `true` or `false` | set to allow email register (only applied when email is set, default is `true`) |
249+
| allowemailregister | `true` or `false` | set to allow email register (only applied when email is set, default is `true`. Note `bin/manage_users` might help you if registration is `false`.) |
250250
| imageuploadtype | `imgur`(default), `s3`, `minio` or `filesystem` | Where to upload image
251251
| minio | `{ "accessKey": "YOUR_MINIO_ACCESS_KEY", "secretKey": "YOUR_MINIO_SECRET_KEY", "endpoint": "YOUR_MINIO_HOST", port: 9000, secure: true }` | When `imageuploadtype` is set to `minio`, you need to set this key. Also checkout our [Minio Image Upload Guide](docs/guides/minio-image-upload.md) |
252252
| s3 | `{ "accessKeyId": "YOUR_S3_ACCESS_KEY_ID", "secretAccessKey": "YOUR_S3_ACCESS_KEY", "region": "YOUR_S3_REGION" }` | When `imageuploadtype` be set to `s3`, you would also need to setup this key, check our [S3 Image Upload Guide](docs/guides/s3-image-upload.md) |

bin/manage_users

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
3+
// First configure the logger so it does not spam the console
4+
const logger = require("../lib/logger");
5+
logger.transports.console.level = "warning";
6+
7+
const models = require("../lib/models/");
8+
const readline = require("readline-sync");
9+
const minimist = require("minimist");
10+
11+
var usage = `
12+
13+
Command-line utility to create users for email-signin.
14+
15+
Usage: bin/manage_users [--pass password] (--add | --del) user-email
16+
Options:
17+
--add Add user with the specified user-email
18+
--del Delete user with specified user-email
19+
--pass Use password from cmdline rather than prompting
20+
`
21+
22+
// Using an async function to be able to use await inside
23+
async function createUser(argv) {
24+
var existing_user = await models.User.findOne({where: {email: argv["add"]}});
25+
// Cannot create already-existing users
26+
if(existing_user != undefined) {
27+
console.log("User with e-mail "+existing_user.email+" already exists! Aborting ...");
28+
process.exit(1);
29+
}
30+
31+
// Find whether we use cmdline or prompt password
32+
if(argv["pass"] == undefined) {
33+
var pass = readline.question("Password for "+argv["add"]+":", {hideEchoBack: true});
34+
} else {
35+
console.log("Using password from commandline...");
36+
var pass = argv["pass"];
37+
}
38+
39+
// Lets try to create, and check success
40+
var ref = await models.User.create({email: argv["add"], password: pass});
41+
if(ref == undefined) {
42+
console.log("Could not create user with email "+argv["add"]);
43+
process.exit(1);
44+
} else
45+
console.log("Created user with email "+argv["add"]);
46+
}
47+
48+
// Using an async function to be able to use await inside
49+
async function deleteUser(argv) {
50+
// Cannot delete non-existing users
51+
var existing_user = await models.User.findOne({where: {email: argv["del"]}});
52+
if(existing_user == undefined) {
53+
console.log("User with e-mail "+argv["del"]+" does not exist, cannot delete");
54+
process.exit(1);
55+
}
56+
57+
// Sadly .destroy() does not return any success value with all
58+
// backends. See sequelize #4124
59+
await existing_user.destroy();
60+
console.log("Deleted user "+argv["del"]+" ...");
61+
}
62+
63+
// Perform commandline-parsing
64+
var argv = minimist(process.argv.slice(2));
65+
66+
// Check for add/delete missing
67+
if (argv["add"] == undefined && argv["del"] == undefined) {
68+
console.log("You did not specify either --add or --del!");
69+
console.log(usage);
70+
process.exit(1);
71+
}
72+
73+
// Check if both are specified
74+
if (argv["add"] != undefined && argv["del"] != undefined) {
75+
console.log("You cannot add and delete at the same time!");
76+
console.log(usage);
77+
process.exit(1);
78+
}
79+
80+
// Call respective processing functions
81+
if (argv["add"] != undefined) {
82+
createUser(argv).then(function() {
83+
process.exit(0);
84+
});
85+
} else if (argv["del"] != undefined) {
86+
deleteUser(argv).then(function() {
87+
process.exit(0);
88+
})
89+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"mattermost": "^3.4.0",
8080
"meta-marked": "^0.4.2",
8181
"method-override": "^2.3.7",
82+
"minimist": "^1.2.0",
8283
"minio": "^3.1.3",
8384
"moment": "^2.17.1",
8485
"morgan": "^1.7.0",
@@ -103,6 +104,7 @@
103104
"prismjs": "^1.6.0",
104105
"randomcolor": "^0.4.4",
105106
"raphael": "git+https://github.com/dmitrybaranovskiy/raphael",
107+
"readline-sync": "^1.4.7",
106108
"request": "^2.79.0",
107109
"reveal.js": "~3.6.0",
108110
"scrypt": "^6.0.3",

0 commit comments

Comments
 (0)