mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-30 14:57:59 +01:00
ban id and time field refactor
This commit is contained in:
parent
8196271976
commit
5871920c14
7 changed files with 53 additions and 90 deletions
|
@ -3,7 +3,8 @@ defmodule Philomena.Bans.Fingerprint do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
alias Philomena.Users.User
|
alias Philomena.Users.User
|
||||||
alias RelativeDate.Parser
|
import Philomena.Schema.Time
|
||||||
|
import Philomena.Schema.BanId
|
||||||
|
|
||||||
schema "fingerprint_bans" do
|
schema "fingerprint_bans" do
|
||||||
belongs_to :banning_user, User
|
belongs_to :banning_user, User
|
||||||
|
@ -24,38 +25,14 @@ defmodule Philomena.Bans.Fingerprint do
|
||||||
def changeset(fingerprint_ban, attrs) do
|
def changeset(fingerprint_ban, attrs) do
|
||||||
fingerprint_ban
|
fingerprint_ban
|
||||||
|> cast(attrs, [])
|
|> cast(attrs, [])
|
||||||
|> populate_until()
|
|> propagate_time(:valid_until, :until)
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_changeset(fingerprint_ban, attrs) do
|
def save_changeset(fingerprint_ban, attrs) do
|
||||||
fingerprint_ban
|
fingerprint_ban
|
||||||
|> cast(attrs, [:reason, :note, :enabled, :fingerprint, :until])
|
|> cast(attrs, [:reason, :note, :enabled, :fingerprint, :until])
|
||||||
|> populate_valid_until()
|
|> assign_time(:until, :valid_until)
|
||||||
|> put_ban_id()
|
|> put_ban_id("F")
|
||||||
|> validate_required([:reason, :enabled, :fingerprint, :valid_until])
|
|> validate_required([:reason, :enabled, :fingerprint, :valid_until])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp populate_until(%{data: data} = changeset) do
|
|
||||||
put_change(changeset, :until, to_string(data.valid_until))
|
|
||||||
end
|
|
||||||
|
|
||||||
defp populate_valid_until(changeset) do
|
|
||||||
changeset
|
|
||||||
|> get_field(:until)
|
|
||||||
|> Parser.parse()
|
|
||||||
|> case do
|
|
||||||
{:ok, time} ->
|
|
||||||
change(changeset, valid_until: time)
|
|
||||||
|
|
||||||
{:error, _err} ->
|
|
||||||
add_error(changeset, :until, "is not a valid absolute or relative date and time")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp put_ban_id(%{data: %{generated_ban_id: nil}} = changeset) do
|
|
||||||
ban_id = Base.encode16(:crypto.strong_rand_bytes(3))
|
|
||||||
|
|
||||||
put_change(changeset, :generated_ban_id, "F#{ban_id}")
|
|
||||||
end
|
|
||||||
defp put_ban_id(changeset), do: changeset
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,7 +3,8 @@ defmodule Philomena.Bans.Subnet do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
alias Philomena.Users.User
|
alias Philomena.Users.User
|
||||||
alias RelativeDate.Parser
|
import Philomena.Schema.Time
|
||||||
|
import Philomena.Schema.BanId
|
||||||
|
|
||||||
schema "subnet_bans" do
|
schema "subnet_bans" do
|
||||||
belongs_to :banning_user, User
|
belongs_to :banning_user, User
|
||||||
|
@ -24,38 +25,14 @@ defmodule Philomena.Bans.Subnet do
|
||||||
def changeset(subnet_ban, attrs) do
|
def changeset(subnet_ban, attrs) do
|
||||||
subnet_ban
|
subnet_ban
|
||||||
|> cast(attrs, [])
|
|> cast(attrs, [])
|
||||||
|> populate_until()
|
|> propagate_time(:valid_until, :until)
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_changeset(subnet_ban, attrs) do
|
def save_changeset(subnet_ban, attrs) do
|
||||||
subnet_ban
|
subnet_ban
|
||||||
|> cast(attrs, [:reason, :note, :enabled, :specification, :until])
|
|> cast(attrs, [:reason, :note, :enabled, :specification, :until])
|
||||||
|> populate_valid_until()
|
|> assign_time(:until, :valid_until)
|
||||||
|> put_ban_id()
|
|> put_ban_id("S")
|
||||||
|> validate_required([:reason, :enabled, :specification, :valid_until])
|
|> validate_required([:reason, :enabled, :specification, :valid_until])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp populate_until(%{data: data} = changeset) do
|
|
||||||
put_change(changeset, :until, to_string(data.valid_until))
|
|
||||||
end
|
|
||||||
|
|
||||||
defp populate_valid_until(changeset) do
|
|
||||||
changeset
|
|
||||||
|> get_field(:until)
|
|
||||||
|> Parser.parse()
|
|
||||||
|> case do
|
|
||||||
{:ok, time} ->
|
|
||||||
change(changeset, valid_until: time)
|
|
||||||
|
|
||||||
{:error, _err} ->
|
|
||||||
add_error(changeset, :until, "is not a valid absolute or relative date and time")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp put_ban_id(%{data: %{generated_ban_id: nil}} = changeset) do
|
|
||||||
ban_id = Base.encode16(:crypto.strong_rand_bytes(3))
|
|
||||||
|
|
||||||
put_change(changeset, :generated_ban_id, "S#{ban_id}")
|
|
||||||
end
|
|
||||||
defp put_ban_id(changeset), do: changeset
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,8 @@ defmodule Philomena.Bans.User do
|
||||||
|
|
||||||
alias Philomena.Users.User
|
alias Philomena.Users.User
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
alias RelativeDate.Parser
|
import Philomena.Schema.Time
|
||||||
|
import Philomena.Schema.BanId
|
||||||
|
|
||||||
schema "user_bans" do
|
schema "user_bans" do
|
||||||
belongs_to :user, User
|
belongs_to :user, User
|
||||||
|
@ -27,36 +28,19 @@ defmodule Philomena.Bans.User do
|
||||||
def changeset(user_ban, attrs) do
|
def changeset(user_ban, attrs) do
|
||||||
user_ban
|
user_ban
|
||||||
|> cast(attrs, [])
|
|> cast(attrs, [])
|
||||||
|> populate_until()
|
|> propagate_time(:valid_until, :until)
|
||||||
|> populate_username()
|
|> populate_username()
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_changeset(user_ban, attrs) do
|
def save_changeset(user_ban, attrs) do
|
||||||
user_ban
|
user_ban
|
||||||
|> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :username, :until])
|
|> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :username, :until])
|
||||||
|> populate_valid_until()
|
|> assign_time(:until, :valid_until)
|
||||||
|> populate_user_id()
|
|> populate_user_id()
|
||||||
|> put_ban_id()
|
|> put_ban_id("U")
|
||||||
|> validate_required([:reason, :enabled, :user_id, :valid_until])
|
|> validate_required([:reason, :enabled, :user_id, :valid_until])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp populate_until(%{data: data} = changeset) do
|
|
||||||
put_change(changeset, :until, to_string(data.valid_until))
|
|
||||||
end
|
|
||||||
|
|
||||||
defp populate_valid_until(changeset) do
|
|
||||||
changeset
|
|
||||||
|> get_field(:until)
|
|
||||||
|> Parser.parse()
|
|
||||||
|> case do
|
|
||||||
{:ok, time} ->
|
|
||||||
change(changeset, valid_until: time)
|
|
||||||
|
|
||||||
{:error, _err} ->
|
|
||||||
add_error(changeset, :until, "is not a valid absolute or relative date and time")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp populate_username(changeset) do
|
defp populate_username(changeset) do
|
||||||
case maybe_get_by(:id, get_field(changeset, :user_id)) do
|
case maybe_get_by(:id, get_field(changeset, :user_id)) do
|
||||||
nil -> changeset
|
nil -> changeset
|
||||||
|
@ -71,13 +55,6 @@ defmodule Philomena.Bans.User do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp put_ban_id(%{data: %{generated_ban_id: nil}} = changeset) do
|
|
||||||
ban_id = Base.encode16(:crypto.strong_rand_bytes(3))
|
|
||||||
|
|
||||||
put_change(changeset, :generated_ban_id, "U#{ban_id}")
|
|
||||||
end
|
|
||||||
defp put_ban_id(changeset), do: changeset
|
|
||||||
|
|
||||||
defp maybe_get_by(_field, nil), do: nil
|
defp maybe_get_by(_field, nil), do: nil
|
||||||
defp maybe_get_by(field, value), do: Repo.get_by(User, [{field, value}])
|
defp maybe_get_by(field, value), do: Repo.get_by(User, [{field, value}])
|
||||||
end
|
end
|
||||||
|
|
10
lib/philomena/schema/ban_id.ex
Normal file
10
lib/philomena/schema/ban_id.ex
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
defmodule Philomena.Schema.BanId do
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
def put_ban_id(%{data: %{generated_ban_id: nil}} = changeset, prefix) do
|
||||||
|
ban_id = Base.encode16(:crypto.strong_rand_bytes(3))
|
||||||
|
|
||||||
|
put_change(changeset, :generated_ban_id, "#{prefix}#{ban_id}")
|
||||||
|
end
|
||||||
|
def put_ban_id(changeset, _prefix), do: changeset
|
||||||
|
end
|
23
lib/philomena/schema/time.ex
Normal file
23
lib/philomena/schema/time.ex
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
defmodule Philomena.Schema.Time do
|
||||||
|
alias RelativeDate.Parser
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
def assign_time(changeset, field, target_field) do
|
||||||
|
changeset
|
||||||
|
|> get_field(field)
|
||||||
|
|> Parser.parse()
|
||||||
|
|> case do
|
||||||
|
{:ok, time} ->
|
||||||
|
put_change(changeset, target_field, time)
|
||||||
|
|
||||||
|
_err ->
|
||||||
|
add_error(changeset, field, "is not a valid relative or absolute date and time")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def propagate_time(changeset, field, target_field) do
|
||||||
|
time = get_field(changeset, field)
|
||||||
|
|
||||||
|
put_change(changeset, target_field, to_string(time))
|
||||||
|
end
|
||||||
|
end
|
|
@ -126,25 +126,24 @@ defmodule PhilomenaWeb.AppView do
|
||||||
|> to_string()
|
|> to_string()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp text_or_na(nil), do: "N/A"
|
def link_to_ip(_conn, nil), do: content_tag(:code, "null")
|
||||||
defp text_or_na(text), do: to_string(text)
|
|
||||||
|
|
||||||
def link_to_ip(conn, ip) do
|
def link_to_ip(conn, ip) do
|
||||||
link(to: Routes.ip_profile_path(conn, :show, to_string(ip))) do
|
link(to: Routes.ip_profile_path(conn, :show, to_string(ip))) do
|
||||||
[
|
[
|
||||||
content_tag(:i, "", class: "fas fa-network-wired"),
|
content_tag(:i, "", class: "fas fa-network-wired"),
|
||||||
" ",
|
" ",
|
||||||
text_or_na(ip)
|
to_string(ip)
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def link_to_fingerprint(_conn, nil), do: content_tag(:code, "null")
|
||||||
def link_to_fingerprint(conn, fp) do
|
def link_to_fingerprint(conn, fp) do
|
||||||
link(to: Routes.fingerprint_profile_path(conn, :show, fp)) do
|
link(to: Routes.fingerprint_profile_path(conn, :show, fp)) do
|
||||||
[
|
[
|
||||||
content_tag(:i, "", class: "fas fa-desktop"),
|
content_tag(:i, "", class: "fas fa-desktop"),
|
||||||
" ",
|
" ",
|
||||||
String.slice(text_or_na(fp), 0..6)
|
String.slice(fp, 0..6)
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue