users: asyncify and merge userdb.del

This commit is contained in:
Girish Ramakrishnan
2021-06-26 09:57:07 -07:00
parent 147c8df6e3
commit e7d9af5aed
6 changed files with 80 additions and 139 deletions

View File

@@ -5,7 +5,7 @@ exports = module.exports = {
update,
list,
create,
remove,
del,
changePassword,
verifyPassword,
createInvite,
@@ -18,11 +18,12 @@ exports = module.exports = {
load
};
var assert = require('assert'),
const assert = require('assert'),
auditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance'),
users = require('../users.js');
function load(req, res, next) {
@@ -117,17 +118,16 @@ function get(req, res, next) {
next(new HttpSuccess(200, users.removePrivateFields(req.resource)));
}
function remove(req, res, next) {
async function del(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
if (req.user.id === req.resource.id) return next(new HttpError(409, 'Not allowed to remove yourself.'));
if (users.compareRoles(req.user.role, req.resource.role) < 0) return next(new HttpError(403, `role '${req.resource.role}' is required but user has only '${req.user.role}'`));
users.remove(req.resource, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(users.remove(req.resource, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
next(new HttpSuccess(204));
}
function verifyPassword(req, res, next) {