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

99 lines
2.9 KiB
Elixir
Raw 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]
def index(conn, %{"q" => q}) when is_binary(q) do
SubnetBan
|> 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)
)
|> 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)
2019-12-16 20:24:38 +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
true -> conn
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end