2018-01-27 21:34:52 +00:00
|
|
|
#!/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");
|
|
|
|
|
|
|
|
var usage = `
|
|
|
|
|
|
|
|
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
|
|
|
|
`
|
|
|
|
|
|
|
|
// Using an async function to be able to use await inside
|
|
|
|
async function createUser(argv) {
|
|
|
|
var 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
|
|
|
|
if(argv["pass"] == undefined) {
|
|
|
|
var pass = readline.question("Password for "+argv["add"]+":", {hideEchoBack: true});
|
|
|
|
} else {
|
|
|
|
console.log("Using password from commandline...");
|
2018-01-29 21:37:53 +00:00
|
|
|
var pass = "" + argv["pass"];
|
2018-01-27 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lets try to create, and check success
|
|
|
|
var 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
|
|
|
|
var 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"]+" ...");
|
|
|
|
}
|
|
|
|
|
2018-10-29 14:03:05 +00:00
|
|
|
var options = {
|
|
|
|
add: createUser,
|
|
|
|
del: deleteUser,
|
|
|
|
};
|
|
|
|
|
2018-01-27 21:34:52 +00:00
|
|
|
// Perform commandline-parsing
|
|
|
|
var argv = minimist(process.argv.slice(2));
|
|
|
|
|
2018-10-29 14:03:05 +00:00
|
|
|
var keys = Object.keys(options);
|
|
|
|
var opts = keys.filter((key) => argv[key] !== undefined);
|
|
|
|
var action = opts[0];
|
|
|
|
|
|
|
|
// Check for options missing
|
|
|
|
if (opts.length === 0) {
|
|
|
|
console.log(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`);
|
2018-01-27 21:34:52 +00:00
|
|
|
console.log(usage);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if both are specified
|
2018-10-29 14:03:05 +00:00
|
|
|
if (opts.length > 1) {
|
|
|
|
console.log(`You cannot ${action.join(' and ')} at the same time!`);
|
2018-01-27 21:34:52 +00:00
|
|
|
console.log(usage);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call respective processing functions
|
2018-10-29 14:03:05 +00:00
|
|
|
options[action](argv).then(function() {
|
|
|
|
process.exit(0);
|
|
|
|
});
|