forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,20 @@ | ||
'use strict'; | ||
|
||
const db = require('../../database'); | ||
const user = require('../../user'); | ||
const api = require('../../api'); | ||
const websockets = require('../index'); | ||
const events = require('../../events'); | ||
const privileges = require('../../privileges'); | ||
const plugins = require('../../plugins'); | ||
const translator = require('../../translator'); | ||
const flags = require('../../flags'); | ||
|
||
module.exports = function (SocketUser) { | ||
SocketUser.banUsers = async function (socket, data) { | ||
websockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid/ban'); | ||
|
||
if (!data || !Array.isArray(data.uids)) { | ||
throw new Error('[[error:invalid-data]]'); | ||
} | ||
|
||
await toggleBan(socket.uid, data.uids, async function (uid) { | ||
await banUser(socket.uid, uid, data.until || 0, data.reason || ''); | ||
await flags.resolveFlag('user', uid, socket.uid); | ||
await flags.resolveUserPostFlags(uid, socket.uid); | ||
await events.log({ | ||
type: 'user-ban', | ||
uid: socket.uid, | ||
targetUid: uid, | ||
ip: socket.ip, | ||
reason: data.reason || undefined, | ||
}); | ||
plugins.fireHook('action:user.banned', { | ||
callerUid: socket.uid, | ||
ip: socket.ip, | ||
uid: uid, | ||
until: data.until > 0 ? data.until : undefined, | ||
reason: data.reason || undefined, | ||
}); | ||
await user.auth.revokeAllSessions(uid); | ||
}); | ||
await Promise.all(data.uids.map(async (uid) => { | ||
await api.users.ban(socket, { uid }); | ||
})); | ||
}; | ||
|
||
SocketUser.unbanUsers = async function (socket, uids) { | ||
websockets.warnDeprecated(socket, 'DELETE /api/v3/users/:uid/ban'); | ||
|
||
await toggleBan(socket.uid, uids, async function (uid) { | ||
await user.bans.unban(uid); | ||
await events.log({ | ||
type: 'user-unban', | ||
uid: socket.uid, | ||
targetUid: uid, | ||
ip: socket.ip, | ||
}); | ||
plugins.fireHook('action:user.unbanned', { | ||
callerUid: socket.uid, | ||
ip: socket.ip, | ||
uid: uid, | ||
}); | ||
}); | ||
await Promise.all(uids.map(async (uid) => { | ||
await api.users.unban(socket, { uid }); | ||
})); | ||
}; | ||
|
||
async function toggleBan(uid, uids, method) { | ||
if (!Array.isArray(uids)) { | ||
throw new Error('[[error:invalid-data]]'); | ||
} | ||
const hasBanPrivilege = await privileges.users.hasBanPrivilege(uid); | ||
if (!hasBanPrivilege) { | ||
throw new Error('[[error:no-privileges]]'); | ||
} | ||
|
||
await Promise.all(uids.map(uid => method(uid))); | ||
} | ||
|
||
async function banUser(callerUid, uid, until, reason) { | ||
const isAdmin = await user.isAdministrator(uid); | ||
if (isAdmin) { | ||
throw new Error('[[error:cant-ban-other-admins]]'); | ||
} | ||
|
||
const banData = await user.bans.ban(uid, until, reason); | ||
await db.setObjectField('uid:' + uid + ':ban:' + banData.timestamp, 'fromUid', callerUid); | ||
|
||
if (!reason) { | ||
reason = await translator.translate('[[user:info.banned-no-reason]]'); | ||
} | ||
|
||
websockets.in('uid_' + uid).emit('event:banned', { | ||
until: until, | ||
reason: reason, | ||
}); | ||
} | ||
}; |