From 45e27fd6adf6cbb1018bfab70807a85e39f533fa Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 20 Nov 2017 13:29:40 -0700 Subject: [PATCH] fixes to cli --- bin/cli-users | 7 ------- models/user.js | 3 ++- services/users.js | 27 +++------------------------ 3 files changed, 5 insertions(+), 32 deletions(-) diff --git a/bin/cli-users b/bin/cli-users index b82ee561d..248bea523 100755 --- a/bin/cli-users +++ b/bin/cli-users @@ -289,13 +289,6 @@ function mergeUsers(dstUserID, srcUserID) { * @param {String} role the role to add */ function addRole(userID, role) { - - if (USER_ROLES.indexOf(role) === -1) { - console.error(`Role '${role}' is not supported. Supported roles are ${USER_ROLES.join(', ')}.`); - util.shutdown(1); - return; - } - UsersService .addRoleToUser(userID, role) .then(() => { diff --git a/models/user.js b/models/user.js index 5d8a41cc7..a793ca9df 100644 --- a/models/user.js +++ b/models/user.js @@ -91,7 +91,8 @@ const UserSchema = new Schema({ // user. roles: [{ type: String, - enum: USER_ROLES + enum: USER_ROLES, + required: true }], // Status stores the user status information regarding permissions, diff --git a/services/users.js b/services/users.js index eab441a30..719666788 100644 --- a/services/users.js +++ b/services/users.js @@ -552,43 +552,22 @@ class UsersService { * Adds a role to a user. * @param {String} id id of a user * @param {String} role role to add - * @param {Function} done callback after the operation is complete */ - static async addRoleToUser(id, role) { - const roles = []; - - // Check to see if the user role is in the allowable set of roles. - if (role && USER_ROLES.indexOf(role) === -1) { - - // User role is not supported! Error out here. - throw new Error(`role ${role} is not supported`); - } else if(role) { - roles.push(role); - } - - return UserModel.update({id}, {$set: {roles}}); + static addRoleToUser(id, role) { + return UserModel.update({id}, {$addToSet: {roles: role}}, {runValidators: true}); } /** * Removes a role from a user. * @param {String} id id of a user * @param {String} role role to remove - * @param {Function} done callback after the operation is complete */ static async removeRoleFromUser(id, role) { - - // Check to see if the user role is in the allowable set of roles. - if (USER_ROLES.indexOf(role) === -1) { - - // User role is not supported! Error out here. - throw new Error(`role ${role} is not supported`); - } - return UserModel.update({id}, { $pull: { roles: role } - }); + }, {runValidators: true}); } /**