gancio-upstream/server/cli/accounts.js

86 lines
2.2 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-06-06 16:56:54 +02:00
async function create (args) {
await _initializeDB()
const User = require('../api/models/user')
console.error(args)
const user = await User.create({
email: args.email,
is_active: true,
is_admin: args.admin || false
})
console.error(user)
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-03-11 11:34:14 +01:00
}
}, modify)
2022-06-06 16:56:54 +02:00
.command('create <email|username>', 'Create an account', {
admin: { describe: 'Define this account as administrator', type: 'boolean' }
}, create)
.positional('email', { describe: '', type: 'string', demandOption: true })
.command('remove <email|username>', 'Remove an account', {}, remove)
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