mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
auto ip bans
This commit is contained in:
parent
1b238f465f
commit
0d8e1c5ceb
2 changed files with 35 additions and 4 deletions
|
@ -4,8 +4,10 @@ defmodule Philomena.Bans do
|
|||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Ecto.Multi
|
||||
alias Philomena.Repo
|
||||
|
||||
alias Philomena.UserIps.UserIp
|
||||
alias Philomena.Bans.Fingerprint
|
||||
|
||||
@doc """
|
||||
|
@ -242,11 +244,40 @@ defmodule Philomena.Bans do
|
|||
|
||||
"""
|
||||
def create_user(creator, attrs \\ %{}) do
|
||||
%User{banning_user_id: creator.id}
|
||||
|> User.save_changeset(attrs)
|
||||
|> Repo.insert()
|
||||
user_ban =
|
||||
%User{banning_user_id: creator.id}
|
||||
|> User.save_changeset(attrs)
|
||||
|
||||
Multi.new()
|
||||
|> Multi.insert(:user_ban, user_ban)
|
||||
|> Multi.run(:auto_ip_ban, fn repo, %{user_ban: user_ban} ->
|
||||
UserIp
|
||||
|> where(user_id: ^user_ban.user_id)
|
||||
|> order_by(desc: :updated_at)
|
||||
|> limit(1)
|
||||
|> select([u], u.ip)
|
||||
|> Repo.one()
|
||||
|> case do
|
||||
nil ->
|
||||
{:ok, nil}
|
||||
|
||||
ip ->
|
||||
ip = masked_ip(ip)
|
||||
|
||||
%Subnet{banning_user_id: creator.id, specification: ip}
|
||||
|> Subnet.save_changeset(attrs)
|
||||
|> repo.insert()
|
||||
end
|
||||
end)
|
||||
|> Repo.isolated_transaction(:serializable)
|
||||
end
|
||||
|
||||
defp masked_ip(%Postgrex.INET{address: {_1, _2, _3, _4}} = ip),
|
||||
do: ip
|
||||
|
||||
defp masked_ip(%Postgrex.INET{address: {h1, h2, h3, h4, _5, _6, _7, _8}} = ip),
|
||||
do: %{ip | address: {h1, h2, h3, h4, 0, 0, 0, 0}, netmask: 64}
|
||||
|
||||
@doc """
|
||||
Updates a user.
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ defmodule PhilomenaWeb.Admin.UserBanController do
|
|||
|> put_flash(:info, "User was successfully banned.")
|
||||
|> redirect(to: Routes.admin_user_ban_path(conn, :index))
|
||||
|
||||
{:error, changeset} ->
|
||||
{:error, :user_ban, changeset, _changes} ->
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue