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

263 lines
10 KiB
JavaScript
Raw Normal View History

2016-04-17 16:22:39 +02:00
'use strict';
exports = module.exports = {
canEditProfile,
get,
setDisplayName,
setEmail,
setFallbackEmail,
getAvatar,
setAvatar,
setLanguage,
2022-05-14 19:41:32 +02:00
getBackgroundImage,
setBackgroundImage,
2021-07-15 09:50:11 -07:00
setPassword,
setTwoFactorAuthenticationSecret,
enableTwoFactorAuthentication,
disableTwoFactorAuthentication,
2024-12-11 18:24:20 +01:00
setNotificationConfig
2016-04-17 16:22:39 +02:00
};
2021-06-25 22:11:17 -07:00
const assert = require('assert'),
AuditSource = require('../auditsource.js'),
2019-10-24 14:40:26 -07:00
BoxError = require('../boxerror.js'),
2021-07-07 14:31:39 +02:00
constants = require('../constants.js'),
2024-10-18 21:50:23 +02:00
crypto = require('crypto'),
2024-01-29 11:47:19 +01:00
dashboard = require('../dashboard.js'),
2016-04-17 16:22:39 +02:00
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
2024-10-18 21:50:23 +02:00
https = require('https'),
2024-01-29 11:47:19 +01:00
path = require('path'),
paths = require('../paths.js'),
2021-04-29 12:49:48 -07:00
safe = require('safetydance'),
userDirectory = require('../user-directory.js'),
users = require('../users.js');
2016-04-17 16:22:39 +02:00
async function canEditProfile(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error, profileConfig] = await safe(userDirectory.getProfileConfig());
if (error) return next(BoxError.toHttpError(error));
2022-01-13 15:20:16 -08:00
if (profileConfig.lockUserProfiles) return next(new HttpError(403, 'admin has disallowed users from editing profiles'));
next();
}
2021-06-25 22:11:17 -07:00
async function get(req, res, next) {
2016-04-17 16:22:39 +02:00
assert.strictEqual(typeof req.user, 'object');
2024-01-29 11:47:19 +01:00
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user.id));
2021-06-25 22:11:17 -07:00
if (error) return next(BoxError.toHttpError(error));
2024-01-29 13:35:54 +01:00
const [avatarError, avatar] = await safe(users.getAvatar(req.user.id));
if (avatarError) return next(BoxError.toHttpError(error));
let avatarType;
if (avatar.equals(constants.AVATAR_GRAVATAR)) avatarType = constants.AVATAR_GRAVATAR;
else if (avatar.equals(constants.AVATAR_NONE)) avatarType = constants.AVATAR_NONE;
else avatarType = constants.AVATAR_CUSTOM;
2024-01-29 11:47:19 +01:00
const { fqdn:dashboardFqdn } = await dashboard.getLocation();
2021-06-25 22:11:17 -07:00
next(new HttpSuccess(200, {
id: req.user.id,
username: req.user.username,
email: req.user.email,
fallbackEmail: req.user.fallbackEmail,
displayName: req.user.displayName,
twoFactorAuthenticationEnabled: req.user.twoFactorAuthenticationEnabled,
role: req.user.role,
source: req.user.source,
hasBackgroundImage: !!backgroundImage,
language: req.user.language,
2024-12-11 18:24:20 +01:00
notificationConfig: req.user.notificationConfig,
2024-01-29 13:35:54 +01:00
avatarUrl: `https://${dashboardFqdn}/api/v1/profile/avatar/${req.user.id}`,
avatarType: avatarType.toString() // this is a Buffer
2021-06-25 22:11:17 -07:00
}));
2016-04-17 16:22:39 +02:00
}
async function setEmail(req, res, next) {
2016-04-17 16:22:39 +02:00
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be string'));
const [error] = await safe(users.update(req.user, { email: req.body.email }, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
}
async function setFallbackEmail(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
if ('fallbackEmail' in req.body && typeof req.body.fallbackEmail !== 'string') return next(new HttpError(400, 'fallbackEmail must be string'));
const [error] = await safe(users.update(req.user, { fallbackEmail: req.body.fallbackEmail }, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
}
async function setDisplayName(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
if ('displayName' in req.body && typeof req.body.displayName !== 'string') return next(new HttpError(400, 'displayName must be string'));
const [error] = await safe(users.update(req.user, { displayName: req.body.displayName }, AuditSource.fromRequest(req)));
2021-07-15 09:50:11 -07:00
if (error) return next(BoxError.toHttpError(error));
2016-04-17 16:22:39 +02:00
2021-07-15 09:50:11 -07:00
next(new HttpSuccess(204));
2016-04-17 16:22:39 +02:00
}
async function setLanguage(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
if ('language' in req.body && typeof req.body.language !== 'string') return next(new HttpError(400, 'language must be string'));
const [error] = await safe(users.update(req.user, { language: req.body.language }, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
}
2021-06-25 22:11:17 -07:00
async function setAvatar(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2021-07-08 10:40:10 +02:00
let avatar = typeof req.body.avatar === 'string' ? Buffer.from(req.body.avatar, 'utf8') : null;
2021-07-07 14:31:39 +02:00
if (req.files && req.files.avatar) {
avatar = safe.fs.readFileSync(req.files.avatar.path);
safe.fs.unlinkSync(req.files.avatar.path);
2021-07-07 14:31:39 +02:00
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
2021-07-08 10:40:10 +02:00
} else if (!avatar || (!avatar.equals(constants.AVATAR_GRAVATAR) && !avatar.equals(constants.AVATAR_NONE))) {
2021-07-07 14:31:39 +02:00
return next(new HttpError(400, `avatar must be a file, ${constants.AVATAR_GRAVATAR} or ${constants.AVATAR_NONE}`));
}
2021-04-29 12:49:48 -07:00
2021-06-25 22:11:17 -07:00
const [error] = await safe(users.setAvatar(req.user.id, avatar));
if (error) return next(BoxError.toHttpError(error));
2021-06-25 22:11:17 -07:00
next(new HttpSuccess(202, {}));
}
2021-06-25 22:11:17 -07:00
async function getAvatar(req, res, next) {
assert.strictEqual(typeof req.params.identifier, 'string');
const [,userId, ext] = req.params.identifier.match(/^(.*?)(?:\.(\w+))?$/);
const [userError, user] = await safe(users.get(userId));
2024-01-29 11:47:19 +01:00
if (userError) return next(BoxError.toHttpError(userError));
const [avatarError, avatar] = await safe(users.getAvatar(userId));
2024-01-29 11:47:19 +01:00
if (avatarError) return next(BoxError.toHttpError(avatarError));
if (avatar.equals(constants.AVATAR_GRAVATAR)) {
2024-10-18 21:50:23 +02:00
const gravatarHash = crypto.createHash('md5').update(user.email).digest('hex');
https.get(`https://www.gravatar.com/avatar/${gravatarHash}.jpg`, function (upstreamRes) {
if (upstreamRes.status !== 200) {
console.error('Gravatar error:', upstreamRes.status);
2024-01-29 11:47:19 +01:00
return res.status(404).end();
}
res.set('content-type', 'image/jpeg');
upstreamRes.pipe(res);
}).on('error', (e) => {
console.error('Gravatar error:', e.message);
return res.status(404).end();
});
} else if (avatar.equals(constants.AVATAR_NONE)) {
if (ext) {
res.sendFile(path.join(paths.DASHBOARD_DIR, '/img/avatar-default-symbolic.png'));
} else {
res.sendFile(path.join(paths.DASHBOARD_DIR, '/img/avatar-default-symbolic.svg'));
}
2024-01-27 22:55:10 +01:00
} else {
res.set('Content-Type', 'image/png');
res.send(avatar);
}
}
2022-05-14 19:41:32 +02:00
async function setBackgroundImage(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2022-05-15 12:14:17 +02:00
let backgroundImage = null;
2022-05-14 19:41:32 +02:00
2022-05-15 12:14:17 +02:00
if (req.files && req.files.backgroundImage) {
backgroundImage = safe.fs.readFileSync(req.files.backgroundImage.path);
safe.fs.unlinkSync(req.files.backgroundImage.path);
2022-05-15 12:14:17 +02:00
if (!backgroundImage) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
}
2022-05-14 19:41:32 +02:00
const [error] = await safe(users.setBackgroundImage(req.user.id, backgroundImage));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
}
async function getBackgroundImage(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user.id));
if (error) return next(BoxError.toHttpError(error));
res.send(backgroundImage);
}
2021-07-15 09:50:11 -07:00
async function setPassword(req, res, next) {
2016-04-17 16:22:39 +02:00
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.user, 'object');
2016-06-02 00:31:41 -07:00
if (typeof req.body.newPassword !== 'string') return next(new HttpError(400, 'newPassword must be a string'));
2016-04-17 16:22:39 +02:00
const [error] = await safe(users.setPassword(req.user, req.body.newPassword, AuditSource.fromRequest(req)));
2021-07-15 09:50:11 -07:00
if (error) return next(BoxError.toHttpError(error));
2016-04-17 16:22:39 +02:00
2021-07-15 09:50:11 -07:00
next(new HttpSuccess(204));
2016-04-17 16:22:39 +02:00
}
2018-04-25 19:08:15 +02:00
2021-07-15 09:50:11 -07:00
async function setTwoFactorAuthenticationSecret(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2018-04-25 19:08:15 +02:00
const [error, result] = await safe(users.setTwoFactorAuthenticationSecret(req.user, AuditSource.fromRequest(req)));
2021-07-15 09:50:11 -07:00
if (error) return next(BoxError.toHttpError(error));
2018-04-25 19:08:15 +02:00
2023-09-13 21:07:07 +05:30
next(new HttpSuccess(200, { secret: result.secret, qrcode: result.qrcode }));
2018-04-25 19:08:15 +02:00
}
2021-07-15 09:50:11 -07:00
async function enableTwoFactorAuthentication(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.user, 'object');
2018-04-25 19:08:15 +02:00
if (!req.body.totpToken || typeof req.body.totpToken !== 'string') return next(new HttpError(400, 'totpToken must be a nonempty string'));
const [error] = await safe(users.enableTwoFactorAuthentication(req.user, req.body.totpToken, AuditSource.fromRequest(req)));
2021-07-15 09:50:11 -07:00
if (error) return next(BoxError.toHttpError(error));
2018-04-26 16:29:40 +02:00
2023-09-13 21:07:07 +05:30
next(new HttpSuccess(204, {}));
2018-04-25 19:08:15 +02:00
}
2021-07-15 09:50:11 -07:00
async function disableTwoFactorAuthentication(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2018-04-25 19:08:15 +02:00
const [error] = await safe(users.disableTwoFactorAuthentication(req.user, AuditSource.fromRequest(req)));
2021-07-15 09:50:11 -07:00
if (error) return next(BoxError.toHttpError(error));
2018-04-26 16:29:40 +02:00
2023-09-13 21:07:07 +05:30
next(new HttpSuccess(204, {}));
2018-04-25 19:08:15 +02:00
}
2024-12-11 18:24:20 +01:00
async function setNotificationConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.user, 'object');
if (!Array.isArray(req.body.notificationConfig)) return next(new HttpError(400, 'notificationConfig must be an array of strings'));
if (req.body.notificationConfig.some(k => typeof k !== 'string')) return next(new HttpError(400, 'notificationConfig must be an array of strings'));
const [error] = await safe(users.setNotificationConfig(req.user, req.body.notificationConfig, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
}