gancio-upstream/server/cli/accounts.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-06-06 16:56:54 +02:00
let db
2022-03-11 11:34:14 +01:00
function _initializeDB () {
const config = require('../config')
config.log_level = 'error'
2022-06-06 16:56:54 +02:00
db = require('../api/models/index')
2022-03-11 11:34:14 +01:00
return db.initialize()
}
async function modify (args) {
await _initializeDB()
const helpers = require('../helpers')
const User = require('../api/models/user')
const user = await User.findOne({ where: { email: args.account } })
console.log()
if (!user) {
console.error(`User ${args.account} not found`)
return
}
if (args['reset-password']) {
const password = helpers.randomString()
user.password = password
await user.save()
console.log(`New password for user ${user.email} is '${password}'`)
}
2022-07-27 12:06:06 +02:00
if (args.admin) {
user.is_admin = true
await user.save()
}
await db.close()
2022-03-11 11:34:14 +01:00
}
2022-06-06 16:56:54 +02:00
async function create (args) {
await _initializeDB()
const User = require('../api/models/user')
const user = await User.create({
email: args.email,
is_active: true,
is_admin: args.admin || false
2022-06-21 22:46:45 +02:00
}).catch(e => console.error(String(e)))
console.error(JSON.stringify(user, null, 2))
2022-06-06 16:56:54 +02:00
await db.close()
}
async function remove (args) {
await _initializeDB()
const User = require('../api/models/user')
const user = await User.findOne({
where: { email: args.email }
})
if (user) {
await user.destroy()
}
await db.close()
2022-03-11 11:34:14 +01:00
}
async function list () {
await _initializeDB()
const User = require('../api/models/user')
const users = await User.findAll()
console.log()
2022-05-05 11:12:53 +02:00
users.forEach(u => console.log(`${u.id}\tadmin: ${u.is_admin}\tenabled: ${u.is_active}\temail: ${u.email}`))
2022-03-11 11:34:14 +01:00
console.log()
2022-06-06 16:56:54 +02:00
await db.close()
2022-03-11 11:34:14 +01:00
}
2022-05-05 16:01:39 +02:00
const accountsCLI = yargs => yargs
2022-03-11 11:34:14 +01:00
.command('list', 'List all accounts', list)
.command('modify', 'Modify', {
account: {
2022-05-05 11:12:53 +02:00
describe: 'Account to modify',
type: 'string',
demandOption: true
2022-03-11 11:34:14 +01:00
},
'reset-password': {
2022-05-05 16:01:39 +02:00
describe: 'Resets the password of the given account ',
2022-05-05 11:12:53 +02:00
type: 'boolean'
2022-07-27 12:06:06 +02:00
},
admin: { describe: 'Define this account as administrator', type: 'boolean' }
2022-03-11 11:34:14 +01:00
}, modify)
2022-07-27 12:06:06 +02:00
.command('create <email|username> [admin]', 'Create an account', {
2022-06-06 16:56:54 +02:00
admin: { describe: 'Define this account as administrator', type: 'boolean' }
}, create)
2022-07-27 12:06:06 +02:00
.command('remove <email|username>', 'Remove an account', {}, remove)
.positional('email', { describe: 'account email or username', type: 'string', demandOption: true })
2022-05-05 11:12:53 +02:00
.recommendCommands()
.demandCommand(1, '')
2022-05-05 16:01:39 +02:00
.argv
2022-03-11 11:34:14 +01:00
2022-05-05 16:01:39 +02:00
module.exports = accountsCLI