fixes for banned users

This commit is contained in:
byte[] 2019-12-08 15:13:01 -05:00
parent 84eef46c78
commit 21d9202347
2 changed files with 20 additions and 7 deletions

View file

@ -305,16 +305,23 @@ defmodule Philomena.Bans do
fingerprint_query(fingerprint, now) ++ fingerprint_query(fingerprint, now) ++
user_query(user, now) user_query(user, now)
union_all_queries(queries) bans =
|> limit(1) union_all_queries(queries)
|> Repo.one() |> limit(1)
|> Repo.all()
# Don't return a ban if the user is currently signed in.
case is_nil(user) do
true -> Enum.at(bans, 0)
false -> user_ban(bans)
end
end end
defp fingerprint_query(nil, _now), do: [] defp fingerprint_query(nil, _now), do: []
defp fingerprint_query(fingerprint, now) do defp fingerprint_query(fingerprint, now) do
[ [
Fingerprint Fingerprint
|> select([:id, :reason, :valid_until]) |> select([f], %{reason: f.reason, valid_until: f.valid_until, generated_ban_id: f.generated_ban_id, type: "FingerprintBan"})
|> where([f], f.enabled and f.valid_until > ^now) |> where([f], f.enabled and f.valid_until > ^now)
|> where([f], f.fingerprint == ^fingerprint) |> where([f], f.fingerprint == ^fingerprint)
] ]
@ -326,7 +333,7 @@ defmodule Philomena.Bans do
[ [
Subnet Subnet
|> select([:id, :reason, :valid_until]) |> select([s], %{reason: s.reason, valid_until: s.valid_until, generated_ban_id: s.generated_ban_id, type: "SubnetBan"})
|> where([s], s.enabled and s.valid_until > ^now) |> where([s], s.enabled and s.valid_until > ^now)
|> where(fragment("specification >>= ?", ^inet)) |> where(fragment("specification >>= ?", ^inet))
] ]
@ -336,7 +343,7 @@ defmodule Philomena.Bans do
defp user_query(user, now) do defp user_query(user, now) do
[ [
User User
|> select([:id, :reason, :valid_until]) |> select([u], %{reason: u.reason, valid_until: u.valid_until, generated_ban_id: u.generated_ban_id, type: "UserBan"})
|> where([u], u.enabled and u.valid_until > ^now) |> where([u], u.enabled and u.valid_until > ^now)
|> where([u], u.user_id == ^user.id) |> where([u], u.user_id == ^user.id)
] ]
@ -346,4 +353,10 @@ defmodule Philomena.Bans do
do: query do: query
defp union_all_queries([query | rest]), defp union_all_queries([query | rest]),
do: query |> union_all(^union_all_queries(rest)) do: query |> union_all(^union_all_queries(rest))
defp user_ban(bans) do
bans
|> Enum.filter(& &1.type == "UserBan")
|> Enum.at(0)
end
end end

View file

@ -27,7 +27,7 @@ defmodule PhilomenaWeb.FilterBannedUsersPlug do
def maybe_halt(_current_ban, conn, redirect_url) do def maybe_halt(_current_ban, conn, redirect_url) do
conn conn
|> Controller.put_flash(:error, "You are currently banned.") |> Controller.put_flash(:error, "You are currently banned.")
|> Controller.redirect(to: redirect_url) |> Controller.redirect(external: redirect_url)
|> Conn.halt() |> Conn.halt()
end end
end end