Added entity reporting.
This commit is contained in:
+36
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user