mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-19 22:27:59 +01:00
add ban filterer
This commit is contained in:
parent
ab5b7782c7
commit
13168e81cb
2 changed files with 34 additions and 1 deletions
|
@ -4,7 +4,7 @@ defmodule PhilomenaWeb.Plugs.CurrentBan do
|
|||
|
||||
## Example
|
||||
|
||||
plug PhilomenaWeb.Plugs.Ban
|
||||
plug PhilomenaWeb.Plugs.CurrentBan
|
||||
"""
|
||||
alias Philomena.Bans
|
||||
alias Plug.Conn
|
||||
|
|
33
lib/philomena_web/plugs/filter_banned_users.ex
Normal file
33
lib/philomena_web/plugs/filter_banned_users.ex
Normal 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
|
Loading…
Reference in a new issue