40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
/*
|
|
This is not a server file, but a command system to make a user an admin via a command.
|
|
|
|
Usage (in the root of the folder): node ./scripts/make-admin.js <username>
|
|
|
|
After making your first admin user, you can add admins via the admin dashboard.
|
|
*/
|
|
|
|
const a = process.argv.slice(2).join(" ");
|
|
|
|
const config = require("../config.json")
|
|
const {MongoClient} = require("mongodb");
|
|
const client = new MongoClient(config["db-url"], {useUnifiedTopology: true});
|
|
|
|
(async function() {
|
|
console.log("Connecting to DB...");
|
|
await client.connect();
|
|
console.log("- Connected to DB.");
|
|
console.log(`Finding account by username "${a}"`);
|
|
let db = await client.db("mediahost");
|
|
let accs = await db.collection("accounts");
|
|
let acc = await accs.findOne({username: a});
|
|
let nw = acc;
|
|
if (acc == null) {
|
|
console.log(`[error] Could not find account by username "${a}"`);
|
|
process.exit(1);
|
|
}
|
|
console.log(`- Found account (id: ${acc.id}, username: ${a})`);
|
|
nw.admin = true;
|
|
console.log(`Updating account...`);
|
|
let action = await accs.findOneAndReplace({username: a}, nw);
|
|
if (action.ok == 1) {
|
|
console.log(`User action for user "${a}" complete.`);
|
|
process.exit();
|
|
} else {
|
|
console.log(`[Error] Error for user "${a}" action: `);
|
|
console.log(action);
|
|
process.exit(1);
|
|
}
|
|
})() |