This commit is contained in:
lesion 2023-06-30 23:50:01 +02:00
parent 5e802c4577
commit 5deca0ad4f
No known key found for this signature in database
GPG key ID: 352918250B012177
2 changed files with 2 additions and 94 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
const pkg = require('../package.json') const pkg = require('../package.json')
const path = require('path') const path = require('path')
const accountsCLI = require('./cli/accounts') const usersCLI = require('./cli/users')
process.env.cwd = process.env.GANCIO_DATA || path.resolve('./') process.env.cwd = process.env.GANCIO_DATA || path.resolve('./')
@ -29,7 +29,7 @@ require('yargs')
return absolute_config_path return absolute_config_path
}}) }})
.command(['start', 'run', '$0'], 'Start gancio', {}, start) .command(['start', 'run', '$0'], 'Start gancio', {}, start)
.command(['accounts'], 'Manage accounts', accountsCLI) .command(['users'], 'Manage users', usersCLI)
.help('h') .help('h')
.alias('h', 'help') .alias('h', 'help')
.epilog('Made with ❤ by underscore hacklab - https://gancio.org') .epilog('Made with ❤ by underscore hacklab - https://gancio.org')

View file

@ -1,92 +0,0 @@
let db
function _initializeDB () {
const config = require('../config')
config.log_level = 'error'
db = require('../api/models/index')
return db.initialize()
}
async function modify (args) {
await _initializeDB()
const helpers = require('../helpers')
const { User } = require('../api/models/models')
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}'`)
}
if (args.admin) {
user.is_admin = true
await user.save()
}
await db.close()
}
async function create (args) {
await _initializeDB()
const { User } = require('../api/models/models')
const user = await User.create({
email: args.email,
is_active: true,
is_admin: args.admin || false
}).catch(e => console.error(String(e)))
console.error(JSON.stringify(user, null, 2))
await db.close()
}
async function remove (args) {
await _initializeDB()
const { User } = require('../api/models/models')
const user = await User.findOne({
where: { email: args.email }
})
if (user) {
await user.destroy()
}
await db.close()
}
async function list () {
await _initializeDB()
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}`))
console.log()
await db.close()
}
const accountsCLI = yargs => yargs
.command('list', 'List all accounts', list)
.command('modify', 'Modify', {
account: {
describe: 'Account to modify',
type: 'string',
demandOption: true
},
'reset-password': {
describe: 'Resets the password of the given account ',
type: 'boolean'
},
admin: { describe: 'Define this account as administrator', type: 'boolean' }
}, modify)
.command('create <email|username> [admin]', 'Create an account', {
admin: { describe: 'Define this account as administrator', type: 'boolean' }
}, create)
.command('remove <email|username>', 'Remove an account', {}, remove)
.positional('email', { describe: 'account email or username', type: 'string', demandOption: true })
.recommendCommands()
.demandCommand(1, '')
.argv
module.exports = accountsCLI