|
| 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 | +} |
0 commit comments