From a531260d71b21120c6402af6332beae41c2c1d35 Mon Sep 17 00:00:00 2001 From: ClementBouvierN Date: Mon, 30 Sep 2024 11:31:23 +0200 Subject: [PATCH] CLI can add and remove users from entities (#7236) Signed-off-by: ClementBouvierN --- cli/src/index.js | 143 +++++++++++++++--------------- cli/src/realtimescreenCommands.js | 2 +- cli/src/usersCommands.js | 81 +++++++++++++++-- 3 files changed, 150 insertions(+), 76 deletions(-) diff --git a/cli/src/index.js b/cli/src/index.js index d63975834a..e0520ec940 100755 --- a/cli/src/index.js +++ b/cli/src/index.js @@ -9,8 +9,7 @@ * This file is part of the OperatorFabric project. */ -const omelette = require('omelette'); // For completion -const fs = require('fs'); +const omelette = require('omelette'); const commands = require('./commands.js'); const config = require('./configCommands.js'); @@ -18,74 +17,79 @@ const args = process.argv.slice(2); const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']; -omelette('opfab').tree({ - bundle: ['load', 'delete'], - businessdata: ['load','delete'], - card: ['send', 'delete','resetratelimiter'], - commands : [], - config: ['set', 'get', 'list'], - connectedusers: { - sendmessage: ['RELOAD', 'BUSINESS_CONFIG_CHANGE', 'USER_CONFIG_CHANGE'] - }, - entities: ['load'], - groups: ['load'], - help: [ - 'bundle', - 'businessdata', - 'card', - 'config', - 'commands', - 'connectedusers', - 'entities', - 'groups', - 'login', - 'logout', - 'monitoringconfig', - 'perimeters', - 'processgroups', - 'processmonitoring', - 'realtimescreen', - 'service', - 'status', - 'users' - ], - login: [], - logout: [], - monitoringconfig: ['load', 'delete'], - perimeters: ['create', 'addtogroup', 'delete'], - processgroups: ['load','clear'], - processmonitoring: ['load'], - realtimescreen: ['load'], - service: { - 'get-log-level': [ - 'users', - 'businessconfig', - 'cards-consultation', - 'cards-publication', - 'external-devices', - 'supervisor', - 'cards-external-diffusion', - 'cards-reminder' +// Configuration of autocompletion in the CLI +omelette('opfab') + .tree({ + bundle: ['load', 'delete'], + businessdata: ['load', 'delete'], + card: ['send', 'delete', 'resetratelimiter'], + commands: [], + config: ['set', 'get', 'list'], + connectedusers: { + sendmessage: ['RELOAD', 'BUSINESS_CONFIG_CHANGE', 'USER_CONFIG_CHANGE'] + }, + entities: ['load'], + groups: ['load'], + help: [ + 'bundle', + 'businessdata', + 'card', + 'config', + 'commands', + 'connectedusers', + 'entities', + 'groups', + 'login', + 'logout', + 'monitoringconfig', + 'perimeters', + 'processgroups', + 'processmonitoring', + 'realtimescreen', + 'service', + 'status', + 'users' ], - 'set-log-level': { - users: levels, - businessconfig: levels, - 'cards-consultation': levels, - 'cards-publication': levels, - 'external-devices': levels, - supervisor: levels, - 'cards-external-diffusion': levels, - 'cards-reminder': levels - } - }, - status: [], - users: [ - 'set-notified', - 'set-not-notified', - 'set-notified-mail', - 'set-not-notified-mail' - ] -}).init(); + login: [], + logout: [], + monitoringconfig: ['load', 'delete'], + perimeters: ['create', 'addtogroup', 'delete'], + processgroups: ['load', 'clear'], + processmonitoring: ['load'], + realtimescreen: ['load'], + service: { + 'get-log-level': [ + 'users', + 'businessconfig', + 'cards-consultation', + 'cards-publication', + 'external-devices', + 'supervisor', + 'cards-external-diffusion', + 'cards-reminder' + ], + 'set-log-level': { + users: levels, + businessconfig: levels, + 'cards-consultation': levels, + 'cards-publication': levels, + 'external-devices': levels, + supervisor: levels, + 'cards-external-diffusion': levels, + 'cards-reminder': levels + } + }, + status: [], + users: [ + 'addtoentity', + 'removefromentity', + 'set-notified', + 'set-not-notified', + 'set-notified-mail', + 'set-not-notified-mail' + ] + }) + .init(); (async () => { if (args[0] === undefined) { @@ -95,4 +99,3 @@ omelette('opfab').tree({ commands.processCommand(args); } })(); - diff --git a/cli/src/realtimescreenCommands.js b/cli/src/realtimescreenCommands.js index 4d0876421e..2560fe1ec5 100644 --- a/cli/src/realtimescreenCommands.js +++ b/cli/src/realtimescreenCommands.js @@ -64,7 +64,7 @@ const realtimescreenCommands = { Command list : - load load realtimescreen from a file : opfab realtimescreen load + load load realtimescreen from a file : opfab realtimescreen load `); } diff --git a/cli/src/usersCommands.js b/cli/src/usersCommands.js index ecf445438d..9e09264517 100644 --- a/cli/src/usersCommands.js +++ b/cli/src/usersCommands.js @@ -9,6 +9,7 @@ const config = require('./configCommands.js'); const prompts = require('prompts'); +const utils = require('./utils.js'); const usersCommands = { async processUsersCommand(args) { @@ -20,6 +21,8 @@ const usersCommands = { name: 'value', message: 'Users action', choices: [ + {title: 'Add to entity', value: 'addtoentity'}, + {title: 'Remove from entity', value: 'removefromentity'}, {title: 'Set notified', value: 'set-notified'}, {title: 'Set not notified', value: 'set-not-notified'}, {title: 'Set notified by mail', value: 'set-notified-mail'}, @@ -34,6 +37,12 @@ const usersCommands = { } switch (action) { + case 'addtoentity': + await this.addUserTo('Entity', 'entities', args[1], args[2]); + break; + case 'removefromentity': + await this.removeUserFrom('Entity', 'entities', args[1], args[2]); + break; case 'set-notified': await this.configureNotification(args[1], args[2], 'POST', 'processstatenotified'); break; @@ -54,6 +63,59 @@ const usersCommands = { } }, + async missingPrompt(object, objectId, user) { + if (!objectId) { + objectId = ( + await prompts({ + type: 'text', + name: 'value', + message: `${object} ` + }) + ).value; + if (!objectId) { + console.log(`${object} is required`); + return; + } + } + if (!user) { + user = ( + await prompts({ + type: 'text', + name: 'value', + message: `user ` + }) + ).value; + if (!user) { + console.log(`user is required`); + return; + } + } + return {objectId, user}; + }, + + async addUserTo(object, objectUrl, objectId, user) { + const {objectId: resolvedObjectId, user: resolvedUser} = await this.missingPrompt(object, objectId, user); + await utils.sendRequest( + `users/${objectUrl}/${resolvedObjectId}/users`, + 'PATCH', + `["${resolvedUser}"]`, + `User ${resolvedUser} has been added to ${resolvedObjectId}`, + ``, + `${object} or user not found` + ); + }, + + async removeUserFrom(object, objectUrl, objectId, user) { + const {objectId: resolvedObjectId, user: resolvedUser} = await this.missingPrompt(object, objectId, user); + await utils.sendRequest( + `users/${objectUrl}/${resolvedObjectId}/users/${resolvedUser}`, + 'DELETE', + undefined, + `User ${resolvedUser} has been removed from ${resolvedObjectId}`, + `${object} or user not found` + ); + }, + async configureNotification(process, state, method, path) { if (!process) { process = ( @@ -82,13 +144,20 @@ const usersCommands = { } } - const url = `${config.getConfig('url')}:${config.getConfig('port')}/` + 'users/notificationconfiguration/' + path + '/' + process + '/' + state; + const url = + `${config.getConfig('url')}:${config.getConfig('port')}/` + + 'users/notificationconfiguration/' + + path + + '/' + + process + + '/' + + state; const token = config.getConfig('access_token'); const options = { method: method, headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${token}` + Authorization: `Bearer ${token}` } }; @@ -96,7 +165,7 @@ const usersCommands = { try { response = await fetch(url, options); } catch (error) { - console.error('Failed to configure process/state notification for ' + process + ' / ' +state); + console.error('Failed to configure process/state notification for ' + process + ' / ' + state); console.error('Error:', error); return; } @@ -104,16 +173,18 @@ const usersCommands = { if (response.ok) { console.error('Success'); } else { - console.error('Failed to configure process/state notification for ' + process + '/' +state); + console.error('Failed to configure process/state notification for ' + process + '/' + state); console.error('Response:', response); } }, async printHelp() { - console.log(`Usage: opfab users set-notif + console.log(`Usage: opfab users Commands list : + addtoentity Add a to an : opfab users + removefromentity Remove a from an : opfab users set-notified Configure / as to be notified for all users set-not-notified Configure / as not to be notified for all users set-notified-mail Configure / as to be notified by email for all users