2020-10-15 15:52:07 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const user = require('../user');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
const privileges = require('../privileges');
|
|
|
|
|
const events = require('../events');
|
|
|
|
|
|
|
|
|
|
const usersAPI = module.exports;
|
|
|
|
|
|
|
|
|
|
usersAPI.create = async function (caller, data) {
|
|
|
|
|
const uid = await user.create(data);
|
|
|
|
|
return await user.getUserData(uid);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
usersAPI.update = async function (caller, data) {
|
2020-10-15 16:22:40 -04:00
|
|
|
const oldUserData = await user.getUserFields(data.uid, ['email', 'username']);
|
2020-10-15 15:52:07 -04:00
|
|
|
if (!oldUserData || !oldUserData.username) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [isAdminOrGlobalMod, canEdit, passwordMatch] = await Promise.all([
|
|
|
|
|
user.isAdminOrGlobalMod(caller.uid),
|
2020-10-15 16:22:40 -04:00
|
|
|
privileges.users.canEdit(caller.uid, data.uid),
|
|
|
|
|
data.password ? user.isPasswordCorrect(data.uid, data.password, caller.ip) : false,
|
2020-10-15 15:52:07 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Changing own email/username requires password confirmation
|
2020-10-15 16:22:40 -04:00
|
|
|
if (['email', 'username'].some(prop => Object.keys(data).includes(prop)) && !isAdminOrGlobalMod && caller.uid === data.uid && !passwordMatch) {
|
2020-10-15 15:52:07 -04:00
|
|
|
throw new Error('[[error:invalid-password]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!canEdit) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isAdminOrGlobalMod && meta.config['username:disableEdit']) {
|
|
|
|
|
data.username = oldUserData.username;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isAdminOrGlobalMod && meta.config['email:disableEdit']) {
|
|
|
|
|
data.email = oldUserData.email;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await user.updateProfile(caller.uid, data);
|
|
|
|
|
const userData = await user.getUserData(data.uid);
|
|
|
|
|
|
|
|
|
|
async function log(type, eventData) {
|
|
|
|
|
eventData.type = type;
|
|
|
|
|
eventData.uid = caller.uid;
|
2020-10-15 16:22:40 -04:00
|
|
|
eventData.targetUid = data.uid;
|
2020-10-15 15:52:07 -04:00
|
|
|
eventData.ip = caller.ip;
|
|
|
|
|
await events.log(eventData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userData.email !== oldUserData.email) {
|
|
|
|
|
await log('email-change', { oldEmail: oldUserData.email, newEmail: userData.email });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userData.username !== oldUserData.username) {
|
|
|
|
|
await log('username-change', { oldUsername: oldUserData.username, newUsername: userData.username });
|
|
|
|
|
}
|
|
|
|
|
};
|