CLI documentation and new cli users

This commit is contained in:
lesion 2023-06-30 23:44:35 +02:00
parent aa5a29123c
commit 1bfe4b3995
No known key found for this signature in database
GPG key ID: 352918250B012177
2 changed files with 179 additions and 0 deletions

65
docs/usage/cli.md Normal file
View file

@ -0,0 +1,65 @@
---
layout: default
title: CLI
permalink: /usage/cli
nav_order: 2
parent: Usage
has_toc: true
---
# CLI - Command Line Interface
{: .no_toc }
1. TOC
{:toc}
## Using CLI
Gancio is distributed with an embedded CLI.
To use the CLI you need to specify the `config.json` configuration file via `--config <your_config.json>` flag; by default the CLI will look for one in the current directory, so if your current directory is /opt/gancio (having followed the [installation instructions](/install/debian)) there is no need to specify it.
#### Using CLI with Docker installation
To use the CLI in a docker installation you can execute a shell inside the container with:
`docker exec --workdir /home/node/data -it gancio sh` and following the normal CLI usage or running commands with:
`docker exec --workdir /home/node/data gancio gancio <your command>`
(the first "gancio" is the container name)
## Users
All users related sub-commands starts with `gancio users`.
Note that most of this actions could be done from administration panel (Admin > Users).
### List all users
To list all users use
`gancio users list`
### Create a new user
`gancio users create <username|email>`
To create an user with administrator privileges use the `--admin` flag, e.g. `gancio users create admin@example.com --admin`
### Remove a user
`gancio users remove <username|email>`
### Reset password
`gancio users reset-password <username|email>`
> info "Changelog"
> from: 1.6.14
### Change administrator privileges
To add administrator privileges to an user:
`gancio users set-admin <username|email>`
To remove administrator privileges from an user:
`gancio users unset-admin <username|email>`
> info "Changelog"
> from: 1.6.14

114
server/cli/users.js Normal file
View file

@ -0,0 +1,114 @@
let db
function _initializeDB () {
const config = require('../config')
config.log_level = 'error'
db = require('../api/models/index')
return db.initialize()
}
async function setAdmin (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.is_admin = true
await user.save()
console.log(`User ${user.email} is now an administrator!`)
await db.close()
}
async function unsetAdmin (args) {
await _initializeDB()
const helpers = require('../helpers')
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.is_admin = false
await user.save()
console.log(`User ${user.email} is not an administrator anymore!`)
await db.close()
}
async function resetPassword (args) {
await _initializeDB()
const helpers = require('../helpers')
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
}
const password = helpers.randomString()
user.password = password
await user.save()
console.log(`New password for user ${user.email} is '${password}'`)
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()
console.error(`User "${args.email}" succesfully removed`)
} else {
console.error(`User "${args.email}" not found!`)
}
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 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> [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 })
.recommendCommands()
.demandCommand(1, '')
.argv
module.exports = usersCLI