Files
cloudron-box/src/routes/users.js

179 lines
6.8 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2016-04-17 18:27:11 +02:00
get: get,
update: update,
2016-04-17 18:27:11 +02:00
list: list,
create: create,
remove: remove,
changePassword: changePassword,
verifyPassword: verifyPassword,
2018-08-17 09:49:58 -07:00
createInvite: createInvite,
2016-02-09 15:47:02 -08:00
sendInvite: sendInvite,
setGroups: setGroups,
load: load
};
var assert = require('assert'),
2019-03-25 15:07:06 -07:00
auditSource = require('../auditsource.js'),
2019-10-24 14:40:26 -07:00
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
2019-10-24 14:40:26 -07:00
users = require('../users.js');
function load(req, res, next) {
assert.strictEqual(typeof req.params.userId, 'string');
users.get(req.params.userId, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
req.resource = result;
next();
});
}
2016-04-17 18:27:11 +02:00
function create(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be string'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be string'));
2016-01-20 00:05:06 -08:00
if ('displayName' in req.body && typeof req.body.displayName !== 'string') return next(new HttpError(400, 'displayName must be string'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be string'));
2018-08-21 16:41:42 +02:00
if ('admin' in req.body && typeof req.body.admin !== 'boolean') return next(new HttpError(400, 'admin flag must be a boolean'));
var password = req.body.password || null;
var email = req.body.email;
var username = 'username' in req.body ? req.body.username : null;
var displayName = req.body.displayName || '';
2019-03-25 15:07:06 -07:00
users.create(username, password, email, displayName, { invitor: req.user, admin: req.body.admin }, auditSource.fromRequest(req), function (error, user) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, users.removePrivateFields(user)));
});
}
function update(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
2016-01-25 14:12:09 +01:00
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be string'));
2018-01-21 14:25:39 +01:00
if ('fallbackEmail' in req.body && typeof req.body.fallbackEmail !== 'string') return next(new HttpError(400, 'fallbackEmail must be string'));
2016-01-25 14:08:11 +01:00
if ('displayName' in req.body && typeof req.body.displayName !== 'string') return next(new HttpError(400, 'displayName must be string'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
if ('admin' in req.body) {
if (typeof req.body.admin !== 'boolean') return next(new HttpError(400, 'admin must be a boolean'));
// this route is only allowed for admins, so req.user has to be an admin
if (req.user.id === req.resource.id && !req.body.admin) return next(new HttpError(409, 'Cannot remove admin flag on self'));
}
2019-08-08 07:39:32 -07:00
if ('active' in req.body && typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
users.update(req.resource, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
}
2016-04-17 18:27:11 +02:00
function list(req, res, next) {
2019-01-14 16:39:20 +01:00
var page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a postive number'));
var perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
users.getAllPaged(req.query.search || null, page, perPage, function (error, results) {
if (error) return next(BoxError.toHttpError(error));
results = results.map(users.removeRestrictedFields);
2018-04-29 10:58:45 -07:00
next(new HttpSuccess(200, { users: results }));
});
}
2016-04-17 18:27:11 +02:00
function get(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
assert.strictEqual(typeof req.user, 'object');
next(new HttpSuccess(200, users.removePrivateFields(req.resource)));
}
2016-04-17 18:27:11 +02:00
function remove(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.'));
users.remove(req.resource, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
2016-06-02 22:25:48 -07:00
next(new HttpSuccess(204));
});
}
function verifyPassword(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.password !== 'string') return next(new HttpError(400, 'API call requires user password'));
2020-01-31 15:28:42 -08:00
users.verifyWithUsername(req.user.username, req.body.password, users.AP_WEBADMIN, function (error) {
if (error) return next(BoxError.toHttpError(error));
req.body.password = '<redacted>'; // this will prevent logs from displaying plain text password
next();
});
}
2018-08-17 09:49:58 -07:00
function createInvite(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
2018-08-17 09:49:58 -07:00
users.createInvite(req.resource, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2018-08-17 09:49:58 -07:00
next(new HttpSuccess(200, result));
2018-08-17 09:49:58 -07:00
});
}
2016-01-18 15:37:03 +01:00
function sendInvite(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
2016-01-18 15:37:03 +01:00
users.sendInvite(req.resource, { invitor: req.user }, function (error) {
if (error) return next(BoxError.toHttpError(error));
2016-01-18 15:37:03 +01:00
2018-08-17 09:49:58 -07:00
next(new HttpSuccess(200, { }));
2016-01-18 15:37:03 +01:00
});
}
2016-02-09 15:47:02 -08:00
function setGroups(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
2016-02-09 15:47:02 -08:00
if (!Array.isArray(req.body.groupIds)) return next(new HttpError(400, 'API call requires a groups array.'));
users.setMembership(req.resource, req.body.groupIds, function (error) {
if (error) return next(BoxError.toHttpError(error));
2016-02-09 15:47:02 -08:00
next(new HttpSuccess(204));
});
}
2018-06-28 16:48:04 -07:00
function changePassword(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
users.setPassword(req.resource, req.body.password, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
}