add ban filterer

This commit is contained in:
byte[] 2019-11-15 19:48:17 -05:00
parent ab5b7782c7
commit 13168e81cb
2 changed files with 34 additions and 1 deletions

View file

@ -4,7 +4,7 @@ defmodule PhilomenaWeb.Plugs.CurrentBan do
## Example
plug PhilomenaWeb.Plugs.Ban
plug PhilomenaWeb.Plugs.CurrentBan
"""
alias Philomena.Bans
alias Plug.Conn

View file

@ -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