From 13168e81cb0929073df1de73a2da7febd4220e34 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 15 Nov 2019 19:48:17 -0500 Subject: [PATCH] add ban filterer --- lib/philomena_web/plugs/current_ban.ex | 2 +- .../plugs/filter_banned_users.ex | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lib/philomena_web/plugs/filter_banned_users.ex diff --git a/lib/philomena_web/plugs/current_ban.ex b/lib/philomena_web/plugs/current_ban.ex index e8d04eaf..c9acc682 100644 --- a/lib/philomena_web/plugs/current_ban.ex +++ b/lib/philomena_web/plugs/current_ban.ex @@ -4,7 +4,7 @@ defmodule PhilomenaWeb.Plugs.CurrentBan do ## Example - plug PhilomenaWeb.Plugs.Ban + plug PhilomenaWeb.Plugs.CurrentBan """ alias Philomena.Bans alias Plug.Conn diff --git a/lib/philomena_web/plugs/filter_banned_users.ex b/lib/philomena_web/plugs/filter_banned_users.ex new file mode 100644 index 00000000..f5b820ae --- /dev/null +++ b/lib/philomena_web/plugs/filter_banned_users.ex @@ -0,0 +1,33 @@ +defmodule PhilomenaWeb.Plugs.FilterBannedUsers do + @moduledoc """ + This plug redirects back if there is a ban for the current user. + CurrentBan must also be plugged, and it must come after it. + + ## Example + + plug PhilomenaWeb.Plugs.FilterBannedUsers + """ + alias Phoenix.Controller + alias Plug.Conn + + @doc false + @spec init(any()) :: any() + def init(opts), do: opts + + @doc false + @spec call(Conn.t(), any()) :: Conn.t() + def call(conn, _opts) do + redirect_url = conn.assigns.referrer + + conn.assigns.current_ban + |> maybe_halt(conn, redirect_url) + end + + def maybe_halt(nil, conn, _redirect_url), do: conn + def maybe_halt(_current_ban, conn, redirect_url) do + conn + |> Controller.put_flash(:error, "You are currently banned.") + |> Controller.redirect(to: redirect_url) + |> Conn.halt() + end +end \ No newline at end of file