feat: new users setrole CLI subcommand

This commit is contained in:
lesion 2024-03-28 13:24:46 +01:00
parent 22c2735e93
commit 40ca01c489
No known key found for this signature in database
GPG key ID: 352918250B012177

View file

@ -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 <email|username>', 'Resets the password of the given user', {
}, resetPassword)
.command('set-admin <email|username>', 'Set administrator privileges to the given user', {}, setAdmin)
.command('unset-admin <email|username>', 'Remove administrator privileges to the given user', {}, unsetAdmin)
.command('create <email|username> [password] [admin]', 'Create an user', {
admin: { describe: 'Define this user as administrator', type: 'boolean' },
}, create)
.command('remove <email|username>', '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 <email|username> <role>', 'Set specified role privileges to the given user',
{ role: { describe: 'Define this user role', choices: ['user', 'admin', 'editor' ] } }, setRole)
.command('create <email|username> [password] [role]', 'Create an user',
{ role: { describe: 'Define this user role', choices: ['user', 'admin', 'editor' ] } }, create)
.command('remove <email|username>', '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