From 40ca01c48932e96b3480b3972977b730133cc96e Mon Sep 17 00:00:00 2001 From: lesion Date: Thu, 28 Mar 2024 13:24:46 +0100 Subject: [PATCH] feat: new `users setrole` CLI subcommand --- server/cli/users.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/server/cli/users.js b/server/cli/users.js index e1e8b530..9b89c624 100644 --- a/server/cli/users.js +++ b/server/cli/users.js @@ -36,15 +36,31 @@ async function create (args) { email: args.email, password: args.password, is_active: true, - is_admin: args.admin || false + role: args.role || 'user' }) - console.error(`User ${args.email} created`) + console.error(`User "${args.email}" created with role ${user.role}`) } catch(e) { console.error(String(e)) } await db.close() } +async function setRole (args) { + await _initializeDB() + const { User } = require('../api/models/models') + const user = await User.findOne({ where: { email: args.email } }) + console.log() + if (!user) { + console.error(`User ${args.email} not found`) + return + } + + user.role = args.role + await user.save() + console.log(`User ${user.email} role is now ${user.role}!`) + await db.close() +} + async function remove (args) { await _initializeDB() @@ -66,7 +82,7 @@ async function list () { const { User } = require('../api/models/models') const users = await User.findAll() console.log() - users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email}`)) + users.forEach(u => console.log(`${u.id}\trole: ${u.role}\tenabled: ${u.is_active}\temail: ${u.email}`)) console.log() await db.close() } @@ -75,14 +91,13 @@ const usersCLI = yargs => yargs .command('list', 'List all users', list) .command('reset-password ', 'Resets the password of the given user', { }, resetPassword) - .command('set-admin ', 'Set administrator privileges to the given user', {}, setAdmin) - .command('unset-admin ', 'Remove administrator privileges to the given user', {}, unsetAdmin) - .command('create [password] [admin]', 'Create an user', { - admin: { describe: 'Define this user as administrator', type: 'boolean' }, - }, create) - .command('remove ', 'Remove an user', {}, remove) - .positional('email', { describe: 'user email or username', type: 'string', demandOption: true }) - .positional('password', { describe: 'Password', type: 'string', demandOption: false }) + .command('set_role ', 'Set specified role privileges to the given user', + { role: { describe: 'Define this user role', choices: ['user', 'admin', 'editor' ] } }, setRole) + .command('create [password] [role]', 'Create an user', + { role: { describe: 'Define this user role', choices: ['user', 'admin', 'editor' ] } }, create) + .command('remove ', 'Remove an user', {}, remove) + // .positional('email', { describe: 'user email or username', type: 'string', demandOption: true }) + // .positional('password', { describe: 'Password', type: 'string', demandOption: false }) .recommendCommands() .demandCommand(1, '') .argv