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

257 lines
9.4 KiB
JavaScript
Raw Normal View History

2016-04-17 16:22:39 +02:00
'use strict';
exports = module.exports = {
2024-01-22 13:53:40 +01:00
canEditProfile,
2020-07-09 14:35:34 -07:00
get,
2024-01-18 17:34:45 +01:00
setDisplayName,
setEmail,
setFallbackEmail,
2025-06-08 12:42:13 +02:00
getAvatarById,
2020-07-09 14:35:34 -07:00
setAvatar,
2025-07-15 10:06:26 +02:00
unsetAvatar,
setLanguage,
2022-05-14 19:41:32 +02:00
getBackgroundImage,
setBackgroundImage,
2025-06-06 18:36:58 +02:00
unsetBackgroundImage,
2021-07-15 09:50:11 -07:00
setPassword,
2020-07-09 14:35:34 -07:00
setTwoFactorAuthenticationSecret,
enableTwoFactorAuthentication,
disableTwoFactorAuthentication,
2025-06-11 22:00:09 +02:00
setNotificationConfig,
destroyUserSession
2016-04-17 16:22:39 +02:00
};
2025-08-14 11:17:38 +05:30
const assert = require('node:assert'),
2021-09-30 09:50:30 -07:00
AuditSource = require('../auditsource.js'),
2019-10-24 14:40:26 -07:00
BoxError = require('../boxerror.js'),
2025-07-10 11:00:31 +02:00
HttpError = require('@cloudron/connect-lastmile').HttpError,
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
2025-06-11 22:00:09 +02:00
oidcServer = require('../oidcserver.js'),
2021-04-29 12:49:48 -07:00
safe = require('safetydance'),
2025-06-11 22:00:09 +02:00
tokens = require('../tokens.js'),
userDirectory = require('../user-directory.js'),
users = require('../users.js'),
settings = require('../settings.js');
2016-04-17 16:22:39 +02:00
2024-01-22 13:53:40 +01:00
async function canEditProfile(req, res, next) {
2020-07-09 14:35:34 -07:00
assert.strictEqual(typeof req.user, 'object');
const [error, profileConfig] = await safe(userDirectory.getProfileConfig());
2021-08-18 15:40:28 -07:00
if (error) return next(BoxError.toHttpError(error));
2020-07-09 14:35:34 -07:00
2022-01-13 15:20:16 -08:00
if (profileConfig.lockUserProfiles) return next(new HttpError(403, 'admin has disallowed users from editing profiles'));
2020-07-09 14:35:34 -07:00
2021-08-18 15:40:28 -07:00
next();
2020-07-09 14:35:34 -07:00
}
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');
2025-06-08 12:42:13 +02:00
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user));
2021-06-25 22:11:17 -07:00
if (error) return next(BoxError.toHttpError(error));
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 || await settings.get(settings.LANGUAGE_KEY),
2024-12-11 18:24:20 +01:00
notificationConfig: req.user.notificationConfig,
2021-06-25 22:11:17 -07:00
}));
2016-04-17 16:22:39 +02:00
}
2024-01-18 17:34:45 +01: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'));
2024-01-18 17:34:45 +01:00
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'));
2024-01-18 17:34:45 +01:00
const [error] = await safe(users.update(req.user, { fallbackEmail: req.body.fallbackEmail }, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
2016-06-02 23:53:06 -07:00
2024-01-18 17:34:45 +01:00
next(new HttpSuccess(204));
}
2021-09-09 23:01:28 +02:00
2024-01-18 17:34:45 +01:00
async function setDisplayName(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
2021-09-14 10:36:14 -07:00
2024-01-18 17:34:45 +01:00
if ('displayName' in req.body && typeof req.body.displayName !== 'string') return next(new HttpError(400, 'displayName must be string'));
2021-09-09 23:01:28 +02:00
2024-01-18 17:34:45 +01:00
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 (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) {
2019-11-25 16:12:22 +01:00
assert.strictEqual(typeof req.user, 'object');
2025-06-08 12:42:13 +02:00
assert.strictEqual(typeof req.files, 'object');
2019-11-25 16:12:22 +01:00
2025-06-08 12:42:13 +02:00
const avatar = safe.fs.readFileSync(req.files.avatar.path);
safe.fs.unlinkSync(req.files.avatar.path);
if (!avatar) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
2021-04-29 12:49:48 -07:00
2025-06-08 12:42:13 +02:00
const [error] = await safe(users.setAvatar(req.user, avatar));
2021-06-25 22:11:17 -07:00
if (error) return next(BoxError.toHttpError(error));
2019-11-25 16:12:22 +01:00
2025-06-30 20:48:51 +02:00
next(new HttpSuccess(204, {}));
2019-11-25 16:12:22 +01:00
}
2025-07-15 10:06:26 +02:00
async function unsetAvatar(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error] = await safe(users.setAvatar(req.user, null));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
}
2025-06-08 12:42:13 +02:00
async function getAvatarById(req, res, next) {
assert.strictEqual(typeof req.params, 'object');
2020-07-09 22:33:36 -07:00
2025-06-08 12:42:13 +02:00
const [userError, user] = await safe(users.get(req.params.identifier));
2024-01-29 11:47:19 +01:00
if (userError) return next(BoxError.toHttpError(userError));
2025-06-12 01:23:10 +02:00
if (!user) return next(new HttpError(404, 'no avatar'));
2024-01-29 11:47:19 +01:00
2025-06-08 12:42:13 +02:00
const [avatarError, avatar] = await safe(users.getAvatar(user));
2024-01-29 11:47:19 +01:00
if (avatarError) return next(BoxError.toHttpError(avatarError));
2025-06-08 12:42:13 +02:00
if (!avatar) return next(new HttpError(404, 'no avatar'));
2024-01-29 11:47:19 +01:00
2025-06-08 12:42:13 +02:00
res.set('Content-Type', 'image/png');
res.status(200).send(avatar);
2019-11-25 16:12:22 +01:00
}
2022-05-14 19:41:32 +02:00
async function setBackgroundImage(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2025-06-06 18:36:58 +02:00
assert.strictEqual(typeof req.files, 'object');
2022-05-14 19:41:32 +02:00
2025-06-06 18:36:58 +02:00
if (!req.files.backgroundImage) return next(new HttpError(400, 'backgroundImage must be provided'));
const backgroundImage = safe.fs.readFileSync(req.files.backgroundImage.path);
safe.fs.unlinkSync(req.files.backgroundImage.path);
if (!backgroundImage) return next(BoxError.toHttpError(new BoxError(BoxError.FS_ERROR, safe.error.message)));
2022-05-14 19:41:32 +02:00
2025-06-08 12:42:13 +02:00
const [error] = await safe(users.setBackgroundImage(req.user, backgroundImage));
2022-05-14 19:41:32 +02:00
if (error) return next(BoxError.toHttpError(error));
2025-06-06 18:36:58 +02:00
next(new HttpSuccess(200, {}));
}
async function unsetBackgroundImage(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2025-06-08 12:42:13 +02:00
const [error] = await safe(users.setBackgroundImage(req.user, null));
2025-06-06 18:36:58 +02:00
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
2022-05-14 19:41:32 +02:00
}
async function getBackgroundImage(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
2025-06-08 12:42:13 +02:00
const [error, backgroundImage] = await safe(users.getBackgroundImage(req.user));
2022-05-14 19:41:32 +02:00
if (error) return next(BoxError.toHttpError(error));
2025-06-06 18:36:58 +02:00
if (!backgroundImage) return next(new HttpError(404, 'no background set'));
res.set('Cache-Control', 'no-cache');
res.set('Content-Type', 'image/webp');
res.status(200).send(backgroundImage);
2022-05-14 19:41:32 +02:00
}
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
2021-09-30 09:50:30 -07: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) {
2018-04-26 15:12:14 +02:00
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) {
2018-04-26 15:12:14 +02:00
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) {
2018-04-26 15:12:14 +02:00
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, {}));
}
2025-06-11 22:00:09 +02:00
async function destroyUserSession(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error] = await safe(oidcServer.revokeByUsername(req.user.username));
2025-06-11 22:00:09 +02:00
if (error) return next(BoxError.toHttpError(error));
await safe(tokens.del(req.token.id));
next(new HttpSuccess(204));
}