more role fixes

This commit is contained in:
Wyatt Johnson
2017-11-27 16:44:40 -07:00
parent f2fe095b43
commit d4889eb52c
3 changed files with 27 additions and 52 deletions
+23 -39
View File
@@ -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 <userID> <role>')
.description('adds a role to a given user')
.action(addRole);
program
.command('removerole <userID> <role>')
.description('removes a role from a given user')
.action(removeRole);
.command('setrole <userID> [role]')
.option('-i, --interactive', 'Enable interactive mode')
.description('sets the role on a user')
.action(setRole);
program
.command('verify <userID> <email>')
+1 -10
View File
@@ -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);
+3 -3
View File
@@ -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