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
|
|
|
|
{:ok, _subnet_ban} ->
|
|
|
|
conn
|
2019-12-13 20:06:08 +01:00
|
|
|
|> put_flash(:info, "Subnet was successfully banned.")
|
2019-12-13 19:50:35 +01:00
|
|
|
|> redirect(to: Routes.admin_subnet_ban_path(conn, :index))
|
|
|
|
|
|
|
|
{: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
|
|
|
|
{:ok, _subnet_ban} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Subnet ban successfully updated.")
|
|
|
|
|> redirect(to: Routes.admin_subnet_ban_path(conn, :index))
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
{:ok, _subnet_ban} = Bans.delete_subnet(conn.assigns.subnet)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Subnet ban successfully deleted.")
|
|
|
|
|> redirect(to: Routes.admin_subnet_ban_path(conn, :index))
|
|
|
|
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
|
2019-12-13 19:50:35 +01:00
|
|
|
end
|