Add route to set the users groups

This commit is contained in:
Girish Ramakrishnan
2016-02-09 15:47:02 -08:00
parent 2b0791f4a3
commit f413bfb3a0
7 changed files with 186 additions and 16 deletions

View File

@@ -13,7 +13,8 @@ exports = module.exports = {
remove: removeUser,
verifyPassword: verifyPassword,
requireAdmin: requireAdmin,
sendInvite: sendInvite
sendInvite: sendInvite,
setGroups: setGroups
};
var assert = require('assert'),
@@ -226,3 +227,18 @@ function sendInvite(req, res, next) {
next(new HttpSuccess(200, {}));
});
}
function setGroups(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.params.userId, 'string');
if (!Array.isArray(req.body.groupIds)) return next(new HttpError(400, 'API call requires a groups array.'));
user.setGroups(req.params.userId, req.body.groupIds, function (error) {
if (error && error.reason === UserError.NOT_FOUND) return next(new HttpError(404, 'One or more groups not found'));
if (error && error.reason === UserError.NOT_ALLOWED) return next(new HttpError(403, 'Last admin'));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(204));
});
}