From 21d9202347e31e3731d75bfde071b736f467881a Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 8 Dec 2019 15:13:01 -0500 Subject: [PATCH] fixes for banned users --- lib/philomena/bans.ex | 25 ++++++++++++++----- .../plugs/filter_banned_users_plug.ex | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/philomena/bans.ex b/lib/philomena/bans.ex index 19a01664..3e2c4500 100644 --- a/lib/philomena/bans.ex +++ b/lib/philomena/bans.ex @@ -305,16 +305,23 @@ defmodule Philomena.Bans do fingerprint_query(fingerprint, now) ++ user_query(user, now) - union_all_queries(queries) - |> limit(1) - |> Repo.one() + bans = + union_all_queries(queries) + |> 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 defp fingerprint_query(nil, _now), do: [] defp fingerprint_query(fingerprint, now) do [ 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.fingerprint == ^fingerprint) ] @@ -326,7 +333,7 @@ defmodule Philomena.Bans do [ 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(fragment("specification >>= ?", ^inet)) ] @@ -336,7 +343,7 @@ defmodule Philomena.Bans do defp user_query(user, now) do [ 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.user_id == ^user.id) ] @@ -346,4 +353,10 @@ defmodule Philomena.Bans do do: query defp union_all_queries([query | 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 diff --git a/lib/philomena_web/plugs/filter_banned_users_plug.ex b/lib/philomena_web/plugs/filter_banned_users_plug.ex index 5b92a9ef..37cb1612 100644 --- a/lib/philomena_web/plugs/filter_banned_users_plug.ex +++ b/lib/philomena_web/plugs/filter_banned_users_plug.ex @@ -27,7 +27,7 @@ defmodule PhilomenaWeb.FilterBannedUsersPlug do def maybe_halt(_current_ban, conn, redirect_url) do conn |> Controller.put_flash(:error, "You are currently banned.") - |> Controller.redirect(to: redirect_url) + |> Controller.redirect(external: redirect_url) |> Conn.halt() end end \ No newline at end of file