2020-07-23 11:57:44 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2021-03-07 23:53:54 +03:00
|
|
|
import * as Serializers from "~/node_common/serializers";
|
2020-07-23 11:57:44 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-09-22 03:36:45 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-10-27 07:41:42 +03:00
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
2020-11-17 20:43:39 +03:00
|
|
|
import * as SearchManager from "~/node_common/managers/search";
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
2021-07-14 01:52:21 +03:00
|
|
|
import * as RequestUtilities from "~/node_common/request-utilities";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
import BCrypt from "bcrypt";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2021-07-14 01:52:21 +03:00
|
|
|
const userInfo = await RequestUtilities.checkAuthorizationInternal(req, res);
|
|
|
|
if (!userInfo) return;
|
|
|
|
const { id, user } = userInfo;
|
2020-07-22 08:53:29 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let updates = req.body.data;
|
|
|
|
|
|
|
|
if (updates.username && updates.username !== user.username) {
|
|
|
|
if (!Validations.username(req.body.data.username)) {
|
|
|
|
return res.status(400).send({
|
|
|
|
decorator: "SERVER_USER_UPDATE_INVALID_USERNAME",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const existing = await Data.getUserByUsername({
|
2020-11-28 04:30:40 +03:00
|
|
|
username: req.body.data.username.toLowerCase(),
|
2020-07-22 13:51:40 +03:00
|
|
|
});
|
|
|
|
|
2020-11-13 01:36:20 +03:00
|
|
|
if (existing && existing.id !== id) {
|
2021-03-07 23:53:54 +03:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_USER_UPDATE_USERNAME_IS_TAKEN", error: true });
|
2020-09-09 20:56:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:53:30 +03:00
|
|
|
if (updates.email && updates.email !== user.email) {
|
|
|
|
if (!Validations.email(req.body.data.email)) {
|
|
|
|
return res.status(400).send({
|
|
|
|
decorator: "SERVER_USER_UPDATE_INVALID_EMAIL",
|
|
|
|
error: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const existing = await Data.getUserByEmail({
|
|
|
|
email: req.body.data.email.toLowerCase(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existing && existing.id !== id) {
|
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_UPDATE_EMAIL", error: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 04:30:40 +03:00
|
|
|
if (req.body.data.type === "SAVE_DEFAULT_ARCHIVE_CONFIG") {
|
2020-11-12 13:51:23 +03:00
|
|
|
let b;
|
|
|
|
try {
|
|
|
|
b = await Utilities.getBucketAPIFromUserToken({
|
|
|
|
user,
|
|
|
|
bucketName: "data",
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.error(e);
|
2020-11-12 13:51:23 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/pages/api/users/update.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `Utilities.getBucketAPIFromUserToken`,
|
|
|
|
});
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_NO_BUCKET_DATA", error: true });
|
2020-11-12 13:51:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-11-28 04:30:40 +03:00
|
|
|
const configResponse = await b.buckets.setDefaultArchiveConfig(
|
|
|
|
b.bucketKey,
|
|
|
|
req.body.data.config
|
|
|
|
);
|
2020-11-12 13:51:23 +03:00
|
|
|
} catch (e) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.error(e);
|
2020-11-12 13:51:23 +03:00
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/pages/api/users/update.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `b.buckets.setDefaultArchiveConfig`,
|
|
|
|
});
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_USER_UPDATE_DEFAULT_ARCHIVE_CONFIG", error: true });
|
2020-11-12 13:51:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (req.body.data.type === "CHANGE_PASSWORD" && req.body.data.password) {
|
2020-11-28 04:30:40 +03:00
|
|
|
if (!Validations.password(req.body.data.password)) {
|
2021-03-07 23:53:54 +03:00
|
|
|
return res
|
|
|
|
.status(500)
|
|
|
|
.send({ decorator: "SERVER_USER_UPDATE_INVALID_PASSWORD", error: true });
|
2020-07-23 11:57:44 +03:00
|
|
|
}
|
|
|
|
|
2020-08-11 08:15:39 +03:00
|
|
|
const rounds = Number(Environment.LOCAL_PASSWORD_ROUNDS);
|
|
|
|
const salt = await BCrypt.genSalt(rounds);
|
2020-11-28 04:30:40 +03:00
|
|
|
const hash = await Utilities.encryptPassword(req.body.data.password, salt);
|
2020-07-23 11:57:44 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
updates.salt = salt;
|
|
|
|
updates.password = hash;
|
2020-07-23 11:57:44 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let unsafeResponse = await Data.updateUserById({ id, ...updates });
|
2020-11-13 01:36:20 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (unsafeResponse && !unsafeResponse.error) {
|
2020-11-13 01:36:20 +03:00
|
|
|
if (
|
|
|
|
user.username !== unsafeResponse.username ||
|
|
|
|
user.data.name !== unsafeResponse.data.name ||
|
|
|
|
user.data.photo !== unsafeResponse.data.photo
|
|
|
|
) {
|
|
|
|
SearchManager.updateUser(unsafeResponse, "EDIT");
|
|
|
|
}
|
2020-10-27 07:41:42 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
ViewerManager.hydratePartial(id, { viewer: true });
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({ decorator: "SERVER_USER_UPDATE" });
|
2020-07-17 13:24:20 +03:00
|
|
|
};
|