Added entity reporting.

This commit is contained in:
2026-11-13 00:23:13 +01:00
parent 6143c41668
commit 1d219850d7
18 changed files with 840 additions and 5 deletions
+36 -2
View File
@@ -126,19 +126,53 @@ export async function removeTemplate(templateId, reqUser) {
.delete();
}
// a ban without a scope applies to everything that checks for bans
export const banScopes = ['revisions', 'reports'];
export async function createBan(ban, reqUser) {
if (reqUser.role !== 'admin') {
if (reqUser?.role !== 'admin') {
throw new HttpError('You do not have sufficient privileges to set a ban', 403);
}
if (ban.scope && !banScopes.includes(ban.scope)) {
throw new HttpError(`Invalid ban scope '${ban.scope}', must be one of: ${banScopes.join(', ')}`, 400);
}
const targetUser = ban.userId && await knex('users').where('id', ban.userId).first();
const curatedBan = {
user_id: ban.userId,
username: ban.username,
ip: ban.banIp && targetUser.last_ip,
ip: ban.banIp && targetUser?.last_ip,
scope: ban.scope || null,
banned_by: reqUser.id,
expires_at: knex.raw('now() + make_interval(mins => :minutes)', { minutes: config.bans.defaultExpiry }),
};
await knex('bans').insert(curatedBan);
}
// throws if an active ban for this scope (or an unscoped ban) matches the user's ID, username or IP
export async function verifyNotBanned(reqUser, scope, message = 'You are not allowed to do this') {
const ban = await knex('bans')
.where('expires_at', '>', knex.fn.now())
.where((builder) => {
builder
.whereNull('scope')
.orWhere('scope', scope);
})
.where((builder) => {
builder
.where('user_id', reqUser.id)
.orWhere('username', reqUser.username);
if (reqUser.ip) {
builder.orWhereRaw('ip >>= ?::inet', [reqUser.ip]); // ip is a cidr, so a range can be banned
}
})
.first();
if (ban) {
throw new HttpError(message, 403);
}
}