mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
fixes for banned users
This commit is contained in:
parent
84eef46c78
commit
21d9202347
2 changed files with 20 additions and 7 deletions
|
@ -305,16 +305,23 @@ defmodule Philomena.Bans do
|
|||
fingerprint_query(fingerprint, now) ++
|
||||
user_query(user, now)
|
||||
|
||||
bans =
|
||||
union_all_queries(queries)
|
||||
|> limit(1)
|
||||
|> Repo.one()
|
||||
|> 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
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue