summaryrefslogtreecommitdiff
path: root/bin/manage_users
blob: f748e342330008134ae6f1434fc07ea04fb03be2 (plain)
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
#!/usr/bin/env node

// First configure the logger so it does not spam the console
const logger = require("../lib/logger");
logger.transports.console.level = "warning";

const models = require("../lib/models/");
const readline = require("readline-sync");
const minimist = require("minimist");

function showUsage(tips) {
	console.log(`${tips}

Command-line utility to create users for email-signin.

Usage: bin/manage_users [--pass password] (--add | --del) user-email
	Options:
		--add 	Add user with the specified user-email
		--del 	Delete user with specified user-email
		--pass	Use password from cmdline rather than prompting
`);
	process.exit(1);
}

// Using an async function to be able to use await inside
async function createUser(argv) {
	const existing_user = await models.User.findOne({where: {email: argv["add"]}});
	// Cannot create already-existing users
	if(existing_user != undefined) {
		console.log(`User with e-mail ${existing_user.email} already exists! Aborting ...`);
		process.exit(1);
	}

	// Find whether we use cmdline or prompt password
	let pass;
	if(argv["pass"] == undefined) {
		pass = readline.question(`Password for ${argv["add"]}:`, {hideEchoBack: true});
	} else {
		console.log("Using password from commandline...");
		pass = "" + argv["pass"];
	}

	// Lets try to create, and check success
	const ref = await models.User.create({email: argv["add"], password: pass});
	if(ref == undefined) {
		console.log(`Could not create user with email ${argv["add"]}`);
		process.exit(1);
	} else
		console.log(`Created user with email ${argv["add"]}`);
}

// Using an async function to be able to use await inside
async function deleteUser(argv) {
	// Cannot delete non-existing users
	const existing_user = await models.User.findOne({where: {email: argv["del"]}});
	if(existing_user === undefined) {
		console.log(`User with e-mail ${argv["del"]} does not exist, cannot delete`);
		process.exit(1);
	}

	// Sadly .destroy() does not return any success value with all
	// backends. See sequelize #4124
	await existing_user.destroy();
	console.log(`Deleted user ${argv["del"]} ...`);
}

var options = {
	add: createUser,
	del: deleteUser,
};

// Perform commandline-parsing
const argv = minimist(process.argv.slice(2));

const keys = Object.keys(options);
const opts = keys.filter((key) => argv[key] !== undefined);
const action = opts[0];

// Check for options missing
if (opts.length === 0) {
	showUsage(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`);
}

// Check if both are specified
if (opts.length > 1) {
	showUsage(`You cannot ${action.join(' and ')} at the same time!`);
}

// Call respective processing functions
options[action](argv).then(function() {
  process.exit(0);
});