From 1bfe4b399563acadb75ca3bbe483bef94c9a2eb4 Mon Sep 17 00:00:00 2001 From: lesion Date: Fri, 30 Jun 2023 23:44:35 +0200 Subject: [PATCH] CLI documentation and new cli users --- docs/usage/cli.md | 65 +++++++++++++++++++++++++ server/cli/users.js | 114 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 docs/usage/cli.md create mode 100644 server/cli/users.js diff --git a/docs/usage/cli.md b/docs/usage/cli.md new file mode 100644 index 00000000..e58041d3 --- /dev/null +++ b/docs/usage/cli.md @@ -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 ` 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 ` + +(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 ` + +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 ` + + +### Reset password +`gancio users reset-password ` + +> info "Changelog" +> from: 1.6.14 + +### Change administrator privileges + +To add administrator privileges to an user: +`gancio users set-admin ` + +To remove administrator privileges from an user: +`gancio users unset-admin ` + +> info "Changelog" +> from: 1.6.14 \ No newline at end of file diff --git a/server/cli/users.js b/server/cli/users.js new file mode 100644 index 00000000..e8c72f92 --- /dev/null +++ b/server/cli/users.js @@ -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 ', 'Resets the password of the given user', { + }, resetPassword) + .command('set-admin ', 'Set administrator privileges to the given user', {}, setAdmin) + .command('unset-admin ', 'Remove administrator privileges to the given user', {}, unsetAdmin) + .command('create [admin]', 'Create an user', { + admin: { describe: 'Define this user as administrator', type: 'boolean' } + }, create) + .command('remove ', 'Remove an user', {}, remove) + .positional('email', { describe: 'user email or username', type: 'string', demandOption: true }) + .recommendCommands() + .demandCommand(1, '') + .argv + +module.exports = usersCLI \ No newline at end of file