philomena/lib/philomena_web/controllers/ip_profile_controller.ex

42 lines
926 B
Elixir
Raw Permalink Normal View History

2019-12-08 02:49:28 +01:00
defmodule PhilomenaWeb.IpProfileController do
use PhilomenaWeb, :controller
alias Philomena.UserIps.UserIp
alias Philomena.Bans.Subnet
alias Philomena.Repo
import Ecto.Query
plug :authorize_ip
def show(conn, %{"id" => ip}) do
{:ok, ip} = EctoNetwork.INET.cast(ip)
user_ips =
UserIp
|> where(ip: ^ip)
|> order_by(desc: :updated_at)
|> preload(:user)
|> Repo.all()
subnet_bans =
Subnet
|> where([s], fragment("? >>= ?", s.specification, ^ip))
|> order_by(desc: :created_at)
2019-12-08 02:49:28 +01:00
|> Repo.all()
2020-01-11 05:20:19 +01:00
render(conn, "show.html",
title: "#{ip}'s IP profile",
ip: ip,
user_ips: user_ips,
subnet_bans: subnet_bans
)
2019-12-08 02:49:28 +01:00
end
defp authorize_ip(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :show, :ip_address) do
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-08 02:49:28 +01:00
end
end
2019-12-16 20:24:38 +01:00
end