feat(api): account deletion routes for the Write API (#8881)

* feat(api): account deletion routes for the Write API

* refactor: rewrite client-side calls to account deletion to use api

* style: apply DRY
This commit is contained in:
Julian Lam
2020-11-17 17:29:50 -05:00
committed by GitHub
parent 422aa7f0b6
commit a0b7a82350
12 changed files with 169 additions and 152 deletions

View File

@@ -15,7 +15,6 @@ const db = require('../database');
const userController = require('../controllers/user');
const privileges = require('../privileges');
const utils = require('../utils');
const flags = require('../flags');
const sockets = require('.');
const SocketUser = module.exports;
@@ -37,37 +36,8 @@ SocketUser.exists = async function (socket, data) {
};
SocketUser.deleteAccount = async function (socket, data) {
if (!socket.uid) {
throw new Error('[[error:no-privileges]]');
}
const hasPassword = await user.hasPassword(socket.uid);
if (hasPassword) {
const ok = await user.isPasswordCorrect(socket.uid, data.password, socket.ip);
if (!ok) {
throw new Error('[[error:invalid-password]]');
}
}
const isAdmin = await user.isAdministrator(socket.uid);
if (isAdmin) {
throw new Error('[[error:cant-delete-admin]]');
}
if (meta.config.allowAccountDelete !== 1) {
throw new Error('[[error:no-privileges]]');
}
await flags.resolveFlag('user', socket.uid, socket.uid);
const userData = await user.deleteAccount(socket.uid);
require('./index').server.sockets.emit('event:user_status_change', { uid: socket.uid, status: 'offline' });
await events.log({
type: 'user-delete',
uid: socket.uid,
targetUid: socket.uid,
ip: socket.ip,
username: userData.username,
email: userData.email,
});
sockets.warnDeprecated(socket, 'DELETE /api/v3/users/:uid/account');
await api.users.deleteAccount(socket, data);
};
SocketUser.emailExists = async function (socket, data) {