Files
once-campfire/app/models/user/bannable.rb
Kevin McConnell 30fe6ab121 Add IP-based user banning
This adds the ability to ban a user by their IP address.

When an admin is viewing a user profile, a new "Ban user" button is
present. Clicking on that will:

- Create a ban on the IP addresses that were tracked for that user's
  sessions
- Remove all the messages authored by that user
- Log the user out immediately

In addition, that user will no longer be shown in most user lists in the
app. They are still shown to admins, in account settings. Viewing their
profile from there will now show a "Remove ban" button which can be used
to restore their access (it doesn't restore their messages though --
those are already gone -- it just removes the blocks so they can log in
again).
2025-11-26 14:30:38 +00:00

43 lines
745 B
Ruby

module User::Bannable
extend ActiveSupport::Concern
def ban
transaction do
create_bans_from_sessions
apply_ban
banned!
end
end
def unban
transaction do
bans.delete_all
active!
end
end
def remove_banned_content_later
RemoveBannedContentJob.perform_later(self)
end
def remove_banned_content
messages.each do |message|
message.destroy
message.broadcast_remove
end
end
private
def create_bans_from_sessions
sessions.pluck(:ip_address).compact_blank.uniq.each do |ip|
bans.create!(ip_address: ip)
end
end
def apply_ban
close_remote_connections
sessions.delete_all
remove_banned_content_later
end
end