philomena/lib/philomena_web/controllers/admin/subnet_ban_controller.ex

126 lines
3.7 KiB
Elixir
Raw Permalink Normal View History

2019-12-13 19:50:35 +01:00
defmodule PhilomenaWeb.Admin.SubnetBanController do
use PhilomenaWeb, :controller
alias Philomena.Bans.Subnet, as: SubnetBan
alias Philomena.Bans
alias Philomena.Repo
import Ecto.Query
plug :verify_authorized
plug :load_resource, model: SubnetBan, only: [:edit, :update, :delete]
2020-09-17 20:35:47 +02:00
plug :check_can_delete when action in [:delete]
2019-12-13 19:50:35 +01:00
def index(conn, %{"q" => q}) when is_binary(q) do
SubnetBan
2020-01-11 05:20:19 +01:00
|> where(
[sb],
sb.generated_ban_id == ^q or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", sb.reason, ^q) or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", sb.note, ^q)
2019-12-13 19:50:35 +01:00
)
|> load_bans(conn)
end
def index(conn, %{"ip" => ip}) when is_binary(ip) do
{:ok, ip} = EctoNetwork.INET.cast(ip)
SubnetBan
|> where([sb], fragment("? >>= ?", sb.specification, ^ip))
|> load_bans(conn)
end
def index(conn, _params) do
load_bans(SubnetBan, conn)
end
def new(conn, %{"specification" => ip}) do
{:ok, ip} = EctoNetwork.INET.cast(ip)
changeset = Bans.change_subnet(%SubnetBan{specification: ip})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New Subnet Ban", changeset: changeset)
2019-12-13 19:50:35 +01:00
end
def new(conn, _params) do
changeset = Bans.change_subnet(%SubnetBan{})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New Subnet Ban", changeset: changeset)
2019-12-13 19:50:35 +01:00
end
def create(conn, %{"subnet" => subnet_ban_params}) do
case Bans.create_subnet(conn.assigns.current_user, subnet_ban_params) do
2021-11-07 19:51:55 +01:00
{:ok, subnet_ban} ->
2019-12-13 19:50:35 +01:00
conn
2019-12-13 20:06:08 +01:00
|> put_flash(:info, "Subnet was successfully banned.")
|> moderation_log(details: &log_details/3, data: subnet_ban)
|> redirect(to: ~p"/admin/subnet_bans")
2019-12-13 19:50:35 +01:00
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = Bans.change_subnet(conn.assigns.subnet)
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing Subnet Ban", changeset: changeset)
2019-12-13 19:50:35 +01:00
end
def update(conn, %{"subnet" => subnet_ban_params}) do
case Bans.update_subnet(conn.assigns.subnet, subnet_ban_params) do
2021-11-07 19:51:55 +01:00
{:ok, subnet_ban} ->
2019-12-13 19:50:35 +01:00
conn
|> put_flash(:info, "Subnet ban successfully updated.")
|> moderation_log(details: &log_details/3, data: subnet_ban)
|> redirect(to: ~p"/admin/subnet_bans")
2019-12-13 19:50:35 +01:00
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
def delete(conn, _params) do
2021-11-07 19:51:55 +01:00
{:ok, subnet_ban} = Bans.delete_subnet(conn.assigns.subnet)
2019-12-13 19:50:35 +01:00
conn
|> put_flash(:info, "Subnet ban successfully deleted.")
|> moderation_log(details: &log_details/3, data: subnet_ban)
|> redirect(to: ~p"/admin/subnet_bans")
2019-12-13 19:50:35 +01:00
end
defp load_bans(queryable, conn) do
subnet_bans =
queryable
|> order_by(desc: :created_at)
|> preload(:banning_user)
|> Repo.paginate(conn.assigns.scrivener)
2020-01-11 05:20:19 +01:00
render(conn, "index.html",
title: "Admin - Subnet Bans",
layout_class: "layout--wide",
subnet_bans: subnet_bans
)
2019-12-13 19:50:35 +01:00
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, SubnetBan) do
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-13 19:50:35 +01:00
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
2020-09-17 20:35:47 +02:00
defp check_can_delete(conn, _opts) do
case conn.assigns.current_user.role == "admin" do
true -> conn
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
2021-11-07 19:51:55 +01:00
defp log_details(_conn, action, ban) do
2021-11-07 19:51:55 +01:00
body =
case action do
:create -> "Created a subnet ban #{ban.generated_ban_id}"
:update -> "Updated a subnet ban #{ban.generated_ban_id}"
:delete -> "Deleted a subnet ban #{ban.generated_ban_id}"
end
%{body: body, subject_path: ~p"/admin/subnet_bans"}
2021-11-07 19:51:55 +01:00
end
2019-12-13 19:50:35 +01:00
end