From d4889eb52c7088a94755dfce59ad3f0276ffa5ff Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 27 Nov 2017 16:44:40 -0700 Subject: [PATCH] more role fixes --- bin/cli-users | 62 +++++++++++++++------------------------ routes/api/users/index.js | 11 +------ services/setup.js | 6 ++-- 3 files changed, 27 insertions(+), 52 deletions(-) diff --git a/bin/cli-users b/bin/cli-users index 35ae78ba5..cad663260 100755 --- a/bin/cli-users +++ b/bin/cli-users @@ -282,42 +282,30 @@ function mergeUsers(dstUserID, srcUserID) { * @param {String} userUD id of the user to add the role to * @param {String} role the role to add */ -function addRole(userID, role) { - UsersService - .addRoleToUser(userID, role) - .then(() => { - console.log(`Added the ${role} role to User ${userID}.`); - util.shutdown(); - }) - .catch((err) => { - console.error(err); - util.shutdown(1); - }); -} +async function setRole(userID, role, options) { + try { + if (options.interactive || !role) { + const answers = await inquirer.prompt([ + { + name: 'role', + message: 'User Role', + type: 'list', + choices: USER_ROLES + } + ]); -/** - * Removes a role from a user - * @param {String} userUD id of the user to remove the role from - * @param {String} role the role to remove - */ -function removeRole(userID, role) { + role = answers.role; + } - if (USER_ROLES.indexOf(role) === -1) { - console.error(`Role '${role}' is not supported. Supported roles are ${USER_ROLES.join(', ')}.`); + await UsersService.setRole(userID, role); + + console.log(`Set User ${userID} to the ${role} role.`); + util.shutdown(); + } catch (err) { + console.error(err); util.shutdown(1); - return; } - UsersService - .removeRoleFromUser(userID, role) - .then(() => { - console.log(`Removed the ${role} role from User ${userID}.`); - util.shutdown(); - }) - .catch((err) => { - console.error(err); - util.shutdown(1); - }); } /** @@ -379,14 +367,10 @@ program .action(mergeUsers); program - .command('addrole ') - .description('adds a role to a given user') - .action(addRole); - -program - .command('removerole ') - .description('removes a role from a given user') - .action(removeRole); + .command('setrole [role]') + .option('-i, --interactive', 'Enable interactive mode') + .description('sets the role on a user') + .action(setRole); program .command('verify ') diff --git a/routes/api/users/index.js b/routes/api/users/index.js index 7dbd95a71..4832847c6 100644 --- a/routes/api/users/index.js +++ b/routes/api/users/index.js @@ -48,16 +48,7 @@ router.get('/', authorization.needed('ADMIN', 'MODERATOR'), async (req, res, nex router.post('/:user_id/role', authorization.needed('ADMIN', 'MODERATOR'), async (req, res, next) => { try { - await UsersService.addRoleToUser(req.params.user_id, req.body.role); - res.status(204).end(); - } catch (e) { - next(e); - } -}); - -router.delete('/:user_id/role', authorization.needed('ADMIN', 'MODERATOR'), async (req, res, next) => { - try { - await UsersService.removeRoleFromUser(req.params.user_id, req.body.role); + await UsersService.setRole(req.params.user_id, req.body.role); res.status(204).end(); } catch (e) { next(e); diff --git a/services/setup.js b/services/setup.js index 053a85789..ab50fd11b 100644 --- a/services/setup.js +++ b/services/setup.js @@ -27,7 +27,7 @@ module.exports = class SetupService { // Get the current settings, we are expecing an error here. await SettingsService.retrieve(); - + // We should NOT have gotten a settings object, this means that the // application is already setup. Error out here. throw errors.ErrSettingsInit; @@ -88,10 +88,10 @@ module.exports = class SetupService { // Grant them administrative privileges and confirm the email account. await Promise.all([ - UsersService.addRoleToUser(user.id, 'ADMIN'), + UsersService.setRole(user.id, 'ADMIN'), UsersService.confirmEmail(user.id, email) ]); - + return { settings, user